findcontrol footertemplate in datagrid asp

时间:2015-09-05 05:09:51

标签: c# asp.net datagrid

我想在DataGrid Asp中找到Footertemplate的控件。但它返回null。你能帮帮我吗?

<asp:DataGrid ID="dtgDSSP" runat="server" AutoGenerateColumns="false" OnItemDataBound="dtgDSSP_ItemDataBound"
            ShowFooter="true" onselectedindexchanged="dtgDSSP_SelectedIndexChanged">
            <Columns>
 <asp:TemplateColumn HeaderText="Sản Phẩm">
                    <ItemTemplate>
                        <asp:HiddenField ID="HidIDSP" runat="server" />
                        <asp:DropDownList ID="dropSanPham" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="dropSPAdd" runat="server">
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateColumn>

代码背后:

protected void dtgDSSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dropAdd;

int footerIndex = dtgDSSP.Controls[0].Controls.Count - 1; dropAdd = dtgDSSP.Controls[0].Controls[footerIndex].FindControl("dropSPAdd") as DropDownList; if (dropAdd != null) { dropAdd.DataSource = Constant.dictSanPham; dropAdd.DataValueField = "key"; dropAdd.DataTextField = "value"; dropAdd.DataBind(); } }

1 个答案:

答案 0 :(得分:0)

使用以下syntex获取页脚dropSPAdd控件:

dtgDSSP.Controls[0].Controls[dtgDSSP.Controls[0].Controls.Count - 1].Controls[0].FindControl("dropSPAdd") as DropDownList

请检查以下代码

Html: -

        <asp:DataGrid ID="dtgDSSP" runat="server" AutoGenerateColumns="false" OnItemDataBound="dtgDSSP_ItemDataBound"
            ShowFooter="true" OnSelectedIndexChanged="dtgDSSP_SelectedIndexChanged">
            <Columns>
                <asp:TemplateColumn HeaderText="Sản Phẩm">
                    <ItemTemplate>
                        <asp:HiddenField ID="HidIDSP" runat="server" />
                        <asp:DropDownList ID="dropSanPham" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:DropDownList ID="dropSPAdd" runat="server">
                        </asp:DropDownList>
                    </FooterTemplate>
                </asp:TemplateColumn>
                 <asp:ButtonColumn Text="Select" CommandName="Select"></asp:ButtonColumn>
            </Columns>
        </asp:DataGrid>

代码背后: -

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
        System.Data.DataTable dtobj = new System.Data.DataTable();
        dtobj.Columns.Add("Test");
        dtobj.Rows.Add();
        dtobj.Rows[dtobj.Rows.Count - 1]["Test"] = "Testimg";
        dtobj.Rows.Add();
        dtobj.Rows[dtobj.Rows.Count - 1]["Test"] = "Testimg111";
        dtgDSSP.DataSource = dtobj;
        dtgDSSP.DataBind();
      }
    }

    protected void dtgDSSP_SelectedIndexChanged(object sender, EventArgs e)
    {
        var dropAdd = dtgDSSP.Controls[dtgDSSP.Controls.Count - 1].Controls[dtgDSSP.Controls[0].Controls.Count - 1].Controls[0].FindControl("dropSPAdd") as DropDownList;
        if (dropAdd != null)
        {
            dropAdd.DataSource = Constant.dictSanPham;
            dropAdd.DataValueField = "key";
            dropAdd.DataTextField = "value";
            dropAdd.DataBind();
        }

    }

    protected void dtgDSSP_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType  == ListItemType.Footer)
        {
            var dropAdd = e.Item.Controls[0].FindControl("dropSPAdd") as DropDownList;
            if (dropAdd != null)
            {
                dropAdd.DataSource = Constant.dictSanPham;
                dropAdd.DataValueField = "key";
                dropAdd.DataTextField = "value";
                dropAdd.DataBind();
            }
        }
    }