无法在gridview中的页脚模板“label”中设置值

时间:2017-05-01 20:47:11

标签: c# asp.net gridview

我在gridview中有下拉(我的代码的一部分已经给出)

<asp:TemplateField HeaderText="Section">
                                        <ItemTemplate>
                                            <asp:Label runat="server" Text='<%# Bind("BOQ_SECTION") %>' ID="lblSection"></asp:Label>
                                        </ItemTemplate>
                                          <FooterTemplate>
                  <asp:DropDownList ID="ddlBOQSection" OnSelectedIndexChanged="ddlSectionItem_SelectedIndexChanged" AutoPostBack="true" runat="server">
                        </asp:DropDownList>

            </FooterTemplate>
                                    </asp:TemplateField>

                                         <asp:TemplateField HeaderText="Category Name">
                                        <ItemTemplate>
                                            <asp:Label runat="server" Text='<%# Bind("Category_Name") %>' ID="lblCategoryName"></asp:Label>
                                        </ItemTemplate>
                                             <FooterTemplate>
                        <asp:Label runat="server" Text='' ID="lblCatName"></asp:Label>

                                                 </FooterTemplate>
                                    </asp:TemplateField>

我也在footertemplate中给了一个标签。我无法从C#设置此标签的值。它显示错误为“此上下文中不存在名称lblCatName”

在aspx.cs中我有

 protected void ddlSectionItem_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropDownList ddl = sender as DropDownList;


            objInvoiceUser.P_Section_ID = int.Parse(ddl.ID);

            DataSet ds = objInvoiceUser.GetAllBySection();

            if (ds.Tables[0].Rows.Count > 0)
            {


                lblCatName.Text = ds.Tables[0].Rows[0]["SECTION_CAT_NAME"].ToString();


            }

        }

2 个答案:

答案 0 :(得分:0)

标签嵌套在转发器内。您必须使用FindControl()来获取标签:How To Find Controls In <ItemTemplate> Repeater

答案 1 :(得分:0)

我按照以下方式得到了答案

protected void ddlSectionItem_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropDownList ddl = sender as DropDownList;


            objInvoiceUser.P_Section_ID = int.Parse(ddl.SelectedItem.Value);

            DataSet ds = objInvoiceUser.GetAllBySection();

            if (ds.Tables[0].Rows.Count > 0)
            {


                ***Label lblCatName = (Label)grdBOQ.FooterRow.FindControl("lblCatName");***
                lblCatName.Text = ds.Tables[0].Rows[0]["SECTION_CAT_NAME"].ToString();


            }

        }