ASP.NET Retrieves Hidden Field Value After Partial Postback

on 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 add hiddenFields to the placeholder, and then set their values โ€‹โ€‹using a clientide script, and then try to read the values โ€‹โ€‹again on an asynchronous postback in the updatepanel_Load event.

The problem is that FindControl returns null, because at this moment placeholder1.Controls.Count is 0. Why is it zero? I added a hidden field before postback.

Thanks for any help

+1
source share
3 answers

Any controls that you add dynamically will disappear after the postback. Therefore, it does not exist when the page returns. Like Layoric said that it is destroyed during the life cycle of the page. I would say if you can just put a hidden field inline, since it is a hidden field, and if you do not need it, just do not look at it (it can still sit there otherwise).

Keep in mind that when an ASP.NET page "goes back", it goes through the entire life cycle of the page. This means that when the page first loads, it goes through the page preinit, init, load, prerender, render, etc. Then, when it goes back, it goes through at least preinit, init and load (there may be other events, I canโ€™t remember from my head) before any events are triggered.

+2
source

Use this HttpContext.Request.Form[hiddenField1.UniqueID] .

+1
source

Why is HttpContext.Request.Form["hiddenField1"] not working?

0
source

All Articles