Lost viewstate text box loss

I am dynamically generating a form based on a drop down list. The form consists of fields (data input for decimal values ​​+ several text fields). You must add all decimal values ​​at the end and update the Total TextBox with this value. Shared text field is disabled.

When I click the "Save" button on the form after the user has entered their values, the entire form is saved in the viewstate, except for the disabled text field. When I turn on the text box, everything works fine. Keep in mind, I dynamically generate the form and update the value of the full text field with javascript to calculate (add all decimal fields).

PS I'm doing everything right to persist in the submission.

So, what is on / off, got support on viewstate

+4
source share
3 answers

Basically, I added two operators to make it work.

txtBox.Attributes.Add("readonly", "readonly"); txtBox.Style.Add("color","gray"); 

When I used txtBox.Enabled = false, it was not saved in viewstate, but did it alternatively using the above two operators on my page with the code below

+7
source

Yes, a disabled form element will not send its value to the server, you can see the request header. the disconnected item did not appear in the "get" or "post" collection.

If you want the user to not be able to edit it, you can set it as read-only.

+2
source

Add javascript to page:

 function enableTextBoxes() { $("input[type='text'][disabled='disabled']").removeAttr("disabled"); } 

And add to the server code (in Page_Load mode, PreRender or some other)

 ClientScript.RegisterOnSubmitStatement(typeof(Page), "enableTextBoxes", "enableTextBoxes();"); 

If you are using UpdatePanels, use the ScriptManager.RegisterOnSubmitStatement method

0
source

All Articles