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


  点击按钮时让一些事情发生
  
  现在已将按钮添加到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那一行中的按钮被点击。这是一个非常重要的问题,并将在本文下一部分进行研究。
  
  
相关新闻
相关评论
 
评论表单加载中...
 
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号