ASP.Net:为什么我的按钮的单击/命令事件没有在转发器中绑定/触发?

发布于 2024-07-04 21:34:28 字数 1307 浏览 7 评论 0原文

这是来自具有中继器的 ascx 的代码:

<asp:Repeater ID="ListOfEmails" runat="server" >
    <HeaderTemplate><h3>A sub-header:</h3></HeaderTemplate>
    <ItemTemplate>
        [Some other stuff is here]
        <asp:Button ID="removeEmail" runat="server" Text="X" ToolTip="remove" />
    </ItemTemplate>
</asp:Repeater>

在中继器数据绑定和事件的代码隐藏中:

Protected Sub ListOfEmails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ListOfEmails.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim removeEmail As Button = CType(e.Item.FindControl("removeEmail"), Button)
        removeEmail.CommandArgument = e.Item.ItemIndex.ToString()

        AddHandler removeEmail.Click, AddressOf removeEmail_Click
        AddHandler removeEmail.Command, AddressOf removeEmail_Command
    End If
End Sub

Sub removeEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Write("<h1>click</h1>")
End Sub

Sub removeEmail_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
    Response.Write("<h1>command</h1>")
End Sub

单击或命令都没有被调用,我做错了什么?

Here's the code from the ascx that has the repeater:

<asp:Repeater ID="ListOfEmails" runat="server" >
    <HeaderTemplate><h3>A sub-header:</h3></HeaderTemplate>
    <ItemTemplate>
        [Some other stuff is here]
        <asp:Button ID="removeEmail" runat="server" Text="X" ToolTip="remove" />
    </ItemTemplate>
</asp:Repeater>

And in the codebehind for the repeater's databound and events:

Protected Sub ListOfEmails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ListOfEmails.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim removeEmail As Button = CType(e.Item.FindControl("removeEmail"), Button)
        removeEmail.CommandArgument = e.Item.ItemIndex.ToString()

        AddHandler removeEmail.Click, AddressOf removeEmail_Click
        AddHandler removeEmail.Command, AddressOf removeEmail_Command
    End If
End Sub

Sub removeEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Write("<h1>click</h1>")
End Sub

Sub removeEmail_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
    Response.Write("<h1>command</h1>")
End Sub

Neither the click or command is getting called, what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

世俗缘 2024-07-11 21:34:28

这里有一个实验供您尝试:

在 ListOfEmails_ItemDataBound 上设置断点,然后查看是否调用它进行回发。

Here's an experiment for you to try:

Set a breakpoint on ListOfEmails_ItemDataBound and see if it's being called for postbacks.

莫相离 2024-07-11 21:34:28

你知道这有什么令人沮丧的吗?

如果您在该 asp:Button 标记中指定 OnClick,构建验证代码隐藏类中是否存在命名方法,如果不存在,则报告错误...即使该方法将永远不会被打电话。

You know what's frustrating about this?

If you specify an OnClick in that asp:Button tag, the build will verify that the named method exists in the codebehind class, and report an error if it doesn't... even though that method will never get called.

不羁少年 2024-07-11 21:34:28

如果您计划使用 ItemCommand 事件,请确保在 Page_Init 中而不是在 Page_Load 中注册 ItemCommand 事件。

protected void Page_Init(object sender, EventArgs e)
{
    // rptr is your repeater's name
    rptr.ItemCommand += new RepeaterCommandEventHandler(rptr_ItemCommand);
}

我不确定为什么在 Page_Load 中注册的此事件对我不起作用,但将其移动到 Page_Init 有帮助。

If you're planning to use ItemCommand event, make sure you register to ItemCommand event in Page_Init not in Page_Load.

protected void Page_Init(object sender, EventArgs e)
{
    // rptr is your repeater's name
    rptr.ItemCommand += new RepeaterCommandEventHandler(rptr_ItemCommand);
}

I am not sure why it wasn't working for me with this event registered in Page_Load but moving it to Page_Init helped.

负佳期 2024-07-11 21:34:28

您需要处理 ItemCommand 事件< /a> 在你的中继器上。 这是示例

然后,您的按钮点击将由 ListOfEmails_ItemCommand 方法处理。 我认为在 ItemDataBound 中连接 Click 或 Command 事件(按钮的)不起作用。

You need to handle the ItemCommand event on your Repeater. Here's an example.

Then, your button clicks will be handled by the ListOfEmails_ItemCommand method. I don't think wiring up the Click or Command event (of the button) in ItemDataBound will work.

十年九夏 2024-07-11 21:34:28

嵌套在中继器内的控件不会拦截事件。 相反,您需要绑定到Repeater.ItemCommand 事件。

ItemCommand 包含 RepeaterCommandEventArgs< /code>其中有两个重要字段:

  • CommandName
  • CommandArgument

那么,一个简单的例子:

void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        // Stuff to databind
        Button myButton = (Button)e.Item.FindControl("myButton");

        myButton.CommandName = "Add";
        myButton.CommandArgument = "Some Identifying Argument";
    }
}

void rptr_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Add")
    {
        // Do your event
    }
}

Controls nested inside of Repeaters do not intercept events. Instead you need to bind to the Repeater.ItemCommand Event.

ItemCommand contains RepeaterCommandEventArgs which has two important fields:

  • CommandName
  • CommandArgument

So, a trivial example:

void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        // Stuff to databind
        Button myButton = (Button)e.Item.FindControl("myButton");

        myButton.CommandName = "Add";
        myButton.CommandArgument = "Some Identifying Argument";
    }
}

void rptr_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "Add")
    {
        // Do your event
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文