下面我以DataGrid为便进行说明.
footer栏又称页脚栏,在很多时候我们可以在该栏放页码及相当的功能键.但是最后在获得这些控件引用的时候就会有点麻烦,由于footer(页脚),header(页眉)都不在DataGrid.Items属性中,该属性返回的集合只包含数据绑定生成的item,所以必须用其他方法来做.下面是我从csdn和其他地方总结而来的几种方法:
1.在ItemCreated或者ItemDataBound中对其控件进行查找;
if (e.Item.ItemType == ListItemType.Footer)
{
}
2.遍历DataGrid中的所有Item.DataGrid.Controls[0]一般来说为生成的Table.{
}
foreach (DataGridItem item in myGrid.Controls[0].Controls)
{
if(item.ItemType==ListItemType.Footer)
{
//用Item.FindControl查找相应的控件
}
}
{
if(item.ItemType==ListItemType.Footer)
{
//用Item.FindControl查找相应的控件
}
}
前面两种方法大家都经常用,这里再列出以前在csdn上看到的思归大哥提出的另外两种方法。
3.Table t=(Table)DataGrid.Controls[0];
//如果你有Pager的话,那么你需要这样编码T.Rows[T.Rows.Count-2]去获得Pager
DataGridItem item=(DataGridItem)t.Rows[t.Rows.Count-1];
//该item就是Footer,此时你就可以用FindControl查找了。
下面为一个完整的demo
前台页;
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchHeaderAndFooterControl.aspx.cs"
Inherits="SearchHeaderAndFooterControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>F</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid runat="server" ID="gvUserView" DataSourceID="ldsUserData" ShowFooter="True"
AllowCustomPaging="True" AllowPaging="True"
onitemdatabound="gvUserView_ItemDataBound">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:TextBox runat="server" ID="txtHeaderInfo" Text="this is header textbox value!"></asp:TextBox>
</HeaderTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<FooterTemplate>
<asp:TextBox runat="server" ID="txtFooterInfo" Text="this is footer textbox value!"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:LinqDataSource ID="ldsUserData" runat="server" ContextTypeName="DataClassesDataContext"
TableName="Customers">
</asp:LinqDataSource>
<br />
<asp:Button ID="btnGetFooterControlValue" runat="server" Text="获取footer控件" OnClick="btnGetFooterControlValue_Click" />
<asp:Button ID="btnGetHeaderControlValue" runat="server"
Text="获取header控件" onclick="btnGetHeaderControlValue_Click" />
</div>
</form>
</body>
</html>
Inherits="SearchHeaderAndFooterControl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>F</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataGrid runat="server" ID="gvUserView" DataSourceID="ldsUserData" ShowFooter="True"
AllowCustomPaging="True" AllowPaging="True"
onitemdatabound="gvUserView_ItemDataBound">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:TextBox runat="server" ID="txtHeaderInfo" Text="this is header textbox value!"></asp:TextBox>
</HeaderTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<FooterTemplate>
<asp:TextBox runat="server" ID="txtFooterInfo" Text="this is footer textbox value!"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:LinqDataSource ID="ldsUserData" runat="server" ContextTypeName="DataClassesDataContext"
TableName="Customers">
</asp:LinqDataSource>
<br />
<asp:Button ID="btnGetFooterControlValue" runat="server" Text="获取footer控件" OnClick="btnGetFooterControlValue_Click" />
<asp:Button ID="btnGetHeaderControlValue" runat="server"
Text="获取header控件" onclick="btnGetHeaderControlValue_Click" />
</div>
</form>
</body>
</html>
后台页;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class SearchHeaderAndFooterControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGetFooterControlValue_Click(object sender, EventArgs e)
{
#region 方法1
//foreach (DataGridItem item in gvUserView.Controls[0].Controls)
//{
// if (item.ItemType == ListItemType.Footer)
// {
// TextBox txtFooter = item.FindControl("txtFooterInfo") as TextBox;
// ShowMessage(txtFooter.Text);
// break;
// }
//}
#endregion
#region 方法2
//Table clientTable = gvUserView.Controls[0] as Table;
//DataGridItem item = (DataGridItem)clientTable.Rows[clientTable.Rows.Count - 2];
//TextBox txtFooter = item.FindControl("txtFooterInfo") as TextBox;
//ShowMessage(txtFooter.Text);
#endregion
}
private void ShowMessage(string str)
{
string notifyInfo = string.Format("alert('{0}');", str);
Page.ClientScript.RegisterClientScriptBlock(GetType(), "", notifyInfo, true);
}
protected void btnGetHeaderControlValue_Click(object sender, EventArgs e)
{
foreach (DataGridItem item in gvUserView.Controls[0].Controls)
{
if (item.ItemType == ListItemType.Header)
{
TextBox txtHeader = item.FindControl("txtHeaderInfo") as TextBox;
ShowMessage(txtHeader.Text);
break;
}
}
}
protected void gvUserView_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
TextBox txtHeader = e.Item.FindControl("txtHeaderInfo") as TextBox;
ShowMessage(txtHeader.Text);
}
//if (e.Item.ItemType == ListItemType.Footer)
//{
// TextBox txtFooter = e.Item.FindControl("txtFooterInfo") as TextBox;
// ShowMessage(txtFooter.Text);
//}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class SearchHeaderAndFooterControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGetFooterControlValue_Click(object sender, EventArgs e)
{
#region 方法1
//foreach (DataGridItem item in gvUserView.Controls[0].Controls)
//{
// if (item.ItemType == ListItemType.Footer)
// {
// TextBox txtFooter = item.FindControl("txtFooterInfo") as TextBox;
// ShowMessage(txtFooter.Text);
// break;
// }
//}
#endregion
#region 方法2
//Table clientTable = gvUserView.Controls[0] as Table;
//DataGridItem item = (DataGridItem)clientTable.Rows[clientTable.Rows.Count - 2];
//TextBox txtFooter = item.FindControl("txtFooterInfo") as TextBox;
//ShowMessage(txtFooter.Text);
#endregion
}
private void ShowMessage(string str)
{
string notifyInfo = string.Format("alert('{0}');", str);
Page.ClientScript.RegisterClientScriptBlock(GetType(), "", notifyInfo, true);
}
protected void btnGetHeaderControlValue_Click(object sender, EventArgs e)
{
foreach (DataGridItem item in gvUserView.Controls[0].Controls)
{
if (item.ItemType == ListItemType.Header)
{
TextBox txtHeader = item.FindControl("txtHeaderInfo") as TextBox;
ShowMessage(txtHeader.Text);
break;
}
}
}
protected void gvUserView_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
TextBox txtHeader = e.Item.FindControl("txtHeaderInfo") as TextBox;
ShowMessage(txtHeader.Text);
}
//if (e.Item.ItemType == ListItemType.Footer)
//{
// TextBox txtFooter = e.Item.FindControl("txtFooterInfo") as TextBox;
// ShowMessage(txtFooter.Text);
//}
}
}
如果控件为GridView,则进行简单变换即可使用
if (e.Row.RowType == DataControlRowType.Header)
{
TextBox txtHeader = e.Row.FindControl("txtHeaderInfo") as TextBox;
ShowMessage(txtHeader.Text);
}
//if (e.Row.RowType == DataControlRowType.Footer)
//{
// TextBox txtFooter = e.Item.FindControl("txtFooterInfo") as TextBox;
// ShowMessage(txtFooter.Text);
//}
{
TextBox txtHeader = e.Row.FindControl("txtHeaderInfo") as TextBox;
ShowMessage(txtHeader.Text);
}
//if (e.Row.RowType == DataControlRowType.Footer)
//{
// TextBox txtFooter = e.Item.FindControl("txtFooterInfo") as TextBox;
// ShowMessage(txtFooter.Text);
//}
DataList,Repeater也是同理
//if (e.Item.ItemType == ListItemType.Header)
//{
//}
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox txtFooter = e.Item.FindControl("txtFooterInfo") as TextBox;
//TextBox txtFooter = e.Item.Controls[1] as TextBox;
ShowMessage(txtFooter.Text);
}
//{
//}
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox txtFooter = e.Item.FindControl("txtFooterInfo") as TextBox;
//TextBox txtFooter = e.Item.Controls[1] as TextBox;
ShowMessage(txtFooter.Text);
}