教程中国
PHOTOSHOP CS9.0中文版 MAYA 8.5 FOR WINDOWS Corel Painter v9.0 Flash MX2004 中文版 Illustrator cs2 中文版
VC++6.0含sp6 中英文版 VB6.0 +sp6 简体中文版 Borland Delphi 7汉化版 MSDN for vb6.0中文版 Visual Studio 2005简体
教程中国下属 文件存储共享专家BIBIDU.COM 提供大型软件,教材,源码,电影,音乐,图书等下载 更多精品请点此进入
  您目前所在位置: 教程中国 >> .NET类 >> ASP.NET >> DataGrid Web控件深度历险(3) part2 RSS订阅
DataGrid Web控件深度历险(3) part2
教程(视频,书籍)下载:  ASP.NET AutoCAD 数据库 C# ASP java photoshop 网页设计 delphi 3dmax Flash C++ VB 张孝祥 实例   更多请进入BIBIDU搜索
IT搜索引擎   
点击按钮时让一些事情发生

现在已将按钮添加到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那一行中的按钮被点击。这是一个非常重要的问题,并将在本文下一部分进行研究。



来源:upschool.com.cn
作者:
关键字:DataGrid,Web控件深度历险,part2
发表日期:2005-12-23

网页显示有限 阅读全文请下载本文完整版WORD文档

上一篇:DataGrid Web控件深度历险(3) part1   下一篇:怎样使用DataGrid控件


本文的相类似文章
  • 两个简单方法加速DataGridView
  • 显示和隐藏 DataGrid中的列
  • 格式化DataGrid的例子【将数据源中的0,1值或者逻辑值转换成实际的文字】 2
  • 把Excel文件中的数据读入到 DataGrid中
  • ASP.NET组件DataGrid的分页实用方法
  • 利用DataGrid显示某目录下的所有文件
  • ASP.NET中DataGrid控件应用技巧简述(1)
  • ASP.NET中DataGrid控件应用技巧简述2
  • ASP.NET中DataGrid控件应用技巧简述3
  • ASP.NET中Web DataGrid的使用指南
  • 网友评论 查看本文全部评论
    笔 名: *
    评 论:
    最多500字。当前字数:0
    联系方式:
    验证码:
    在学习中进步 在进步中成长 教程中国相随您的成长之路
    华腾联合科技股份有限公司版权所有
    广告联系:Rosibo@163.com