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

℡╲_俬逩灬. 提交于 2019-12-12 03:15:13

问题


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


回答1:


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.




回答2:


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 = "";
            }
        }
    }



回答3:


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


来源:https://stackoverflow.com/questions/17541206/how-to-get-reference-to-the-label-in-repeater-item-in-code-behind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!