Solved

ArrayList in ViewState Add & Read Foreach?

Posted on 2012-03-30
2
682 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I use this extension method? 8 47
Binding a check box in an Asp.net DataGrid 4 40
Why don't I see this table in EDMX file? 2 23
Problem to page 4 74
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

17 Experts available now in Live!

Get 1:1 Help Now