Set a hidden field value after postback
I have a compone开发者_StackOverflow中文版nt that determines a value on a postback event.
protected void Button_Click(object s, EventArgs e)
{
HiddenField.Value = 5;
}
There's more involved in the value of course, but HiddenField is an asp:HiddenField control with runat=server set. I have in my javascript:
var id = $("#<%= HiddenField.ClientID %>").val();
The code in the javascript is set to be run only after the postback has occured (a different client click event) for the purpose of passing the hidden field value via QueryString to another URL (since i can't do a response redirect on postbacks and the client wants it in a different page anyway).
I tried adding:
ScriptManager.RegisterHiddenField(HiddenField, "Value", string.Empty);
To a !Page.IsPostback section of code, but the ID is still not set when the javascript is run.
You could add a global js variable on your main page and then set that variable instead of the hidden field.
Page.aspx
<script>
var id='';
</script>
Page.aspx.cs
protected void btn_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "updateJavaScriptId", "id='5';", true);
}
Here is a similar issue. Maybe you can use it to modify what you are doing.
Do you need to access the client ID like that? You should be able to set the ID for the control with little issue as long as it is not in a repeater or something. That could simplify what you are trying to do.
In the example they use getElementById to get the hidden field and just pass the ID of the HiddenField.
精彩评论