ASP.NET getting a hidden field's value after a partial postback

做~自己de王妃 提交于 2019-12-11 11:59:31

问题


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


回答1:


Any 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.




回答2:


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




回答3:


Why won't HttpContext.Request.Form["hiddenField1"] work?



来源:https://stackoverflow.com/questions/6379455/asp-net-getting-a-hidden-fields-value-after-a-partial-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!