-->

repeater linkbutton not firing

2019-09-06 06:16发布

问题:

I have a repeater and inside it a linkbutton, the repeater is bound to OnItemCommand but the event is not firing here is my code:

<asp:Repeater ID="rptList" runat="server" OnItemDataBound="rpt_OnItemDataBound" OnItemCommand="rptList_ItemCommand">
                <ItemTemplate>
                    <asp:LinkButton ID="lbName" Text='<%# Eval("Name") %>' runat="server" 
                        CommandArgument='<%# Eval("ID").ToString() %>' CommandName="NameClick">  
                    </asp:LinkButton><br />

                    <asp:Label ID="lblCreateDate" runat="server" Text='<%# Eval("CreateDate") %>' /><br />
                    <br />
                </ItemTemplate>
            </asp:Repeater>

here is the code behind:

protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "NameClick":
                    Guid id = (Guid)e.CommandArgument;

                    //do something
                    break;

                default:
                    break;
            }
        }

回答1:

As per Ram S comment you need to make sure you are not rebinding the repeater on postback (see this question Repeater's Item command event is not firing on linkbutton click)

You will also have a problem casting a Guid in that way - change this line of code

Guid id = (Guid)e.CommandArgument;

To this

Guid id = new Guid(e.CommandArgument.ToString());