Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone tell in vb code how can i get the id of button which i pressed in repeater


because btnclick event will not support because button is present in repeater against each row.
Posted

There are a couple of options and lots of examples online. For example, see http://stackoverflow.com/questions/21791208/how-can-i-tell-which-button-in-a-repeater-got-pressed[^]
 
Share this answer
 
XML
<asp:Repeater ID="myRepeater" runat="server" OnItemCommand="myRepeater_ItemCommand" OnItemDataBound="myRepeater_ItemDataBound">
    <ItemTemplate>
        <asp:Button ID="ButtonDelete" Text="Delete" runat="server"/>
    </ItemTemplate>
</asp:Repeater>


C#
public class MyData
{
    public int ID { get; set; }
    public string Name { get; set; }
}



C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        List<MyData> data = new List<MyData>();
        data.Add(new MyData { ID = 1, Name = "Item 1" });
        data.Add(new MyData { ID = 5, Name = "Item 2" });
        data.Add(new MyData { ID = 123, Name = "Item 3" });

        myRepeater.DataSource = data;
        myRepeater.DataBind();
    }
}

protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // Note this event is configured on the repeater in the markup

    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        // get the data
        MyData data = (MyData)e.Item.DataItem;

        // get the button
        Button ButtonDelete = (Button)e.Item.FindControl("ButtonDelete");

        // set the command name and argument
        ButtonDelete.CommandName = "Delete";
        ButtonDelete.CommandArgument = data.ID.ToString();
    }
}

protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    // Note this event is configured on the repeater in the markup

    switch(e.CommandName)
    {
        case "Delete":
            // get the ID of the button clicked from the argumenmt
            int id = int.Parse(e.CommandArgument.ToString());

            // delete the data that matches this id
            break;
    }
}
 
Share this answer
 
Comments
Richard Deeming 4-Aug-15 8:46am    
No need for the ItemDataBound handler; you can set the CommandName and CommandArgument properties in the markup.

<asp:Button runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' />
F-ES Sitecore 4-Aug-15 8:58am    
You can, but I personally prefer to set things in the code behind, especially when dealing with complex data structures, or using properties in links etc etc, I just think it all reads better and is easier to code, and "Eval" has all the issues that design-time binding normally does, ie lack of type safety and issues when properties change.

In my proper code I even set the event handlers in code-behind too :)
Richard Deeming 4-Aug-15 9:02am    
You can sort out the type safety and refactoring issues by specifying the ItemType on the control:

<asp:Repeater runat="server" ItemType="MyNamespace.MyData">
...
<asp:Button runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%# Item.ID %>' />
F-ES Sitecore 4-Aug-15 9:18am    
Cool, I didn't know that :) However I still prefer code in the code-behind where it is supposed to go. When a repeater gets complex with a lot of controls and properties, especially composite or complex ones, it's just better (IMEO) to move the binding to the code-behind. And the ItemTtype in the mark-up still isn't going to catch compile-time issues.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900