1

我有一个转发器,里面有一个链接按钮,转发器绑定到 OnItemCommand 但事件没有触发这里是我的代码:

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

这是后面的代码:

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

                    //do something
                    break;

                default:
                    break;
            }
        }
4

1 回答 1

1

根据 Ram S 评论,您需要确保您没有在回发时重新绑定转发器(请参阅此问题Repeater's Item command event is not fire on linkbutton click

以这种方式投射 Guid 也会有问题 - 更改这行代码

Guid id = (Guid)e.CommandArgument;

对此

Guid id = new Guid(e.CommandArgument.ToString());
于 2012-10-30T10:23:25.777 回答