I am dynamically generating the form based on the drop down selected. The form consists o开发者_高级运维f 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.
精彩评论