转发器链接按钮没有触发

时间:2012-10-30 10:05:17

标签: c# asp.net events repeater linkbutton

我有一个转发器,里面有一个linkbutton,转发器绑定到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;
            }
        }

1 个答案:

答案 0 :(得分:1)

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

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

Guid id = (Guid)e.CommandArgument;

到此

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