事件发生后 ASP.NET UserControl 未更新
我有一个代表表的 asp.net 控件和代表表中一行的控件。每行包含一个日期文本字段和一个提交输入日期的按钮。
<asp:TextBox ID="TextBox" runat="server"/>
<asp:ImageButton ID="SubmitButton" onclick="SubmitButton_Click" runat="server" />
该按钮会触发一个事件,从而触发数据库中的更新。
protected void SubmitButton_Click(object sender, EventArgs e)
{
//Validate information in TextBox
//Submit to database
}
这应该会导致根据文本框中输入的信息显示一组新的行。但是,除非我强制刷新页面,否则不会显示更新的信息。我怀疑这是因为事件处理属于 asp.net 页面/控件生命周期的位置。据我所知,在事件实际触发之前,值已加载到行中,这导致它们获取旧值,而不是应有的新值。
我的问题是解决这个问题的最佳方法是什么?我假设这类似于检查页面加载事件中是否有回发,而不是加载用户控件行,而是以某种方式导致它们从事件中加载或刷新,但我不确定应该做什么看起来像。
编辑:我认为我需要调用在 Page_Load 中调用的相同函数,但我不确定如何从我猜的自定义控件中访问它。
I have a asp.net control that represents a table and a control that represents a row in the table. Each row contains a text field for a date and a button to submit the date entered.
<asp:TextBox ID="TextBox" runat="server"/>
<asp:ImageButton ID="SubmitButton" onclick="SubmitButton_Click" runat="server" />
The button fires an event which triggers an update in the database.
protected void SubmitButton_Click(object sender, EventArgs e)
{
//Validate information in TextBox
//Submit to database
}
This should cause a new set of rows to be shown based on the information entered in the text box. However, the updated information is not being shown unless I force a page refresh. I suspect this is because of where event handling falls in the asp.net page/control lifecycle. As near as I can tell the values are loaded into the rows before the event is actually triggered, which causes them to get the old values and not the new values that they should have.
My question is what is the best way to get around this? I'm assuming it will be something like checking to see if it's a postback in the page load event and not loading the user control rows but instead causing them to load or to refresh from the event somehow but I'm not sure what that should look like.
Edit: I think I need to call the same function that is being called in the Page_Load but I'm not sure how to get to it from the custom control I guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要在事件触发后重新绑定网格。
I think you need to re-bind your grid after your event has fired.
正如 Locksfree 所说,您可能正在寻找重新绑定。
回发不会自动显示新数据,除非您通过调用 ASP.NET 对象的 DataBind() 专门请求新数据。
如果您动态创建表,则必须重新运行代码以再次重建表以获取新数据。根据它的构建方式,您也许可以只部分重新加载新数据。
As Locksfree said, a rebind is what you are probably looking for.
Postback does not automatically show new data unless you specifically request the new data by calling DataBind() of your asp.net object.
If you are dynamically creating the table then you will have to rerun the code to rebuild your table again to get the new data. Depending on how it is built you may be able to get away with a partial reload of just the new data.