BoundColumn 最主要的功能是利用LinkButton 或PushButton 来显示数据源中的一个字段内容,并且可以触发DataGrid Web 控件的事件。其使用语法如下所示:
<ASP:ButtonColumn
ButtonType="LinkButton | PushButton"
CommandName="命令名称"
DataTextField="DataSourceField"
DataTextFormatString="FormatString"
FooterText="FooterText"
HeaderImageUrl="url"
HeaderText="HeaderText"
ReadOnly="True | False"
SortField="DataSourceFieldToSortBy"
Text="ButtonCaption"
Visible="True | False"
/>
<%@Import Namespace=System.Data.ADO%>
<%@Import Namespace=System.Data%>
<!--#Include File="GetTable.inc"-->
<Html>
<Form Runat="Server">
<ASP:DataGrid Id="dgA" AllowPaging="True" PageSize="5"
OnPageIndexChanged="dgA_PageChg" Runat="Server"
PagerStyle-Mode="NumericPages" BorderColor="#808080"
HeaderStyle-Font-Names="Courier New"
HeaderStyle-BackColor="#D1DCEB"
HeaderStyle-Font-Bold="True" HeaderStyle-HorizontalAlign="Center"
AutoGenerateColumns="False"
OnItemCommand="dgA_ICmd">
<Property Name="Columns">
<ASP: ButtonColumn
HeaderText="姓名"
DataTextField="UserName"/>
<ASP:BoundColumn
HeaderText="电话"
DataField="UserTel"/>
<ASP:BoundColumn
HeaderText="住址"
DataField="UserAdd"/>
</Property>
</ASP:DataGrid>
</Form>
<ASP:Label Id="Label1" Runat="Server"/>
<Script Language="VB" Runat="Server">
Sub Page_Load(Sender As Object, e As EventArgs)
If Page.IsPostBack=False Then
Dim dtDataTable As DataTable=GetTable("CH08\MyWeb.mdb", "Members")
dgA.DataSource=dtDataTable.DefaultView
Page.DataBind()
End If
Label1.Text="您目前没有点选任何记录."
End Sub
Sub dgA_PageChg(Sender As Object, e As DataGridPageChangedEventArgs)
Dim dtDataTable As DataTable=GetTable("CH08\MyWeb.mdb", "Members")
dgA.DataSource=dtDataTable.DefaultView
Page.DataBind()
End Sub
Sub dgA_ICmd(Sender As Object, e As DataGridCommandEventArgs)
Dim shtRow As Short= e.Item.ItemIndex+1
If shtRow<>0 Then
Label1.Text="您点选了第" & shtRow.ToString() & _
" 个字段的数据, 这<br>笔数据在数据源中是第"
Label1.Text+=((dgA.CurrentPageIndex * dgA.PageSize) +
shtRow).ToString & _
"笔记录."
End If
End Sub
</SCRIPT>
</Html>
Sub dgA_ICmd(Sender As Object, e As DataGridCommandEventArgs)
Dim shtRow As Short= e.Item.ItemIndex+1
If shtRow<>0 Then
Label1.Text="您点选了第" & shtRow.ToString() & _
" 个字段的数据, 这<br>笔数据在数据源中是第"
Label1.Text+=((dgA.CurrentPageIndex * dgA.PageSize) +
shtRow).ToString & _
"笔记录."
End If
End Sub