asp.net 我的列表在回发时消失 - 找不到我丢失的内容?
这可能是一个简单的问题,但我无法弄清楚。啊...
带有自定义用户控件的 C# asp.net 应用程序。我的自定义控件是一个简单的图像列表,包含在链接中,用作父页面的导航工具。
当我单击链接时,它会进行回发:我的单击事件似乎没有触发,并且当页面重新加载时,图像列表消失。
我在父页面和自定义控件中都有 EnableViewState="true"
。据我了解,这足以保留我的选项列表吗?如果我每次重新加载列表并重新绑定我的事件处理程序,我会认为该事件将会丢失,对吗?
这是(缩写代码)我将控件添加到面板的位置。
public void LoadMediaOptions() {
loop through a datareader {
LinkButton b = new LinkButton();
b.Click += new EventHandler(navOption_Click);
b.Attributes.Add("mediaID", mediaID.ToString());
HtmlImage i = new HtmlImage();
i.Src = strThumbLink;
i.Width = 120;
i.Height = 90;
b.Controls.Add(i);
_loadedMediaHtml.Add(b);
panMediaOptions.Controls.Add(b);
}
}
目前,此方法仅在 Page_Load 方法的父页面中调用一次:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
uxMediaNav.LoadMediaOptions();
}
catch (Exception ex)
{
throw ex;
}
}
页面回发后,没有为我加载任何媒体选项。我对 EnableViewState 的理解是它会保留我动态加载的元素,但事实并非如此。
This is probably an easy one, but I can't figure it out. argh...
C# asp.net application with a custom user control. My custom control is a simple list of images wrapped in a link to be used as a navigation tool for the parent page.
When I click a link, it does a postback: my click event does not seem to fire, and when the page reloads, the list of images disappears.
I have EnableViewState="true"
in both the parent page and the custom control. It is my understanding that this would be enough to preserve my list of options? If I reload the list each time and rebind my event handler, I would think then that the event would be lost, correct?
Here is (abbreviated code) where I add the controls to my panel.
public void LoadMediaOptions() {
loop through a datareader {
LinkButton b = new LinkButton();
b.Click += new EventHandler(navOption_Click);
b.Attributes.Add("mediaID", mediaID.ToString());
HtmlImage i = new HtmlImage();
i.Src = strThumbLink;
i.Width = 120;
i.Height = 90;
b.Controls.Add(i);
_loadedMediaHtml.Add(b);
panMediaOptions.Controls.Add(b);
}
}
This method is currently only being called once in the parent page in the Page_Load method:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!Page.IsPostBack)
uxMediaNav.LoadMediaOptions();
}
catch (Exception ex)
{
throw ex;
}
}
After the page posts-back, there are no media options loaded for me. My understanding of the EnableViewState is that it would preserve the elements I have loaded dynamically, but this is not the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在页面生命周期中构建控件的时间太晚了。 ViewState 在 init 之后、Load 之前加载。有关页面生命周期,请参阅此处。
这意味着您的自定义控件无法保留任何状态,因为加载状态发生时控件尚不存在。将您的控制结构移至 PreInit,它应该可以工作。
控制事件在 Load 之后但 onLoadComplete 之前触发。事件实际上是在请求中传递的,并根据ID匹配到一个控件。如果在触发控件事件时可以找到该控件,则该事件将触发。因此,事件通常不依赖于 ViewState,更改事件除外。更改事件之所以如此,是因为 ASP.NET 需要能够将 ViewState 中保留的原始值与当前请求值进行比较,以便知道是否触发该事件。
为了确保触发任何更改事件,您需要再次确保自定义控件中的控件是在 PreInit 上创建的。
You are constructing your controls too late in the page life cycle. ViewState is loaded after init and before Load. See here for the page life cycle.
This means that your custom control is unable to persist any state as the controls do not yet exist when load state occurs. Move your control construction to PreInit and it should work.
Control events fire after Load but before onLoadComplete. The event is actually passed in the request and matched to a control based on ID. Provided the control can be found by the time control events are triggered then the event will fire. Hence events generally do not rely on ViewState, with the exception of change events. Change events do because ASP.NET needs to be able to compare the original value persisted in ViewState with the current request value in order to know whether to fire the event.
To ensure any change events fire you again need to make sure the controls within your custom control are created on PreInit.
解决方案很简单:将changeControl 放在Page_Load 方法中。
The solution is simple: put the changeControl in the Page_Load method.