事件处理后如何更新页面数据?
在 Page_Init 上,我根据几个数据库表创建了一个动态创建的控件表。 其中一个控件是 ImageButton,用于在列表中向上移动列表项。 此事件处理程序的作用是更新数据库中受影响项目的 SortOrder 列。
现在的问题是,由于控件是在 Page_Init 事件中创建的,并且稍后在触发 ImageButton 命令事件时更新 SortOrder。 使用正确的排序顺序更新表的最佳过程是什么? 如果我在事件触发后重新创建表,则 ImageButton 命令事件将不再起作用。
- 我应该实现一种方法来更新表中的数据而不重新创建它吗?
- 事件触发后我应该在代码中重新加载页面吗?
您解决这个问题的首选方法是什么?
On Page_Init I create a table of dynamically created controls based on a couple of database tables. One of the controls is an ImageButton for moving an list item up the list. What this event handler does is to update the SortOrder column in the database for the affected items.
Now the problem is that since the controls are created in the Page_Init event and the SortOrder is updated later on when the ImageButton command event is fired. What's the best procedure for updating the table with the correct SortOrder. If I recreate the table after the event has fired the ImageButton command event does not work any more.
- Should I implement a method for updating the data in the table without recreating it?
- Should I reload the page in code after the event has fired?
What's your preferred way for solving this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Init
和Load
等页面事件始终会在引发回发的事件处理程序之前触发。 这是页面生命周期的基础(查看 Peter 的可视化表示)布罗姆伯格,参见此处)。 大多数刚接触 ASP.NET 的开发人员在理解和正确处理这一“困境”时都面临着一个重大问题。执行此操作的理想方法是:
您的
Page_Init
应该调用一个过程(为了便于说明,我们将其称为BindData()
)来处理基于数据库数据的表的创建。 此方法类似于绑定到数据库数据并基于该绑定呈现 UI 元素的绑定方法。 IOW,您应该从Page_Init
方法中删除表创建代码,并将其放在单独的方法中,以便在需要时可以调用它。重要说明:此
BindData()
方法还处理将动态创建的 ImageButton 控件的事件处理程序附加到该控件的操作。 我们将其命名为ImageButton_Click
。 这对于控制事件在后续回发时触发至关重要。b. 当您的
ImageButton_Click
方法执行时,它会调用BindData()
方法来重新创建表及其绑定,但使用新的排序顺序规则。因此,首次加载时的执行顺序为:
Page_Init
BindData()
后续加载(回发时)的执行顺序为:
Page_Init
BindData()
- 附加 ImageButton 的事件处理程序。ImageButton_Click
BindData()
Page events such as
Init
andLoad
will always fire before the event handler that raised the postback. This is the basis of the Page lifecycle (For a visual representation by Peter Bromberg, see here). Most developers new to ASP.NET have a major problem understanding and appropriately handling this "quandary".The ideal way to do this is:
a. Your
Page_Init
should call a procedure (let's call itBindData()
for illustration) that handles the creation of the table based on database data. This method would be similar to a binding method that binds to the database data and renders UI elements on the basis of that binding. IOW, you should remove the table creation code from thePage_Init
method and put it in a separate method so that it can be called when needed.Important note: This
BindData()
method also handles the attaching of the eventhandler for the dynamically created ImageButton control to the control. We'll call thisImageButton_Click
. This is crucial for the control to the event to fire on subsequent postback.b. When your
ImageButton_Click
method executes, it calls theBindData()
method to recreate the table and it's bindings but with new sort order rules.So, the order of execution on first load is:
Page_Init
BindData()
The order of execution on subsequent loads (on postback) is:
Page_Init
BindData()
- Eventhandler for ImageButton attached.ImageButton_Click
BindData()
你需要这样的东西......
单击按钮
OnInit (IsPostBack = true / 1st Postback)
ImageButton_OnClick (仍然与第 1 次回发相同)
引起其他回发
You'll need something like this...
Click the button
OnInit (IsPostBack = true / 1st Postback)
ImageButton_OnClick (Still the same 1st postback)
Cause some other Postback
首先,您似乎手动将数据绑定到 UI 控件。 在 Asp.Net 中,有很多方法可以使用内置数据绑定技术来避免这种情况。 许多控件(例如 GridView)允许从给定数据源自动创建 Html 表。 还有许多其他选项,包括中继器。
无论您选择如何绑定数据,该技术都是在页面生命周期中的每次某个时刻重新绑定。
您需要...
代码看起来像这样...
如果单击“图像”按钮,您最终将调用 BindData 两次。 但这是必要的,因为可以从任何控件启动页面回发,因此您需要始终确保在页面加载时绑定数据。
Firstly, you seem to be binding your data manually to UI controls. In Asp.Net there and many ways to avoid this using built-in data binding techniques. Many controls like the GridView allow automatic creation of Html tables from a given data source. There are many other options including Repeaters.
However you do choose to bind your data, the technique is to rebind at some point every time through the page lifecycle.
You need to...
The code would look something like this...
If the Image button is clicked, you will end up calling BindData twice. But this is necessary since a page postback could be initiated from any control, you need to always ensure you bind the data when the page loads.