人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

DataGrid Web控件深度歷險(3) part2

DataGrid Web控件深度歷險(3) part2

更新時間:2022-06-06 文章作者:未知 信息來源:網絡 閱讀次數:

點擊按鈕時讓一些事情發生

現在已將按鈕添加到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那一行中的按鈕被點擊。這是一個非常重要的問題,并將在本文下一部分進行研究。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 蓬安县| 安阳县| 文登市| 乌苏市| 阿拉尔市| 齐齐哈尔市| 绥芬河市| 莱阳市| 阿坝县| 凤凰县| 丹棱县| 关岭| 什邡市| 惠水县| 民权县| 葫芦岛市| 海门市| 广平县| 神农架林区| 青浦区| 阿克陶县| 黎川县| 龙州县| 酉阳| 石楼县| 上饶县| 延寿县| 茂名市| 峨眉山市| 晋江市| 平和县| 凌海市| 吉水县| 阜南县| 华安县| 合水县| 安宁市| 临猗县| 襄垣县| 绥德县| 南丹县|