How to store multiple values of CheckBoxList? in ASP.net

binay00713

Broken In
I am creating a webpage using ASP.net(c#)
I have inserted a CheckBoxList inside the webpage like below

Interested in
  • Friends
  • Dating
  • Business
  • Activity Partners


where multiple values can be selected
when I click on the submit button,I want to store multiple selected values (text) in the database in a single coulmn.

and when next time the page_Load event fires,I want the multiple values to be fetched using select statement & the stored values must be selected in the CheckBoxList

My idea is to use a String datatype to store multiple values of CheckBoxList by concating & to use substring to fetch & select the multiple items
But I can't do it
Please help me...
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Well, I'd say concatenate the strings using some delimiter and then split them based on that delimiter.

Say for Eg, concatenate with comma(,)

Friends,Dating,Business

Now split it with

s.Split(',');

where s is the string which contains "Friends,Dating,Business"
 
> button1 = Submit
> instead using Session, query the DB

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack && Session["data"]!=null)
            foreach (string s in Session["data"].ToString().Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                foreach (ListItem l in CheckBoxList1.Items)
                    if (l.Text == s)
                        l.Selected = true;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (ListItem val in CheckBoxList1.Items)
            if (val.Selected)
                Session["data"] += val.Text + ",";

        foreach (ListItem l in CheckBoxList1.Items)
            l.Selected = false;
    }
 

Zangetsu

I am the master of my Fate.
@binay00713: just create a stored procedure & pass the selected values in string as ',' separated.
use column datatype a varchar
& while fetching the data use another stored procedure which returns a Table
& use split function to return comma separated values in Table form
 
Top Bottom