?
Solved

ArrayList in ViewState Add & Read Foreach?

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

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

770 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