Click here to Skip to main content
15,567,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

how can i display value on page load and seleced in checkboxlist

i have a checkboxlist for "hobby"

and there are multiple items in checkboxlist.

i stored many items in database(sql server)

but when i going to edit my profile then
how can i retrive that value and want it selected..

suppose from 5o of selected items are "pc games" "playing cricket" "reading" "history"..etc
and clicke submit button and those items stored in database..
and when i going to edit...these items "pc games" "playing cricket" "reading" must be selected on edit load page

what i have to do for these many 5o of items
may i have to use for loop or if condition for these 5o of items..??
give me simple coding hint..

1 solution

1. Create a method that returns the data from database, for example:<br />
 public static List<pcgame> GetPcGames(int pcGameID)<br />
        {<br />
            List<pcgame> pcGames= new List<pcgame>();<br />
<br />
            DataTable dtResult = GetDataTable("exec GetPcGamesByPcGameID @pcGameID='" + pcGameID + "'",<br />
               CommandType.StoredProcedure);<br />
<br />
            foreach (DataRow dr in dtResult.Rows)<br />
            {<br />
                PcGame game = new PcGame(dr);<br />
                game.PcGameID = dr["PcGameID"].ToString();<br />
                game.Name = dr["Name"].ToString();<br />
               <br />
                pcGames.Add(game);<br />
            }<br />
<br />
            return pcGames;<br />
        }<br />
<br />
2. Assign the value returned from the method to the CheckBoxList:<br />
chkBox1.Source = GetPcGames(chkBox1.SelectedID);<br />
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900