Reputation: 149
I'm trying to change a hidden field with javascript and then use that changed value in my codebehind. I have a breakpoint in Page_Load to check if the value of HiddenField1 has changed but it always remains 0 on postback.
<script type="text/javascript">
$(document).ready(function () {
var hiddenControl = '<%= HiddenField1.ClientID %>';
var s = $('#cbox');
$("#cbox").combobox({
selected: function (event, ui) {
alert(s.val());
document.getElementById(hiddenControl).value = s.val();
alert(document.getElementById(hiddenControl).value);
}
});
});
<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="False" Value="0" />
If I can't get this to work is there any other method to pass information between javascript and c# codebehind ?
Upvotes: 3
Views: 5637
Reputation: 156
My answer is similar to what others said to get a hidden variable/control that can be passed back to the Server Side Code Behind like Button Event or Page_Load, etc. I got the idea from them. But here it is:
<asp:TextBox ID="hdnErrorMsg" ClientIDMode="Static" style="display: none;" runat="server"/>
This can be set in JavaScript, and it will return to Server Side Asp.NET and be used and examined. If you want to preserve it when it goes back to the client, you may have to put it back and all other things that you need. In my case it was an error message. I wanted it to stop in the ASP.NET button click event for the save and exit, but I needed to put the error stuff back so that when it went back to the client it would be there again.
Upvotes: 0
Reputation: 1
Put your HiddenField inside the UpdatePanel and use the HiddenField with the ClientID.
Upvotes: 0
Reputation: 11
Although you can always use set get method to store the values from java script ... But here is some simple way to solve it...
case 1 - If control is just for storing purpose ,
put display : none in it's style and make
visible = true in the attribute of the control.
case 2 - if control is to be displayed but in disabled mode put ReadOnly="true" instead of
eg..
<asp:textbox runat="server" ID="textbox1" Enabled="false"></asp:textbox> will not work
<asp:textbox runat="server" ID="textbox2" ReadOnly="true"></asp:textbox> will work
Upvotes: 0
Reputation: 15413
You may want to note the client side ClientID of you HiddenField and then look at the corresponding value in the Request.Form collection server side during postback.
This way you will check the value sent to the server. If the value is correct, the problem might occur because something disturbs ProcessPostData (manual resetting or dynamic change of form organization for example)
Though it is difficult to give an advice without the whole code, I agree with those saying that EnableViewState=false is surprising.
Upvotes: 1
Reputation: 142
try to do the same task using html hidden variable
<input id="hdName" runat="server" type="hidden" />
As we have provided runat="server" we will be able to access this variable on server side and on hdName.Value we can get the value on server side. Thought it is html control we need to provide hdName.ClientID in javascript as we have given runat=server.
document.getElementById('<%= hdName.ClientID %>').value
Hopefully this will solve your issue and give you desired result. Thanks
Upvotes: 0
Reputation: 13115
Check out the example on the MS documentation for HiddenField:
Form1.HiddenField1.value = s.val();
Bottom of this page - http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx
Upvotes: 0