【问题标题】:findcontrol footertemplate in datagrid asp在datagrid asp中找到控制页脚模板
【发布时间】:2015-12-01 07:11:59
【问题描述】:

我想在 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(对象发送者,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(); } }

【问题讨论】:

    标签: c# asp.net datagrid


    【解决方案1】:

    使用以下语法获取 Footer 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();
                }
            }
        }
    

    【讨论】:

    • 我尝试但它显示错误:指定的参数超出了有效值的范围。参数名称:索引
    • 你能检查一下你的 dtgDSSP.Controls[0].Controls[dtgDSSP.Controls[0].Controls.Count - 1].Controls.Count 返回值是否大于 0?因为 dtgDSSP.Controls[0].Controls[dtgDSSP.Controls[0].Controls.Count - 1] 获取数据网格中的最后一行,而最后一项的 control[0] 获取最后一行的第一列(页脚模板)。我可以使用这个 syantex 获得页脚模板。
    • 它返回 "null" ,我在事件 Page_Load 中使用 dtgDSSP.Controls[0].Controls[dtgDSSP.Controls[0].Controls.Count - 1].Controls ,我想绑定数据到下拉列表 dropSPAdd
    • 在这种情况下,您需要先将数据源应用到数据网格,然后在 ItemDataBound 事件中绑定“dropSPAdd”DropDownList。像: - protected void dtgDSSP_ItemDataBound(object sender, DataGridItemEventArgs e) { if (e.Item.ItemType == ListItemType.Footer) { var obj = e.Item.Controls[0].FindControl("dropSPAdd") as DropDownList; //绑定数据源 } }
    • 这成功了。非常感谢Pragnesh ^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2022-03-16
    • 2014-12-04
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    相关资源
    最近更新 更多