C# 如何访问 GridView 的 HeaderTemplate 中的控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/615950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access a control in the HeaderTemplate of my GridView
提问by minty
I want to have a DropDownList in the header of my GridView. In My codebehind I can't seem to access it. Here is the HeaderTemplate:
我想在我的 GridView 的标题中有一个 DropDownList。在我的代码隐藏中,我似乎无法访问它。这是标题模板:
<asp:TemplateField SortExpression="EXCEPTION_TYPE">
<HeaderTemplate>
<asp:Label ID="TypeId" runat="server" Text="Type" ></asp:Label>
<asp:DropDownList ID="TypeFilter" runat="server" AutoPostBack="true">
</asp:DropDownList>
</HeaderTemplate>
...
</asp:TemplateField>
And here is the section in the code behind where I am trying to access the control 'TypeFilter'.
这是我试图访问控件“TypeFilter”的代码后面的部分。
protected void ObjectDataSource1_Selected(object sender,
ObjectDataSourceStatusEventArgs e)
{
DataTable dt = (DataTable)e.ReturnValue;
int NumberOfRows = dt.Rows.Count;
TotalCount.Text = NumberOfRows.ToString();
DataView dv = new DataView(dt);
DataTable types = dv.ToTable(true, new string[] { "EXCEPTION_TYPE" });
DropDownList typeFilter = (DropDownList)GridView1.FindControl("TypeFilter");
typeFilter.DataSource = types;
typeFilter.DataBind();
}
You will notice that I am trying to use FindControl to get a reference to the DropDownList Control. This call returns null instead of returning the control. How do I get access to the control?
您会注意到我正在尝试使用 FindControl 来获取对 DropDownList 控件的引用。此调用返回 null 而不是返回控件。如何访问控件?
回答by iZ.
With Repeaters, you access headerTemplate items by using FindControl in the OnItemDataBoundEvent like this:
使用中继器,您可以通过在 OnItemDataBoundEvent 中使用 FindControl 来访问 headerTemplate 项目,如下所示:
RepeaterItem item = (RepeaterItem)e.Item;
if (item.ItemType == ListItemType.Header) {
item.FindControl("control"); //goes here
}
Does this work for GridViews as well?
这也适用于 GridViews 吗?
回答by sathish kumar sandupatla
protected void ObjectDataSource1__RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
DropDownList typeFilter = (DropDownList)GridView1.FindControl("TypeFilter");
}
}
回答by Mubashir Ahmed
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
DropDownList ddlLocation = (DropDownList)e.Row.FindControl("ddlLocation");
ddlLocation.DataSource = dtLocation;
ddlLocation.DataBind();
}
}
}
回答by user2077087
private void GetDropDownListControl()
{
DropDownList TypeFilter = ((DropDownList)this.yorGridView.HeaderRow.FindControl("TypeFilter"));
}
回答by user2366118
Try this to find a control in the HeaderTemplate without a row-data-bind, if that's what is needed:
如果需要,请尝试在 HeaderTemplate 中找到一个没有行数据绑定的控件:
private void Lab_1_GV1_Populate_SearchText()
{
GridView GV1 = (GridView)FindControl("Lab_1_GV1");
TextBox TXB1 = (TextBox)GV1.HeaderRow.FindControl("Lab_1_TX2GV1");
}
Thanks
谢谢
Ruchir
鲁奇尔