Solved

Preserve checkboxlist using session

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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