DotNetInfo
DotNetInfo

Reputation: 3404

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

Upvotes: 5

Views: 5023

Answers (4)

Nitin Kumar
Nitin Kumar

Reputation: 1

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.

Upvotes: 0

xling
xling

Reputation: 252

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.

Upvotes: 2

IUnknown
IUnknown

Reputation: 22448

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

Upvotes: 0

DotNetInfo
DotNetInfo

Reputation: 3404

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

Upvotes: 8

Related Questions