简体   繁体   中英

Hidden field loses value in codebehind?

I am trying to pass data from my client to server using a HiddenField.

Javascript/UI.ascx

<asp:HiddenField ID="PassArgs" runat="server" EnableViewState="true"/>
//Tried using EnableViewState, but still no luck


<script>

document.getElementById("<%=PassArgs.ClientID%>").value = JSON.stringify(SP.UI.ModalDialog.get_childDialog().get_args());
alert(document.getElementById("<%=PassArgs.ClientID%>").value); 
//Alert shows correct data to be passed

</script>

However when I try to access my HiddenField from the codebehind, my result is empty.

CodeBehind

var jss = new JavaScriptSerializer();

var dict = jss.Deserialize<Dictionary<string, dynamic>>(PassArgs.Value);
//PassArgs.Value = ""     --- whyyyy???

I am posting this question as a last resort because I have spent a full work day looking at similar questions, but I am not able to get this to work for my situation.

How come my alert of 'PassArgs' contains the correct values, but my code behind shows empty? I have tried using HiddenFields, TextBoxes, and Labels, but the result is the same for all of them.

EDIT: The HiddenField seems to have the correct values when the script is run, I dont think thats the problem, but I think the script is running AFTER iv already tried to access it in the codebehind. I think that may be why the HiddenField is showing empty, because the script hasent run yet. How can I get this to run in UI.ascx before my debugger gets to the codebehind (UI.ascx.cs) to access it?

you can fetch data from server variables, so you can get the value by calling Request.Form["PassArgs"] which returns the value of your request , and you can parse the result of the request collection of your form Convert.ToString(Request.Form) as well

otherwise you can use the TextBox field with a hidden style in case it is still not working

<asp:TextBox ID="PassArgs" runat="server" style="display:none;"></asp:TextBox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM