in my ASP.NET page I have an update panel, and in the updatepanel_Load event I have the following code:
if (!IsPostBack || triggeredRefresh.Value == "1")
{
HiddenField hiddenField = new HiddenField();
hiddenField.ID ="hiddenField1";
hiddenField.Value = "0";
placeHolder1.Controls.Add(hiddenField);
}
else if ( triggeredCheck.Value == "1" )
{
HiddenField hiddenField = placeHolder1.FindControl("hiddenField1") as HiddenField;
var x = Convert.ToInt32(hiddenField.Value);
}
so basically I'm adding hiddenFields to a placeholder upon, then setting their values with a clientside script, then trying to read the values again on an asynchronous postback in the updatepanel_Load event.
The problem is that FindControl returns null because the placeholder1.Controls.Count is 0 at this point. Why is it zero? I added the hidden field prior to the postback.
Thanks for any help
in my ASP.NET page I have an update panel, and in the updatepanel_Load event I have the following code:
if (!IsPostBack || triggeredRefresh.Value == "1")
{
HiddenField hiddenField = new HiddenField();
hiddenField.ID ="hiddenField1";
hiddenField.Value = "0";
placeHolder1.Controls.Add(hiddenField);
}
else if ( triggeredCheck.Value == "1" )
{
HiddenField hiddenField = placeHolder1.FindControl("hiddenField1") as HiddenField;
var x = Convert.ToInt32(hiddenField.Value);
}
so basically I'm adding hiddenFields to a placeholder upon, then setting their values with a clientside script, then trying to read the values again on an asynchronous postback in the updatepanel_Load event.
The problem is that FindControl returns null because the placeholder1.Controls.Count is 0 at this point. Why is it zero? I added the hidden field prior to the postback.
Thanks for any help
Share Improve this question asked Jun 16, 2011 at 22:42 Jimmy Jimmy 3591 gold badge9 silver badges14 bronze badges 4- upon the initial updatepanel_Load * – Jimmy Commented Jun 16, 2011 at 22:43
- 1 Why are you adding the HiddenField programmatically? I think the problem is that it field is getting created and destroyed in the life cycle and that viewstate will not be holding any value for it. Also, if you are going to add controls in the code behind like that, I would do it in the Init phase to avoid other viewstate issues. – Darren Reid Commented Jun 16, 2011 at 22:55
-
Did you check if
if
block is executed beforeelse if
? Just wanted to confirm iftriggeredRefresh
&triggeredCheck
are properly set. – Null Head Commented Jun 16, 2011 at 23:17 - Reddy, yes I did check this already, and the first part of the if block is always executed first.. Layoric I am adding the field programmatically as I am actually looping through rows in a database and adding the hiddenfields dynamically based on what is in the database... Is there a better way of doing this? – Jimmy Commented Jun 16, 2011 at 23:26
3 Answers
Reset to default 2Any controls that you add dynamically will disappear upon postbacks. Therefore it does not exist when the page is returned. Like the Layoric said it is being destroyed during the page lifecycle. I would say if you can then just put the hiddenfield inline, as it is a hiddenfield and if you don't need it then just don't look at it (it can still sit there otherwise).
Keep in mind that when an ASP.NET page is "posted back" it goes through the entire page lifecycle. This means that when the page is first loaded it goes through page preinit, init, load, prerender, render, etc. Then when it is posted back it goes through at least preinit, init, and load (there could be other events as well, I can't recall off the top of my head) before any events are fired.
Use this HttpContext.Request.Form[hiddenField1.UniqueID]
.
Why won't HttpContext.Request.Form["hiddenField1"]
work?