Solved

Preserve checkboxlist using session

Posted on 2012-04-02
3
588 Views
Last Modified: 2012-04-03
Been working on this for 4 hours now!

1. I have a checkboxList on page xyz.aspx
2. There's a seach link on this page. User clicks it, goes to search.aspx. Selects a row. I pass the id to xyz.aspx page
3. I use this ID and call the database, get a value and bind to checkboxList. So far so good.
4. But now, user might leave this page again and go to search and get another id. Now, I have 2 ids.
5. When I get back to page xyz.aspx, I want to bind both Ids to the checkboxlist. I'm thinking about using a session var and loop thru the session and bind bit not sure how to do this.

This is my code:

 if (Session["providerId"] != null)
        {

            List<usp_LoadProviderNameResult> provider = sd.LoadProvider(ReferredToProviderId);

            ckProviders.DataSource = provider;
            ckProviders.DataTextField = "Providername";
            ckProviders.DataValueField = "Id";
            ckProviders.DataBind();
        }
        List<string> selectedProviders = new List<string>();
        selectedProviders.Add(ReferredToProviderId.ToString());
        Session["providerId"] = selectedProviders;

Open in new window

0
Comment
Question by:Camillia
[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
3 Comments
 
LVL 53

Accepted Solution

by:
Dhaest earned 250 total points
ID: 37799452
0
 
LVL 7

Assisted Solution

by:dhawalseth
dhawalseth earned 250 total points
ID: 37799895
Hi Farzadw,

You are pretty near to what you want to achieve. I believe the code you wrote is a complicated one and you can replace it with string[] object with ',' or ';' as delimiter to separate two entries.
There are a few things which i would like to point out.
1. By step 3 you mean that you pick a value from the database based on the passed id, and then select the value in checkboxlist.(correct me if i am wrong)
2. The list<string> object works fine, but if there is an existing entry in your check box list- it will not be preserved.
Possible solution: do this functionality when you are navigating to new page (using a button click event will do)
List<string> selectedProviders = new List<string>();
        //selectedProviders.Add(ReferredToProviderId.ToString());
replace this line with:
if(ckProviders.Items.Count > 0)
for(int var =0; var > ckProviders.Items.Count; var++)
{
if(ckProviders.Items[var].Selected)
{
selectedProviders.Add(value you want to add);
}
}
  Session["providerId"] = selectedProviders;
On page load do a reverse after you have bind your checkbox list.
if(Session["providerId"] != null)
List<string> selectedProviders = Session["providerId"];//Please convert this value back to List<string> type.
            foreach (string providerId in selectedProviders)
            {
                ListItem item = ckProviders.Items.FindByValue(providerId);
                if (item != null)
                    item.Selected = true;
            }

If you want to play with an string[] object follow the link shared above, works like a charm and a very good example is posted.

Mark as answer if helped. You might need to fix here and there if required in the code.
Enjoy!!
0
 
LVL 7

Author Comment

by:Camillia
ID: 37800624
let me review, thanks
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 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