`

考察DataGrid控件: Part 3 (下)

阅读更多


本文英文原版:

http://aspnet.4guysfromrolla.com/articles/042402-1.2.aspx

考察DataGrid控件: Part 3 (下)


导言:

在上半部分,我们概述了如何通过使用ButtonColumn标签为DataGrid控件添加按钮.此外,我们创建了相关的事件处理器以因对用户点击按钮的情况.在本节,我们探讨如何确定点击的按钮以及怎样采取对应的行为.


确定点击的是哪个按钮

记得前面那个应对“点击按钮”的事件处理器是这样定义的:

Sub eventHandlerName(sender as Object, e as DataGridCommandEventArgs)
...
End Sub

其中DataGridCommandEventArgs class类包含了一个Item属性,其返回的item包含了触发该事件的对象源(the source of the object).实际上该item就是TableRow class类的一个实例,对应于DataGrid控件里被点击的那个行记录.我们可以通过Cells属性来访问TableRow class类的列,获取其值.也就是说,假设我们有一个DataGrid,它的Columns collection是这样定义的:

<asp:DataGrid runat="server" ... >
<Columns>
<asp:ButtonColumn Text="Details" HeaderText="FAQ Details" CommandName="details" />
<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" />
<asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
</Columns>
</asp:datagrid>


然后,在事件处理器里,我们可以引用那些被点击的行的值,像下面这样:

Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)
Dim buttonColumn as TableCell = e.Item.Cells(0)
Dim FAQIDColumn as TableCell = e.Item.Cells(1)
Dim DescColumn as TableCell = e.Item.Cells(2)

Dim buttonColText as String = buttonColumn.Text
Dim FAQIDColText as String = FAQIDColumn.Text
Dim DescColText as String = DescColumn.Text
End Sub


演示页面:http://aspnet.4guysfromrolla.com/demos/dgExample7.aspx


记得前面我们假设过这种情况,某电子商务公司希望在显示货运信息时只列出部分信息,但同时允许顾客查看某笔交易的全部信息.但是到目前为止,我们的演示页面只列出了存储过程sp_Popularity返回的部分列,假设我们只想列出Description列,并提供一个名为“Details”的按钮供用户查看剩余的信息.


虽然我们不想将FAQID列显示在DataGrid里,但我们仍然需要找到一个途径为detailsClicked事件处理器传递FAQID,因为FAQID是表的主键,即每个FAQ的唯一标识.为达此目的,我们需要在DataGrid标签里做小小的改动.找到与FAQID列对应的BoundColumn 标签,我们只需要添加代码:Visible="False",这将隐藏该列,但通过e.Item.Cells(1).Text的形式,detailsClicked事件处理器仍然可以访问到适当的FAQID值.

我们需要做的是更新detailsClicked事件处理器,以获取并展示用户希望得到某条FAQ的详细信息,页面看起来应该像下面这个样子:

<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 gPopularFAQs DataGrid.
End Sub


Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)
'Get detailed information about the selected FAQ and bind
'the database results to the dgFAQDetails DataGrid
End Sub
</script>

<form runat="server">
<asp:DataGrid runat="server" id="dgFAQDetails" ... >
...
</asp:datagrid>

<asp:DataGrid runat="server" id="dgPopularFAQs" ... >
<Columns>
<asp:ButtonColumn Text="Details" HeaderText="FAQ Details"
ButtonType="PushButton" />
<asp:BoundColumn DataField="FAQID" Visible="False" />
<asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
</Columns>
</asp:datagrid>
</form>


演示页面:http://aspnet.4guysfromrolla.com/demos/dgExample8.aspx


要注意的第一件事情是页面包含2个DataGrid控件,第一个名为dgFAQDetails,用于显示某个特定FAQ的详细信息;第二个名为dgPopularFAQs,用于显示最常见的10个问题.我们注意到在dgPopularFAQs里,我们对FAQID BoundColumn添加了:Visible="False",结果就是其不会显示在dgPopularFAQs的界面里.


结语:
本节我们考察了为每行添加一个按钮,并创建对应的事件处理器,在以后的部分我们还将探讨排序、分页、编辑数据等功能。

祝编程快乐!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics