ASP.NET得到一个局部回传后的隐藏字段的值字段、局部、回传、ASP

2023-09-06 21:48:17 作者:穿着睡衣满街跑丶

在我的ASP.NET页我有一个更新面板,并且在updatepanel_Load事件我有以下的code:

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

所以基本上我在加hiddenFields一个占位符,然后设置它们的值与客户端脚本,然后尝试再次读取值上的updatepanel_Load事件异步回发。

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.

问题是,的FindControl返回null,因为placeholder1.Controls.Count为0,在这一点上。为什么为零?我添加了隐藏字段的回发之前。

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.

感谢您的帮助

推荐答案

这是你动态地添加回发后会消失任何控制。因此,当返回的页面不存在。像Layoric说,它是在页面生命周期过程中遭到破坏。我想说如果可以则只是把hiddenfield内联,因为它是一个hiddenfield,如果你不需要它,然后就是不看它(它仍然可以坐在那里其他方式)。

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

请,当ASP.NET页面被贴背它会遍历整个页面的生命周期。这意味着,当页面第一次加载它通过网页preINIT,初始化,装载,prerender,渲染等,然后当它被调回它通过至少preINIT,init和负载(可能有其他事件,以及,我不记得把我的头顶部)的任何事件被触发之前。

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.