Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ArrayList in ViewState Add & Read Foreach?

Posted on 2012-03-30
2
Medium Priority
?
710 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
[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 36

Accepted Solution

by:
Miguel Oz earned 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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