Solved

ArrayList in ViewState Add & Read Foreach?

Posted on 2012-03-30
2
675 Views
Last Modified: 2012-08-14
I would like to add items to an ArrayList on Gridview checkbox click events to save the Gridview ID column value. How do I code it?

(ArrayList)ViewState["arrayListInViewState"];

protected void CheckBox1_OnCheckChanged(Object sender, EventArgs e)
{
      GridViewRow rw = (GridViewRow)((Control)sender).NamingContainer;
      ArrayList.Add(GridView1.DataKeys[rw.RowIndex]["id"].ToString());
            (ArrayList)ViewState["arrayListInViewState]"
}

protected void txtData_OnTextChanged(object sender, EventArgs e)
{
      GridViewRow rw = (GridViewRow)((Control)sender).NamingContainer;
      ArrayList.Add(GridView1.DataKeys[rw.RowIndex]["id"].ToString());
            (ArrayList)ViewState["arrayListInViewState]"
}

Also unsure how to foreach the arraylist later

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //foreach...
}
0
Comment
Question by:pointeman
2 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 37789522
Use a generic list (ArrayList is  obsolete and it leads to performance issues -box/unbox)

public List<string> MyIdList

{

    get { if (ViewState["arrayListInViewState"] == null)
                ViewState["arrayListInViewState"] = new List();
            return (List<string>)(ViewState["arrayListInViewState"]; }

    set { ViewState["arrayListInViewState"] = value; }
}


protected void CheckBox1_OnCheckChanged(Object sender, EventArgs e)
{
      GridViewRow rw = (GridViewRow)((Control)sender).NamingContainer;
      MyIdList.Add(GridView1.DataKeys[rw.RowIndex]["id"].ToString());
}

protected void txtData_OnTextChanged(object sender, EventArgs e)
{
      GridViewRow rw = (GridViewRow)((Control)sender).NamingContainer;
      MyIdList.Add(GridView1.DataKeys[rw.RowIndex]["id"].ToString());
}

//forearch sample
foreach (string prime in MyIdList) // Loop through List with foreach
      {
          // use prime
      }




Check:
http://www.dotnetperls.com/list
http://www.karpach.com/property-in-viewstate.htm
0
 

Author Comment

by:pointeman
ID: 37789873
Very nice, that gets me off to good start
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

13 Experts available now in Live!

Get 1:1 Help Now