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

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

问题描述

在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事件创建和SortOrder的是当的ImageButton命令触发事件上以后更新。什么是用于与正确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.


  • 我应该落实在更新表中的数据,而无需重新创建它的方法?

  • 我应该重新加载$ ​​C $ c中的页面事件已经解雇后?

什么是你的$ P $解决这个问题的方法pferred?

What's your preferred way for solving this problem?

推荐答案

页的事件,如初始化加载引发回发的事件处理程序之前,将一直触发。这是页面生命周期的基础上(对于由彼得·布朗伯格视觉重新presentation ,看到这里)。大多数新的ASP.NET开发人员有重大问题的理解和妥善处理这种窘境。

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

做这个理想的方式是:

一个。你的 Page_Init 应该调用过程(我们称之为 BindData()作说明)处理该表的创建基础对数据库中的数据。这种方法将类似于结合所述数据库中的数据并呈现的该绑定的基础上的用户界面元素的结合的方法。 IOW,您应该删除从 Page_Init 方法表创建code,把它放在一个单独的方法,以便它可以在需要的时候被调用。

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.

乙。当你的 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()

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

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

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