ASP.Net: why is my button's click/command events not binding/firing in a repeater?

后端 未结 5 1727
北海茫月
北海茫月 2020-12-09 12:31

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


    

A sub-he

5条回答
  •  一生所求
    2020-12-09 13:25

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

提交回复
热议问题