web前端

位置:IT落伍者 >> web前端 >> 浏览文章

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


发布日期:2022年03月21日
 
DataGrid Web控件深度历险(3) part3

在本文第二部分我们研究了如何通过ButtonColumn标记在DataGrid中显示按钮此外我们考察了如何将事件处理程序与按钮的点击联系起来下面我们将了解到如何判断DataGrid中哪一行的按钮被点击并且基于这些信息执行相应的动作

判断哪一行的按钮被点击

回想一下点击按钮的事件处理程序定义如下

Sub eventHandlerName(sender as Object e as DataGridCommandEventArgs)

End Sub

DataGridCommandEventArgs类包含一个Item属性该属性包含了触发该事件的源对象Item属性是TableRow类的一个实例它指向DataGrid中被点击的那一行可使用Cells属性访问TableRow类中的列例如有一个DataGrid它的列信息定义如下

<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 = eItemCells()

Dim FAQIDColumn as TableCell = eItemCells()

Dim DescColumn as TableCell = eItemCells()

Dim buttonColText as String = buttonColumnText

Dim FAQIDColText as String = FAQIDColumnText

Dim DescColText as String = DescColumnText

End Sub

示例运行结果如下:

更新按钮事件处理程序后的DataGrid示例

本示例展示了一个包含Detail按钮的DataGrid Web控件并演示了当按下按钮时如何触发一段代码注意点击某个Detail按钮后你会看到被点击按钮所在行的信息

Value of Clicked Button Column Text:

Value of FAQID Column Text:

Value of Clicked Description Column Text: How can I format numbers and date/times using ASPNET? For example I want to format a number as a currency

FAQ Details

FAQ ID

FAQ Description

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)?

How can I format numbers and date/times using ASPNET? For example I want to format a number as a currency

源代码

<% @Import Namespace=SystemData %>

<% @Import Namespace=SystemDataSqlClient %>

<script language=vb runat=server>

Sub Page_Load(sender as Object e as EventArgs)

If Not PageIsPostBack then

BindData()

End If

End Sub

Sub BindData()

Create a connection

Dim myConnection as New SqlConnection(ConfigurationSettingsAppSettings(connectionString))

Create the command object passing in the SQL string

Const strSQL as String = sp_Popularity

Dim myCommand as New SqlCommand(strSQL myConnection)

Set the datagrids datasource to the datareader and databind

myConnectionOpen()

dgPopularFAQsDataSource = myCommandExecuteReader(CommandBehaviorCloseConnection)

dgPopularFAQsDataBind()

End Sub

Sub dispDetails(sender as Object e As DataGridCommandEventArgs)

Dim buttonColumn as TableCell = eItemCells()

Dim FAQIDColumn as TableCell = eItemCells()

Dim DescColumn as TableCell = eItemCells()

Dim buttonColText as String = buttonColumnText

Dim FAQIDColText as String = FAQIDColumnText

Dim DescColText as String = DescColumnText

lblBCTText = buttonColText

lblFCTText = FAQIDColText

lblDCTText = DescColText

End Sub

</script>

<form runat=server>

<b>Value of Clicked Button Column Text</b>:

<asp:label id=lblBCT runat=server /><br />

<b>Value of FAQID Column Text</b>:

<asp:label id=lblFCT runat=server /><br />

<b>Value of Clicked Description Column Text</b>:

<asp:label id=lblDCT runat=server /><br />

<asp:DataGrid runat=server id=dgPopularFAQs

BackColor=#eeeeee Width=%

HorizontalAlign=Center

FontName=Verdana CellPadding=

FontSize=pt AutoGenerateColumns=False

OnItemCommand=dispDetails>

<HeaderStyle BackColor=Black ForeColor=White FontBold=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>

请仔细检查上面的示例你可能注意到的第一件事就是按钮列不包含任何文本这是因为仅需通过HTML即可显示按钮因此TableCell的Text属性返回了一个空字符串

在本文开始部分我讲述了一个电子商务公司的场景该公司希望显示部分货运信息但同时提供显示所有货运信息的选择到目前为止的示例中我们仅显示了sp_Popularity存储过程返回列中的一小部分列想象一下我们仅希望显示最受欢迎的常见问题的描述列然后提供一个Detail按钮允许用户查看某个常见问题的其余信息

虽然我们不希望在DataGrid中显示FAQID列但是我们仍然需要为detialsClicked事件处理程序提供该信息因为它数据库中表的关键字并唯一标识了每个常见问题通过对DataGrid标记进行小小的改动(在与数据库中FAQID列对应的BoundColumn标记中增加Visible= False)我们仍然能够传递该信息此改动隐藏了FAQID列但仍然允许detailClicked事件处理程序访问某个常见问题的标识(通过eItemCells()Text)

因此我们所要做的就是改写detailsClicked事件处理程序以便当它被触发时获得用户希望显示的那个常见问题的信息然后再显示该常见问题的详细信息在阅读了一系列关于如何使用DataGrid的文章后当需要显示数据库中的数据时你的第一个想法应该就是使用DataGrid因此我们的页面看起来应该是这样:

<script language=vb runat=server>

Sub Page_Load(sender as Object e as EventArgs)

If Not PageIsPostBack 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>

示例运行结果如下:

本示例展示了如何在DataGrid的每一行中显示概要信息和一个Detail按钮当按钮被点击时对所选择的数据项显示其余信息

Category Name

FAQ Description

Views

Author

Authors Email

Date Added

Getting Started

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)?

Scott Mitchell

mitchel

FAQ Details

FAQ Description

上一篇:XMLHTTPRequest的属性和方法简介

下一篇:对XML文档进行添、删、改、查的程序