简体   繁体   English

如何访问 GridView 的 HeaderTemplate 中的控件

[英]How do I access a control in the HeaderTemplate of my GridView

I want to have a DropDownList in the header of my GridView.我想在我的 GridView 的 header 中有一个 DropDownList。 In My codebehind I can't seem to access it.在我的代码隐藏中,我似乎无法访问它。 Here is the HeaderTemplate:这是标头模板:

<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.您会注意到我正在尝试使用 FindControl 来获取对 DropDownList 控件的引用。 This call returns null instead of returning the control.此调用返回 null 而不是返回控件。 How do I get access to the control?如何访问控件?

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 吗?

private void GetDropDownListControl()
    {
        DropDownList TypeFilter = ((DropDownList)this.yorGridView.HeaderRow.FindControl("TypeFilter"));
    }
protected void ObjectDataSource1__RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
             DropDownList typeFilter = (DropDownList)GridView1.FindControl("TypeFilter");
        }
     }
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();
}
}
}

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鲁契尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在GridViewColumn HeaderTemplate内访问我的ComboBoxes? - How do I access my ComboBoxes inside of a GridViewColumn HeaderTemplate? 如何将代码隐藏值传递给Gridview UI的HeaderTemplate标签控件 - How to pass codebehind value to HeaderTemplate label control of Gridview UI 如何将数据表标题列字段设置为Gridview HeaderTemplate标签控件文本 - How to set datatable header column fields to a Gridview HeaderTemplate Label control Text 如何在视图模型的Calendar控件中访问选定的日期? - How do I access selected dates in a Calendar control in my viewmodel? 如何从其他方法访问控件 - How do I access my control from another method 如何为HeaderTemplate的标签控件设置Text值 - How to set a Text value for a label control of HeaderTemplate 如何从页面上的控件访问页面上的 GridView? - How can I access a GridView on a page from a control on a Page? 如何在asp.net的gridview中的各列中的HeaderTemplate中设置标签的对齐方式? - how to set alignment of label in HeaderTemplate in individual column in gridview in asp .net? 如何将数据绑定到嵌套的DevExpress GridView控件? - How do I Bind data to a nested DevExpress GridView control? 如何在DotNetNuke中将Gridview控件导出到Excel? - How do I export a Gridview Control to Excel in DotNetNuke?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM