事件处理后如何更新页面数据? [英] How to update page data after event handling?

查看:117
本文介绍了事件处理后如何更新页面数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Page_Init中,我创建了一个基于几个数据库表的动态创建控件的表。其中一个控件是用于将列表项移动到列表中的ImageButton。这个事件处理程序是更新受影响项目的数据库中的SortOrder列。

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.

现在的问题是,由于控件是在Page_Init事件和稍后在触发ImageButton命令事件时更新SortOrder。使用正确的SortOrder更新表的最佳过程是什么?如果在事件触发之后重新创建表,ImageButton命令事件将不起作用。

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.


  • 我是否应该实现更新表中数据的方法而不重新创建?

  • 应该事件发生后我重新加载页面代码?

您最喜欢的方法是解决这个问题?

What's your preferred way for solving this problem?

推荐答案

页面事件如 Init 加载将始终在引发回发的事件处理程序之前触发。这是页面生命周期的基础(对于彼得的视觉表示Bromberg,参见这里)。 ASP.NET的大多数开发人员都有一个很大的问题,理解和适当地处理这个quandary。

Page events such as Init and Load 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".

理想的方法是:

a。您的 Page_Init 应该调用一个过程(我们称之为 BindData()作为说明),用于处理基于表的创建数据库数据。此方法将与绑定到数据库数据的绑定方法类似,并在该绑定的基础上呈现UI元素。 IOW,您应该从 Page_Init 方法中删除表创建代码,并将其放在一个单独的方法中,以便在需要时可以调用它。

a. Your Page_Init should call a procedure (let's call it BindData() 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 the Page_Init method and put it in a separate method so that it can be called when needed.

重要说明: BindData()方法还处理将动态创建的ImageButton控件的事件处理程序附加到控制。我们将把这个 ImageButton_Click 称为。这对于控制事件在后续回发中触发是至关重要的。

Important note: This BindData() method also handles the attaching of the eventhandler for the dynamically created ImageButton control to the control. We'll call this ImageButton_Click. This is crucial for the control to the event to fire on subsequent postback.

b。当您的 ImageButton_Click 方法执行时,它调用 BindData()方法来重新创建表,它的绑定,但使用新的排序订单规则。

b. When your ImageButton_Click method executes, it calls the BindData() method to recreate the table and it's bindings but with new sort order rules.

所以,第一次加载的执行顺序是:

So, the order of execution on first load is:


  1. Page_Init

  2. BindData()

  1. Page_Init
  2. BindData()

在后续加载(在回发)上执行的顺序是:

The order of execution on subsequent loads (on postback) is:


  1. Page_Init

  2. BindData() - 附加ImageButton的事件处理程序。 >
  3. ImageButton_Click

  4. BindData() / li>
  1. Page_Init
  2. BindData() - Eventhandler for ImageButton attached.
  3. ImageButton_Click
  4. BindData()

这篇关于事件处理后如何更新页面数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆