views:

219

answers:

2

I have a component 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.

A: 

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.

kniemczak
It's not that i'm not accessing the hidden field's value property. It's that the hidden field has no value after the postback (or before for that matter). When i do a view source of the html after the postback, i'd expect to see the value calculated set in the field.
SpaceCowboy74
Can you update your question to include the tag in the aspx page so we can look at the attributes there? Also any code in the Page_Load() would be nice to see as well.
kniemczak
A: 

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);
}
o6tech