在DataGrid中为Footer添加自定义内容

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 littlehb 的 blog

在datagrid中为footer添加自定义内容
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2003年5月2日 1点1分26秒

首先:创建为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) mydatagrid.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 myhyperlink as hyperlink = new hyperlink() if not request.querystring("id") = nothing then myhyperlink.text = "添加内容" myhyperlink.navigateurl = "adddetail.aspx?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>

本文关键:在DataGrid中为Footer添加自定义内容
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top