We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Session[keys.Current.ToString()]));

Islam1424
Islam1424 asked
on
Medium Priority
558 Views
Last Modified: 2008-01-09
do you think i can change from Session to ViewState?
 
when i do Session[keys.Current.ToString()] then its loading my class and class has 53 public properties

but i dont want to load the class into session keys, is there a way i can change to ViewState or is there a way to have different approach?

protected void dg_PreRender(object sender, EventArgs e)
    {
        IEnumerator keys = Session.Contents.GetEnumerator();
       
        if (Session.Count > 0)
        {
            while (keys.MoveNext())
            {
                string value = Convert.ToString((Session[keys.Current.ToString()]));
                if (value.Substring(0, 1) == dg.MasterTableView.CurrentPageIndex.ToString())
                {
                    foreach (GridDataItem dataItem in dg.MasterTableView.Items)
                    {
                        CheckBox box = (CheckBox)dataItem["TemplateColumn"].FindControl("checkBox1");
                        string clientID = value.Substring(2, (value.Length - 7));
                        if (box.ClientID == clientID)
                        {
                            box.Checked = true;
                        }
                    }
                }
            }
        }
    }
Comment
Watch Question

Commented:
if possible create a class with keys are the the properties and assigin values to this class and set this class as the viewstate.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You need to be careful what you store in ViewState.  It is carried along with the page in a UTF-16, Base-64 encoded text.  It is meant for light-weight storage, as the page response goes down drastically when more and more things are stored in the ViewState.

Bob

Author

Commented:
i want to modify the above code, is that possible?

what is heppaning is:

its loading my clsUser in the session context and may be this is due to i'm using
((Session[keys.Current.

is there a way to find why is is it loading my class in to session context?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What are you looking to store in Session variables?

Bob

Author

Commented:
is there a way i can use dictionary instead of session? i haven't use dictionary before so i have not clue how i can replace to it.

actually, i have a data grid with check box so I'm storing how many check box user have checked.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Sure, you could use Dictionary, but if you need to persist it, then it would need to be stored in the Session.

Bob

Author

Commented:
you mean only session have to use here?, no other way?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Is your page going to post back?  If not, then you can use the Dictionary without worry.  But, if it does, then you need to store the Dictionary in the Session:

    Session["CheckedList"] = dictionaryChecked;

Bob

Author

Commented:
Thanks Bob

but how do i use dictionary ?, i dont know how to use dictionary

can you show me an example pls.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Something like this:

using System.Collections.Specialized;

StringDictionary _dictionary = new StringDictionary();

_dictionary.Add(key, value);

Bob

Author

Commented:
is there a way you can modify my below code to dictionary? please :)


protected void dg_PreRender(object sender, EventArgs e)
    {
        IEnumerator keys = Session.Contents.GetEnumerator();
       
        if (Session.Count > 0)
        {
            while (keys.MoveNext())
            {
                string value = Convert.ToString((Session[keys.Current.ToString()]));
                if (value.Substring(0, 1) == dg.MasterTableView.CurrentPageIndex.ToString())
                {
                    foreach (GridDataItem dataItem in dg.MasterTableView.Items)
                    {
                        CheckBox box = (CheckBox)dataItem["TemplateColumn"].FindControl("checkBox1");
                        string clientID = value.Substring(2, (value.Length - 7));
                        if (box.ClientID == clientID)
                        {
                            box.Checked = true;
                        }
                    }
                }
            }
        }
    }
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
You only need this For block:

                    foreach (GridDataItem dataItem in dg.MasterTableView.Items)
                    {
                        CheckBox box = (CheckBox)dataItem["TemplateColumn"].FindControl("checkBox1");
                        string clientID = value.Substring(2, (value.Length - 7));
                        if (box.ClientID == clientID)
                        {
                            box.Checked = true;
                            _dictionary.Add(clientID, box);
                        }
                    }

Bob

Author

Commented:
Error      1      The name 'value' does not exist in the current context       

      protected void RadGrid1_PreRender(object sender, EventArgs e)
    {      
            foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
            {
                CheckBox box = (CheckBox)dataItem["TemplateColumn"].FindControl("checkBox1");
                string clientID = value.Substring(2, (value.Length-7));                        
                if(box.ClientID==clientID)
                {
                    box.Checked = true;
                    _dictionary.Add(clientID, box);
                }
            }
    }
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Describe to me with words what this For loop is doing, please.

Bob

Author

Commented:
Bob,

i'm using datagrid with checkboxes so i want to maintain what i have checked even thou the user change the page numbers of datagrid.

thanks.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
What is the DataGrid bound to?

Bob

Author

Commented:
datagrid bound to a DataSet
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I would suggest adding a column for the check boxes to the DataTable, instead of what you have been trying to do.

Bob

Author

Commented:
can you throw me some sample lines please?

thanks
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
Here is one way:

Adding a CheckBox column to your DataGrid
http://www.codeproject.com/aspnet/datagridcheckbox.asp?df=100&forumid=4431&exp=0&fr=26

Bob

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I believe that I answered the question, as ultimately asked on 11/13/2006.

Bob
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.