简体   繁体   中英

Not able to set value in footer template 'label' in gridview

I have following drop down in my gridview (part of my code is given)

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

and I have also given a label in footertemplate. I am not able to set a value to this label from C# .It shows error as "the name lblCatName doesn't exist in this context"

In aspx.cs I have

 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();


            }

        }

The label is nested inside a repeater. You have to use FindControl() to get the label: How To Find Controls In <ItemTemplate> Repeater

I got the answer in following way

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();


            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM