Solved

ArrayList in ViewState Add & Read Foreach?

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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