cagin
cagin

Reputation: 5920

How to find specific repeater item

I have a repeater and it has an Asp button. I want to get repeater item which contains clicked button.

Here is a part of my repeater:

     ...
       <td>
           <asp:Button runat="server" ID="btnSaveStock" OnClick="btnSaveStock_OnClick" Text="Save" />
       </td>
    </tr>
  </ItemTemplate>

I want to access repeater item in here :

 protected void btnSaveStock_OnClick(object sender, EventArgs e)
    {
        try
        {
            Button btnSaveStock = (Button)sender;

            Repeater rptProductChance = (Repeater)btnSaveStock.Parent;

        }
        catch (Exception)
        {

            throw;
        }
    }

What should I do expect loop as check all items of repeater?

Upvotes: 0

Views: 2822

Answers (2)

Praveen Nambiar
Praveen Nambiar

Reputation: 4892

This is what you can do. You can access the RepeaterItem by casting the buttons NamingContainer.

protected void btnSaveStock_OnClick(object sender, EventArgs e)
{
    try
    {
        Button btnSaveStock = (Button)sender;
        RepeaterItem item = (RepeaterItem)btnSaveStock.NamingContainer;
        //....

    }
    catch (Exception)
    {
        throw;
    }
}

Upvotes: 1

granadaCoder
granadaCoder

Reputation: 27852

Since there are many buttons (not just one), if you want to deal with handling the button click ~in context of the current "row" of the repeater~, you probably want to wire up to the Repeater, not the button "template definition".

http://www.developer.com/net/asp/article.php/3609466/ASPNET-Tip-Responding-to-the-Repeater-Controls-ItemCommand-Event.htm

Upvotes: 0

Related Questions