当前位置:编程学习 > C#/ASP.NET >>

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

答案:点击按钮时让一些事情发生

现在已将按钮添加到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那一行中的按钮被点击。这是一个非常重要的问题,并将在本文下一部分进行研究。

上一个:DataGrid Web控件深度历险(3) part3
下一个:DataGrid Web控件深度历险(3) part1

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,