【问题标题】:Not able to set value in footer template 'label' in gridview无法在 gridview 的页脚模板“标签”中设置值
【发布时间】:2017-05-01 20:47:10
【问题描述】:

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

我还在页脚模板中给出了一个标签。我无法从 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();


            }

        }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

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

    【讨论】:

    • 我不了解中继器,因为我是 ASp.net 的新手,但我使用 findcondrol.plz 解决了下面的问题,并且我更改了获取选择框 ID 的方式。
    【解决方案2】:

    我通过以下方式得到了答案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多