點擊按鈕時讓一些事情發生
現在已將按鈕添加到DataGrid中了,我們希望將服務器端的代碼與按鈕關聯起來,這樣當按鈕被點擊時可發生一些動作。在認識到DataGrid中的ButtonColumn按鈕被點擊時ItemCommand事件將被觸發后,那么我們就可為這個事件編寫服務器端的事件處理程序。這個事件處理程序必須定義如下:
Sub eventHandlerName(sender as Object, e as DataGridCommandEventArgs) ... End Sub
一旦在服務器端代碼塊(或代碼后置頁)中定義了此過程,你可通過在DataGrid的標記中增加OnItemComman屬性的方法將DataGrid的事件與該事件處理程序聯系起來,如下所示:
<asp:datagrid runat="server" ... OnItemCommand="eventHandlerName"> ... </asp:datagrid>
下面的代碼演示了當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> 示例運行結果如下:
包含事件處理程序的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. …
源代碼:
<% @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="10pt" AutoGenerateColumns="False" 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>
這里還有一件非常重要的事情需要注意:我們僅在第一次頁面訪問時執行數據庫查詢并對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那一行中的按鈕被點擊。這是一個非常重要的問題,并將在本文下一部分進行研究。
|