Kelmen
Kelmen

Reputation: 1063

client side dynamically created hidden fields lost when postback

I programmatically created few hidden fields at client side, inserted into the form. I verified the form.childElementCount does increased.

var disabledState_id = "<%=HidData_DisabledState_Prefix%>" + uiObj.attr("id");
...
disabledState = $("<input type='hidden' id='" + disabledState_id + "' />");
disabledState.appendTo("form");

when postback, I can't find these hidden values in Request.Form . can't find those keys as well in Request.Form.AllKeys, as the id is fixed to a prefix.

Upvotes: 0

Views: 175

Answers (2)

A. Wolff
A. Wolff

Reputation: 74420

You need to set name attribute in order to get input value send on FORM submit.

Upvotes: 2

user3263194
user3263194

Reputation: 453

If you have a code-generated HiddenField, it needs to have the same ID and be created before the Page sets the posted values, such as in OnInit.

The following link may help you :

http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

You can use:

 protected string disabledState = "";
 protected void Page_Load(object sender, EventArgs e)
 {
      this.disabledState = Request.Form["disabledState"];
 }

In javascript you can use the following :

 var disabledState ;
     window.onload = function () {
         disabledState = document.getElementById('"+disabledState_id+"');
     };

Upvotes: 0

Related Questions