简体   繁体   中英

Asp.Net / C# - How to get the Text of a Label control nested inside a Repeater?

I am trying to get the text of my label which is inside a repeater, but I keep getting a NullPointerException .

All of the data is coming from database and it is coming correctly.

When I click on the LinkButton, I want to use the Label Text for next bit code.

Aspx page:

   <asp:Repeater ID="RepeaterDepartmentParent" runat="server">
       <ItemTemplate>
                <div id="outerDiv" class="col-lg-3 col-xs-6" runat="server">
                        <!-- small box -->
                        <div>
                            <div class="inner">

                                <p>
                                   <%# DataBinder.Eval(Container.DataItem, "Department_Namestr")%>
                                </p>
                            </div>


                            <asp:Label ID="lblDepartmentId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Department_Idint")%>' Visible="true"></asp:Label>

                            <asp:LinkButton ID="linkChildDepartment" CommandName="Click" runat="server" CssClass="small-box-footer" OnClick="linkChildDepartment_Click">More info<i class="fa fa-arrow-circle-right"></i></asp:LinkButton>

                        </div>
                </div><%--<%-- ./col -->--%>
       </ItemTemplate>
       </asp:Repeater>

Code Behind:

  protected void Page_Load(object sender, EventArgs e)
    {
        DataSet dsParentDepartment = null;
        dsParentDepartment = objDepartmentBL.viewDepartmentparent();
         RepeaterDepartmentParent.DataSource = dsParentDepartment.Tables[0];
        RepeaterDepartmentParent.DataBind();

    }

  protected void linkChildDepartment_Click(Object sender, EventArgs e)
    {
        //what to write here??
        //i have tried the bellow code but it gives me every data in that loop but i
        //want the single data for a link button click.
        //foreach (RepeaterItem item in RepeaterDepartmentParent.Items)
       // {
          //  Label myLabel = (Label)item.FindControl("lblDepartmentId");
         //   myLabel.Text = Id;
        //}
       //edited code that works properly
        LinkButton linkChildDepartment = (LinkButton)sender;
        RepeaterItem item = (RepeaterItem)linkChildDepartment.NamingContainer;
        Label myLabel = (Label)item.FindControl("lblDepartmentId");

    }

How can I correctly reference the Link Button Label Text?

You can use the NamingContainer property to get the reference of the RepeaterItem . From there it's a short way to your label:

protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
    LinkButton linkChildDepartment = (LinkButton) sender;
    RepeaterItem item = (RepeaterItem) linkChildDepartment.NamingContainer;
    Label myLabel = (Label)item.FindControl("lblDepartmentId");
     // ...
}

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