首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
wangchinaking (57)
yjz0065 (113)
coofucoo (105)
Drate (69)
lphpc (30)
smallnest (61)
iiprogram (64)
downmoon (32)
danny_xcz (49)
btbtd (81)
CSDN - 文档中心 - .NET 阅读:8336   评论: 4    参与评论
标题   DataGrid Web控件深度历险(3) part2     选择自 chinapro 的 Blog
关键字   DataGrid
出处   http://aspnet.4guysfromrolla.com/articles/042402-1.aspx

点击按钮时让一些事情发生

现在已将按钮添加到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,DataGridDataSource属性设为这个DataReader,并调用DataGridDataBind()方法。

当有人点击DataGrid中某个Detail按钮时,ASP.Net Web页面将执行回发,页面又将在服务器端执行。Page_Load事件处理程序将再次被激活,但是这次因为我们在执行回发,BindData()过程将不会被调用。此外detailsClicked事件处理程序将被执行。注意如果我们每次在页面装载时均将数据绑定至DataGrid(也就是说我们省略了If Not Page.IsPostBack检查)detailsClicked事件处理程序将不会执行,因为重新绑定DataGrid将会清空(flush out)ItemCommand事件。(请重新阅读上面的两段文字-根据各位对DataGrid的了解,你们很有可能忘记执行回发检查并导致DataGrid不能触发针对按钮的事件处理代码。相信我,这件事在我身上多次发生!J)

虽然本例分析了如何将事件处理与按钮的点击联系起来,我们仍然需要知道如何判断DataGrid那一行中的按钮被点击。这是一个非常重要的问题,并将在本文下一部分进行研究。


相关文章
对该文的评论
fantasylu ( 2004-04-04)
应该叫做 学用DataGrid 会好一点
maxeat ( 2004-04-02)
不能说深度历险,讲得太简单啦。
banping ( 2004-04-02)
期待,慢慢看吧
PPLUNCLE ( 2004-04-01)
恩..我是初学者....顶.....