點擊按鈕時讓一些事情發生
現在已將按鈕添加到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那一行中的按鈕被點擊。這是一個非常重要的問題,并將在本文下一部分進行研究。
新聞熱點
疑難解答