Solved

Preserve checkboxlist using session

Posted on 2012-04-02
3
580 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
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

18 Experts available now in Live!

Get 1:1 Help Now