[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ASP.NET Repeater Control & Array Index

Posted on 2009-05-19
2
Medium Priority
?
662 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
wht1986 earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

650 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