create hidden field with javascript and get its value in code behind on postback
-
I dont want to have to register a hidden field in code behind, i want to dynamically create the hidden field in javascript and then it should appear in the Request.Form if i had created it in javascript. But it is never there in code behind (IsPostBack == true). It use to work, and now it doesn't. Anyone see why or what im missing? //javascript HiddenValue("CurrencyID__VALUE", "CAD"); // called from other function function HiddenValue(id, value) { var NewHiddenControl = document.getElementById(id); if (NewHiddenControl == null) { NewHiddenControl = document.createElement("INPUT"); NewHiddenControl.type = "HIDDEN"; NewHiddenControl.id = id; NewHiddenControl.name = id; } if (value != null) NewHiddenControl.value = value; return NewHiddenControl.value; } // code behind if (IsPostBack && Request.Form["CurrencyID__VALUE"] != null && Request.Form["CurrencyID__VALUE"].Length > 0) CurrencyID.SetValue(Request.Form["CurrencyID__VALUE"]); Thanks.
-
I dont want to have to register a hidden field in code behind, i want to dynamically create the hidden field in javascript and then it should appear in the Request.Form if i had created it in javascript. But it is never there in code behind (IsPostBack == true). It use to work, and now it doesn't. Anyone see why or what im missing? //javascript HiddenValue("CurrencyID__VALUE", "CAD"); // called from other function function HiddenValue(id, value) { var NewHiddenControl = document.getElementById(id); if (NewHiddenControl == null) { NewHiddenControl = document.createElement("INPUT"); NewHiddenControl.type = "HIDDEN"; NewHiddenControl.id = id; NewHiddenControl.name = id; } if (value != null) NewHiddenControl.value = value; return NewHiddenControl.value; } // code behind if (IsPostBack && Request.Form["CurrencyID__VALUE"] != null && Request.Form["CurrencyID__VALUE"].Length > 0) CurrencyID.SetValue(Request.Form["CurrencyID__VALUE"]); Thanks.
Hi there, From what I can see with the code you've posted, the problem is that you've created the hidden element but it's NOT a child of the FORM. Hence on post-back the values won't be posted and hence not picked up by the server-end. Adding the following to the document.createElement part of the code should fix it...
NewHiddenControl = document.createElement("INPUT"); // ... // ... var oForm = document.getElementById("Form1"); if (oForm) oForm.appendChild(NewHiddenControl);
Hope this helps, Andy
-
Hi there, From what I can see with the code you've posted, the problem is that you've created the hidden element but it's NOT a child of the FORM. Hence on post-back the values won't be posted and hence not picked up by the server-end. Adding the following to the document.createElement part of the code should fix it...
NewHiddenControl = document.createElement("INPUT"); // ... // ... var oForm = document.getElementById("Form1"); if (oForm) oForm.appendChild(NewHiddenControl);
Hope this helps, Andy