Solved

ASP.NET Repeater Control & Array Index

Posted on 2009-05-19
2
628 Views
Last Modified: 2012-05-07
I have a working repeater control filled by an array. I have added a delete button, but am unsure how to reference the array index in the Command Argument to pass to the delete command.

<asp:Repeater ID="gvRC" runat="server">
  ...
 <ItemTemplate>
      <li><%#Container.DataItem %>&nbsp;<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/icon_delete.gif" CommandName="deleteStation"  CommandArgument=" "  /></li>
 ...
</asp:Repeater>

The repeater is filled with a simple array by:
gvRC.DataSource = aryRC
gvRC.DataBind()

0
Comment
Question by:el_scotty
2 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 125 total points
ID: 24424011
The index of the item is already included in the RepeaterCommandEventArgs argument of the ItemCommand event.

My code behind example is included below.  e.Item.ItemIndex contains the index of the item clicked.
 private List<string> DataSource

    {

        get

        {

            if (ViewState["DATASOURCE"] == null)

                this.DataSource = new List<string>(new string[] { "Hello", "World", "Good Bye", "Sanity" });

            return (List<string>)ViewState["DATASOURCE"];

        }

        set

        {

            ViewState["DATASOURCE"] = value;

        }

    }
 

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!this.IsPostBack)

        {

            this.Repeater1.DataSource = DataSource;

            this.Repeater1.DataBind();

        }

    }
 

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)

    {

        if (e.CommandName.Equals("deleteStation"))

        {

            this.DataSource.RemoveAt(e.Item.ItemIndex);

            this.Repeater1.DataSource = DataSource;

            this.Repeater1.DataBind();

        }

    }

Open in new window

0
 
LVL 1

Author Closing Comment

by:el_scotty
ID: 31583089
Perfect, Thank You. I figured it must be something simple like that.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now