Abdul Basit
Abdul Basit

Reputation: 722

How to get Reference to the label in repeater item in code behind

<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>

Code Behind:

protected void Delete_Command(object sender, CommandEventArgument e)
{

}

how i get the reference to the "lblMessage" in Delete_Command.

Upvotes: 0

Views: 2360

Answers (3)

user1429080
user1429080

Reputation: 9166

Try this:

protected void Delete_Command(object sender, CommandEventArgs e)
{
    LinkButton button = (LinkButton)sender;
    Label label = (Label)button.NamingContainer.FindControl("lblMessage");
    // do something with the label
}

If you:

  1. Have bound the repeater
  2. Have ViewState enabled
  3. Do not re-bind the repeater earlier in the post back

this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.

Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.

Upvotes: 2

Sumit Kapadia
Sumit Kapadia

Reputation: 385

If You want to make it in your way use following code in

protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
        {

             (((LinkButton)source).NamingContainer).FindControl("lblName")

        }

Another approach.. But something that you can buy

aspx

<asp:Repeater ID="Repeater1" runat="server" 
        onitemcommand="Repeater1_ItemCommand">
        <ItemTemplate>
      <asp:Label ID="lblName" runat="server" Text='<%=Eval("Name") %>' ></asp:Label>
      <asp:LinkButton runat="server"  CommandName="Delete_Command" Text="sd"></asp:LinkButton>
        </ItemTemplate>
    </asp:Repeater>

.cs

 protected void Delete_Command(object sender, CommandEventArgument e)
    {
          if(e.CommandName != null)// Conditional Check
          {    
               Label label = e.Item.FindControl("lblMessage");
               // do something with the label
          }
   }

Upvotes: 0

Manish Sharma
Manish Sharma

Reputation: 2426

means are you want find a lable in Delete_Command event?

in aspx

 <asp:Repeater ID="rpt" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="Delete" runat="server" OnCommand="Delete_Command"></asp:LinkButton>
        <asp:Label ID="lblMessage" run="server">
    </ItemTemplate>
</asp:Repeater>

in aspx.cs

 protected void Delete_Command(object sender, CommandEventArgs e)
    {
        foreach (RepeaterItem item in rpt.Items)
        {
            Label lblMessage = item.FindControl("lblMessage") as Label;
            if (lblMessage != null)
            {
                lblMessage.Text = "";
            }
        }
    }

Upvotes: 0

Related Questions