首页 >> VC.Net >> 正文
DataGrid Web控件深度历险(3) part3-VC.NET
来源:Dotnet频道 作者:采集 时间:2008-3-31


  在本文第二部分我们研究了如何通过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 = 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
  
  示例运行结果如下:
  
  更新按钮事件处理程序后的DataGrid示例
  
  本示例展示了一个包含Detail按钮的DataGrid Web控件并演示了当按下按钮时如何触发一段代码。注意点击某个Detail按钮后你会看到被点击按钮所在行的信息。
  
  Value of Clicked Button Column Text:
  Value of FAQID Column Text: 181
  Value of Clicked Description Column Text: How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency.
  
  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)
  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
  
  lblBCT.Text = buttonColText
  lblFCT.Text = FAQIDColText
  lblDCT.Text = 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="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>
  
  请仔细检查上面的示例。你可能注意到的第一件事就是按钮列不包含任何文本。这是因为仅需通过HTML即可显示按钮,因此TableCell的Text属性返回了一个空字符串。
  
  在本文开始部分我讲述了一个电子商务公司的场景,该公司希望显示部分货运信息,但同时提供显示所有货运信息的选择。到目前为止的示例中,我们仅显示了sp_Popularity存储过程返回列中的一小部分列。想象一下我们仅希望显示最受欢迎的常见问题的描述列,然后提供一个Detail按钮允许用户查看某个常见问题的其余信息。
  
  虽然我们不希望在DataGrid中显示FAQID列,但是我们仍然需要为detialsClicked事件处理程序提供该信息,因为它数据库中表的关键字并唯一标识了每个常见问题。通过对DataGrid标记进行小小的改动(在与数据库中FAQID列对应的BoundColumn标记中增加Visible= "False"),我们仍然能够传递该信息。此改动隐藏了FAQID列,但仍然允许detailClicked事件处理程序访问某个常见问题的标识(通过e.Item.Cells(1).Text)。
  
  因此我们所要做的就是改写detailsClicked事件处理程序,以便当它被触发时获得用户希望显示的那个常见问题的信息,然后再显示该常见问题的详细信息。在阅读了一系列关于如何使用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 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
  Author's 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)?
  163,148
  Scott Mitchell
  mitchell@4guysfromrolla.com
  03-20-2001
  
  FAQ Details
  FAQ Description
相关新闻
相关评论
 
评论表单加载中...
 
VC.Net文章

 在Visual C++应

 编辑:admin

 时间:2008-3-10


   .NET Framework 中多语言支持的实现-.NET Framework
   将Eiffel系统集成到.NET Framework中-.NET Framework
   04年五种常用RAD的测验比较(1)-.NET Framework
   04年五种常用RAD的测验比较(2)-.NET Framework
   04年五种常用RAD的测验比较(3)-.NET Framework
最新文章
   .NET Framework 中多语言支持的实现-.NET Framework
   将Eiffel系统集成到.NET Framework中-.NET Framework
   04年五种常用RAD的测验比较(1)-.NET Framework
   04年五种常用RAD的测验比较(2)-.NET Framework
   04年五种常用RAD的测验比较(3)-.NET Framework
总站搜索
搜索
 
热门文章
   oracle数据库文件中的导入\导出
   用Oracle10g列值掩码技术隐藏敏感数据
   VB程序中用ADO对象动态创建数据库和表-VB.NET
   用VB6写简单程序 让电骡自动关机-VB.NET
   使用.NET2.0编写COM组件供VB调用-VB.NET
   VB.NET:键盘控制焦点移动-VB.NET
   用VB.NET绘制GDI图形-VB.NET
   vb.net中应用 ArrayList 实例-VB.NET
 
推荐文章
ASP.NET中的状态管理-ASP.NET
VC、IE、ASP环境下打印、预备的完美解决方案
oracle数据库文件中的导入\导出
VB.NET中快速访问注册表技巧-VB.NET
在vb中实现超连接的方法!和直接发邮件-VB.NET
用VB做realplayer播放列表-VB.NET
在VB.NET中如何实现和利用SortedLists-VB.NET
利用VB.NET Stopwatch对象记录时间-VB.NET
成都古羌科技有限公司版权所有: Copyright@2007-2010 ,ALL Rights Reserved 蜀ICP备07017240号