DataGrid Web控件深度历险(3) part2

** 点击按钮时让一些事情发生 **

现在已将按钮添加到 DataGrid 中了,我们希望将服务器端的代码与按钮关联起来,这样当按钮被点击时可发生一些动作。在认识到 DataGrid 中的 ButtonColumn 按钮被点击时 ItemCommand 事件将被触发后,那么我们就可为这个事件编写服务器端的事件处理程序。这个事件处理程序必须定义如下:

Sub _eventHandlerName_ ( _sender_ as Object, _e_ as DataGridCommandEventArgs)
...
End Sub

一旦在服务器端代码块 ( 或代码后置页 ) 中定义了此过程,你可通过在 DataGrid 的标记中增加 OnItemComman 属性的方法将 DataGrid 的事件与该事件处理程序联系起来,如下所示:

`

1<asp:datagrid **="" **onitemcommand=" _eventHandlerName_ " ...="" `="" runat="server"> `   
2` ...  `   
3` </asp:datagrid>

`

下面的代码演示了当 DataGrid 中某个按钮被按下时,事件处理程序如何运行:

 1<script language="vb" runat="server">  
 2    
 3      Sub Page_Load(sender as Object, e as EventArgs)  
 4    
 5        **If Not Page.IsPostBack then  
 6    
 7          BindData() 'Only bind the data on the first page load  
 8    
 9        End If**  
10    
11      End Sub  
12    
13        
14    
15        
16    
17      Sub BindData()  
18    
19        'Make a connection to the database  
20    
21        'Databind the DataReader results to the DataGrid.  
22    
23      End Sub  
24    
25      
26    
27      
28    
29      Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)  
30    
31        Response.Write("You clicked one of the details buttons!")  
32    
33      End Sub  
34    
35    </script>
1<form runat="server">  
2    
3      <asp:datagrid ...="" runat="server">  
4    
5        ...  
6    
7      </asp:datagrid>
8</form>

示例运行结果如下 :

** 包含事件处理程序的 DataGrid **

本示例显示一个包含 Detail 按钮的 DataGrid Web 控件并演示了当按下按钮时如何触发一段代码。点击某个 Detail 按钮,你将会在 ** Status ** 文本旁看到一个消息,他指出了你点击了这个按钮!

** Status: ** You clicked one of the details buttons!

** FAQ Details **

|

** FAQ ID **

|

** FAQ Description **

---|---|---

|

144

|

Where can I host my ASP Web site for free (similar to GeoCities or Tripod or any of the many other free Web site sites)?

|

181

|

How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.

|

|

** 源代码 : **

1 @Import Namespace="System.Data" 
1 @Import Namespace="System.Data.SqlClient" 
 1<script language="vb" runat="server">   
 2Sub Page_Load(sender as Object, e as EventArgs)   
 3If Not Page.IsPostBack then   
 4BindData()   
 5End If   
 6End Sub   
 7  
 8  
 9Sub BindData()   
10'1. Create a connection   
11Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))   
12  
13'2. Create the command object, passing in the SQL string   
14Const strSQL as String = "sp_Popularity"   
15Dim myCommand as New SqlCommand(strSQL, myConnection)   
16  
17'Set the datagrid's datasource to the datareader and databind   
18myConnection.Open()   
19dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)   
20dgPopularFAQs.DataBind()    
21End Sub   
22  
23  
24Sub dispDetails(sender as Object, e As DataGridCommandEventArgs)   
25lblOutput.Text = "You clicked one of the details buttons!"   
26End Sub   
27</script>
 1<form runat="server">
 2<b>Status:</b> <asp:label font-italic="True" id="lblOutput" runat="server"></asp:label>
 3<p>
 4<asp:datagrid autogeneratecolumns="False" backcolor="#eeeeee" cellpadding="4" font-name="Verdana" font-size="  10pt  " horizontalalign="Center" id="dgPopularFAQs" onitemcommand="dispDetails" runat="server" width="85%">
 5<headerstyle backcolor="Black" font-bold="True" forecolor="White" horizontalalign="Center"></headerstyle>
 6<alternatingitemstyle backcolor="White"></alternatingitemstyle>
 7<columns>
 8<asp:buttoncolumn buttontype="PushButton" commandname="details" headertext="FAQ Details" text="Details"></asp:buttoncolumn>
 9<asp:boundcolumn datafield="FAQID" headertext="FAQ ID"></asp:boundcolumn>
10<asp:boundcolumn datafield="Description" headertext="FAQ Description"></asp:boundcolumn>
11</columns>
12</asp:datagrid>
13</p></form>

这里还有一件非常重要的事情需要注意:我们仅在第一次页面访问时执行数据库查询并对 DataGrid 进行绑定。在 Page_Load 事件处理程序 ( 每次页面装载时被触发 ) 中,我们检查页面是否被回发 (posted back) 。如果没有,那么就知道是第一次访问这个页面。在此情况下我们通过数据库查询生成一个 DataReader, 将 DataGrid 的 DataSource 属性设为这个 DataReader, 并调用 DataGrid 的 DataBind() 方法。

当有人点击 DataGrid 中某个 Detail 按钮时 ,ASP.Net Web 页面将执行回发,页面又将在服务器端执行。 Page_Load 事件处理程序将再次被激活,但是这次因为我们在执行回发, BindData() 过程将 不会 被调用。此外 detailsClicked 事件处理程序将被执行。注意如果我们每次在页面装载时均将数据绑定至 DataGrid( 也就是说我们省略了 If Not Page.IsPostBack 检查 ) , detailsClicked 事件处理程序将不会执行,因为重新绑定 DataGrid 将会清空 (flush out)ItemCommand 事件。 ( 请重新阅读上面的两段文字 - 根据各位对 DataGrid 的了解,你们很有可能忘记执行回发检查并导致 DataGrid 不能触发针对按钮的事件处理代码。相信我,这件事在我身上多次发生! J )

虽然本例分析了如何将事件处理与按钮的点击联系起来,我们仍然需要知道如何判断 DataGrid 那一行中的按钮被点击。这是一个非常重要的问题,并将在本文下一部分进行研究。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus