当前位置:  开发笔记 > 后端 > 正文

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

如何解决《ASP.Net:为什么我的按钮的点击/命令事件没有在转发器中绑定/触发?》经验,为你挑选了1个好方法。

这是来自具有转发器的ascx的代码:


    

A sub-header:

[Some other stuff is here]

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

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

click

") End Sub Sub removeEmail_Command(ByVal sender As Object, ByVal e As CommandEventArgs) Response.Write("

command

") End Sub

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



1> FlySwat..:

嵌套在Repeater内部的控件不拦截事件.相反,您需要绑定到Repeater.ItemCommand事件.

ItemCommand包含RepeaterCommandEventArgs哪两个重要字段:

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

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有