TextBox is not showing new value in code-behind - c#

I have created a asp:TextBox. its disabled. but its values is changed via javascript. When i click a button to get the updated value in that textbox it always show me empty ("").
In my Page_Load i am doing everything in if(!Page.IsPostBack) so there is no chance of updating TextBox value on postback in Page_Load.
Where i could be wrong? how can i get updated value?

Disabled textboxes are not posted back.
See: http://www.velocityreviews.com/forums/t360866-textbox-no-value-if-disabled.html
Soultion: use a hidden or readonly field.

When you disable a control it's value is not send back in the postback. You should change the textbox to be readonly.

Solution 1: Set your textbox readonly instead of making it disabled.
Solution 2: If you really have to disable the textbox, you can add an asp:hiddenfield to record the text in the textbox, you can get the value at server side then.

Related

Asp.net Textbox.Autopostback causes another issue

I have dropdown and texboxes and one button on the form. I'm using 'autopostback=true' for all the form elements. When you fill the form you need to push 'send' button. But because of texbox.autopostback, you need to push 2 time to send form.
If you select dropdown lastly, then there is no problem. But if you're filling textboxes than you need to click 2 time to send form.
Is there any solution for it? I must use textboxes.autopostback='true' but need some solution.
Thanks,
UPDATE:
All controls stays in updatepanel element.
Set a hidden field value to "1" when textbox posts back and its value is valid, else set the hidden field value to "0"
On client-side, in pageLoad event, automatically click the button if the hidden field value is "1". So, to the user button is only clicked once and your auto submitting the form when text post back returns.
Both the above points need to be done in your code-behind.
The result of of this logic will enforce a consistent workflow, where the button can be clicked only once to submit the page.

How to make textbox as not editable in asp.net(c#)

I've creating one ASP web application, in that application one form have to update date value in the textbox field i put calender button near that textbox. it can update the date in that textbox field but its editable . i want update date value through only using calender button , i used read-only property but return empty value so not works .
Try client side html readonly attribute instead of ASP.NET server side readonly.
myTextBox.Attributes.Add("readonly", "readonly");
From MSDN,
The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code.
This is why textbox with server side readonly attribute has null value in postback.
You can use either
TextBox1.Enabled = false; OR
TextBox1.Attributes.Add("readonly","readonly");
Difference is that if you make enabled= false then you cant pass the value of the textbox. If you need to pass the value of the textbox then you should use read-only property of textbox.
On page load event of your code behind file just write following code:
yourTextBoxName.Attributes.Add("readonly","readonly");
you can also do same by using property
Enabled="false"
on your aspx page but then you will not be able access its value in code behind page.
You can make textBox non-editable in asp.net
By using read only it will give problems on post back just set this java script property
onkeydown="javascript:return false"
by using this u can have properties like readonly and have absolutely no problem will araise
You might want to use this:
TextBox1.Enabled = false;

Disabled textbox losses viewstate

I am dynamically generating the form based on the drop down selected.
The form consists of fields (data entry for decimal values + few text fields). Have to add all the decimal values at the end and update the Total TextBox with that value. Total Textbox is disabled.
When I click Save button on the form after the user have entered their values, whole form is persisted in viewstate except the disabled textbox. When I enable the textbox, everything works fine. Mind you, I am dynamically generating the form and updating the value of the total textbox using javascript to calculate (adding all decimal fields).
P.S. I am doing everything right for persisting the viewstate.
So what has the enabled/disabled got bearing on the viewstate
Basically, I added two statements to get it working.
txtBox.Attributes.Add("readonly", "readonly");
txtBox.Style.Add("color","gray");
When I used txtBox.Enabled = false, it didn't persist viewstate but did it alternatively using above two statements in my code-behind page
Yes, disabled form element will not send it's value to server side, you can look request header. disabled element not appeared at "get" or "post" collection.
If you want set user can't edit it, you can set it as readonly.
Add javascript on the page:
function enableTextBoxes() {
$("input[type='text'][disabled='disabled']").removeAttr("disabled");
}
And add to server code (in Page_Load, PreRender or some else method)
ClientScript.RegisterOnSubmitStatement(typeof(Page), "enableTextBoxes", "enableTextBoxes();");
If you use UpdatePanels then utilize the ScriptManager.RegisterOnSubmitStatement method
Please create custom text box rather than using actual textbox instance.
inherit textbox in your custom textbox and add this textbox in your dynamic form.

Get values of a textbox changed by javascript

strange bug:
i have an ajax datepicker added to a text box of my form.
i submit the form.. and I could receive all values excepting those of the datepicker checkboxes.
why is the .Text property empty of this elements?
Thanks
Is your TextBox disabled for user input, so that it might only be changed by javascript, by the datepicker?
The problem in that case, is that .NET "knows" that the control is disabled, and just assumes that the value cannot, then, have changed since it was rendered. So .NET will use the ViewStated value immediately, without checking the POST data.
There are two solutions to this:
Don't render the TextBox as disabled, but disable it with the datepicker script
Instead of relying on the TextBox's Text property, check Request.Form[myTextBox.ClientID]
Try accessing SelectedDate instead of Text.

HiddenField control maintaining viewstate

I have a strange problem. While populating formfields, the values are maintained when clicking a'submit' button that causes the page to proceed to the next stage. I try to populate a hidden field from the code behind on the event of a 'find 'button click (before clicking this 'submit' button and this works. But the value is not maintained within the hidden field when clicking the 'submit' button of that page.
Any ideas why this is?
Thanks,
(Edit to clarify) The value of form fields gets posted back to the server when the form is submitted. If you have an input that is set as type hidden and you set the value of it such that it renders in the source (look at the source after hitting your find button) then the new value will post back as well.
Check if this is the case or if you have anything else overwriting the value.

Categories

Resources