點擊按鈕時讓一些事情發(fā)生
現(xiàn)在已將按鈕添加到DataGrid中了,我們希望將服務(wù)器端的代碼與按鈕關(guān)聯(lián)起來,這樣當(dāng)按鈕被點擊時可發(fā)生一些動作。在認(rèn)識到DataGrid中的ButtonColumn按鈕被點擊時ItemCommand事件將被觸發(fā)后,那么我們就可為這個事件編寫服務(wù)器端的事件處理程序。這個事件處理程序必須定義如下:
Sub eventHandlerName(sender as Object, e as DataGridCommandEventArgs)
...
End Sub
一旦在服務(wù)器端代碼塊(或代碼后置頁)中定義了此過程,你可通過在DataGrid的標(biāo)記中增加OnItemComman屬性的方法將DataGrid的事件與該事件處理程序聯(lián)系起來,如下所示:
<asp:datagrid runat="server"
...
OnItemCommand="eventHandlerName">
...
</asp:datagrid>
下面的代碼演示了當(dāng)DataGrid中某個按鈕被按下時,事件處理程序如何運行:
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData() 'Only bind the data on the first page load
End If
End Sub
Sub BindData()
'Make a connection to the database
'Databind the DataReader results to the DataGrid.
End Sub
Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)
Response.Write("You clicked one of the details buttons!")
End Sub
</script>
<form runat="server">
<asp:DataGrid runat="server" ... >
...
</asp:datagrid>
</form>
示例運行結(jié)果如下:
包含事件處理程序的DataGrid
本示例顯示一個包含Detail按鈕的DataGrid Web控件并演示了當(dāng)按下按鈕時如何觸發(fā)一段代碼。點擊某個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. |
… | | |
源代碼:
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData()
End If
End Sub
Sub BindData()
'1. Create a connection
Dim myConnection as New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
'2. Create the command object, passing in the SQL string
Const strSQL as String = "sp_Popularity"
Dim myCommand as New SqlCommand(strSQL, myConnection)
'Set the datagrid's datasource to the datareader and databind
myConnection.Open()
dgPopularFAQs.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dgPopularFAQs.DataBind()
End Sub
Sub dispDetails(sender as Object, e As DataGridCommandEventArgs)
lblOutput.Text = "You clicked one of the details buttons!"
End Sub
</script>
<form runat="server">
<b>Status:</b> <asp:label id="lblOutput" runat="server" Font-Italic="True" />
<p>
<asp:DataGrid runat="server" id="dgPopularFAQs"
BackColor="#eeeeee" Width="85%"
HorizontalAlign="Center"
Font-Name="Verdana" CellPadding="4"
Font-Size="
OnItemCommand="dispDetails">
<HeaderStyle BackColor="Black" ForeColor="White" Font-Bold="True" HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" />
<Columns>
<asp:ButtonColumn Text="Details" HeaderText="FAQ Details" CommandName="details" ButtonType="PushButton" />
<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" />
<asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
</Columns>
</asp:datagrid>
</form>
這里還有一件非常重要的事情需要注意:我們僅在第一次頁面訪問時執(zhí)行數(shù)據(jù)庫查詢并對DataGrid進行綁定。在Page_Load事件處理程序(每次頁面裝載時被觸發(fā))中,我們檢查頁面是否被回發(fā)(posted back)。如果沒有,那么就知道是第一次訪問這個頁面。在此情況下我們通過數(shù)據(jù)庫查詢生成一個DataReader,將DataGrid的DataSource屬性設(shè)為這個DataReader,并調(diào)用DataGrid的DataBind()方法。
當(dāng)有人點擊DataGrid中某個Detail按鈕時,ASP.Net Web頁面將執(zhí)行回發(fā),頁面又將在服務(wù)器端執(zhí)行。Page_Load事件處理程序?qū)⒃俅伪患せ?,但是這次因為我們在執(zhí)行回發(fā),BindData()過程將不會被調(diào)用。此外detailsClicked事件處理程序?qū)⒈粓?zhí)行。注意如果我們每次在頁面裝載時均將數(shù)據(jù)綁定至DataGrid(也就是說我們省略了If Not Page.IsPostBack檢查),detailsClicked事件處理程序?qū)⒉粫?zhí)行,因為重新綁定DataGrid將會清空(flush out)ItemCommand事件。(請重新閱讀上面的兩段文字-根據(jù)各位對DataGrid的了解,你們很有可能忘記執(zhí)行回發(fā)檢查并導(dǎo)致DataGrid不能觸發(fā)針對按鈕的事件處理代碼。相信我,這件事在我身上多次發(fā)生!J)
雖然本例分析了如何將事件處理與按鈕的點擊聯(lián)系起來,我們?nèi)匀恍枰廊绾闻袛?/font>DataGrid那一行中的按鈕被點擊。這是一個非常重要的問題,并將在本文下一部分進行研 究。