·天新网首页·加入收藏·设为首页
首页|笔记本|手机|数码相机|摄像机|MP3/MP4|主板|内存|显示器|办公|打印机|下载|开发|汽车|学院|业界
硬件|台式机|数码|数字家庭|投影仪|GPS/CPU|显卡|硬盘|服务器|网络|一体机|驱动|源码|游戏|考试|报价
您现在的位置:天新网 > 软件开发 > .Net开发 > ASP.NET
在DataGrid中为Footer添加自定义内容
http://dev.21tx.com 2003年05月02日 【孟宪会之精彩世界】 孟宪会

首先:创建为Page_Load事件编写数据绑定的代码:

<%@ Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <p> <script runat="server"> Sub Page_Load(Sender As Object, E As EventArgs) Dim myConnectionString As String = "Data Source=.;Initial Catalog=NorthWind;User Id=sa;Password=;" Dim myConnection As SqlConnection = new SqlConnection(myConnectionString) Dim myCommand As SqlCommand = New SqlCommand("select * from Categories", myConnection) Dim myDataReader As SqlDataReader Try myConnection.Open() myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) my<a href="http://dev.21tx.com/dotnet/aspnet/datagrid/" target="_blank">DataGrid</a>.DataSource = myDataReader myDataGrid.DataBind() Catch myException As Exception Response.Write("数据错误:" & myException.ToString()) Finally If Not myDataReader Is Nothing Then myDataReader.Close() End If End Try End Sub

其次:创建OnItemDataBound事件,在OnItemDataBound事件中,我们可以对DataGrid中每行进行数据绑定时进行检测。这里我们只添加Footer部分的内容,因此,我们只检测DataGrid中的Footer部分。下面是 DataGrid中几种ItemTypes类型。

Item Type Description
Header DataGrid控件的Heading部分
Footer DataGrid控件的Footer部分
Item DataGrid控件中每个条目
AlternatingItem DataGrid控件的alternating条目
SelectedItem DataGrid控件的selected条目
EditItem DataGrid控件的可编辑条目
Separator DataGrid控件每个条目之间的分割部分
Pager DataGrid控件的page selection部分

最后:一旦我们检测到当前是Footer部分,就可以添加我们的动态内容。这里我在第二列添加一个链接。

Public Sub myDataGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs) '只有类型为footer的时候进行执行 If(e.Item.ItemType = ListItemType.Footer ) Dim myHy<a href="http://dev.21tx.com/web/perl/" target="_blank">Perl</a>ink As HyperLink = new HyperLink() If Not Request.QueryString("id") = Nothing Then myHyperLink.Text = "添加内容" myHyperLink.NavigateURL = "adddetail.<a href="http://dev.21tx.com/web/asp/" target="_blank">ASP</a>x?id=" & Request.QueryString("id") Else myHyperLink.Text = "没有添加内容" End If 'Cells从0开始 e.Item.Cells(1).Controls.Add(myHyperLink) End If End Sub </script> 下面是aspx页面部分: <html> <head> </head> <body> <form runat="server"> <asp:datagrid id="myDataGrid" runat="server" showfooter="true" onitemdatabound="myDataGrid_ItemDataBound" enableviewstate="false"> </asp:datagrid> </form> </body> </html>

上一篇: 在ASP.NET中动态创建柱状图和饼图
下一篇: 抓取网页中的链接

Google
 
热点文章
关于我们 | 联系我们 | 广告服务 | 工作机会 | 版权声明 | 欢迎投稿 | 网站地图
Copyright © 2000-2008 , www.21tx.com , All Rights Reserved .
晨新科技 版权所有 Created by TXSite.net