在DataGrid中为Footer添加自定义内容
2006-05-08 17:05
337 查看
首先:创建为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>
<%@ 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添加自定义内容
- 在DataGrid中为Footer添加自定义内容
- 在DataGrid中为Footer添加自定义内容
- dede 自定义内容模型 不能添加文章
- Flex自定义在DataGrid中搜索内容
- MTStatusBarOverlay (状态栏,添加自定义内容库)
- 为DataGrid自定义分页添加自定义导航和分页信息
- DataGrid动态添加自定义模板列
- 自定义动态添加字段内容的库类
- 向日历控件中添加自定义内容
- 自定义个性cmd界面 轻松打造个性的cmd 修改cmd字体颜色 添加个性cmd内容(转)
- 为DataGrid自定义分页添加自定义导航和分页信息 (转)
- 单个图片、批量图片添加自定义水印内容小程序开发
- easyUI datagrid 行内容超过添加(...)
- 强大的DataGrid组件[10]_自定义脚模板(FooterTemplate)——Silverlight学习笔记[18]
- 为我们的UITableViewFooterHeaderView添加自定义View的布局
- 为DataGrid添加自定义DataGridColumn类的例子
- 关于DEDECMS自定义模型当中添加自定义字段后在后台添加内容后不显示解决方案
- easyUI datagrid 行内容超过添加(...)
- DataGrid 自定义字段内容