"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Wednesday, May 19, 2010

Check RadioButton/CheckBox Depending Upon Data in DataBase

 hi eveybody,
Saving checked items in RadioButton/CheckBox  to database and showing that back like edit command.
For that i first saved that checked item in checkbox list to database by comma separated form like (TV,Fridge)

Storing data to database Which where items are checked
Code

            foreach (ListItem li in Chk_ItemList.Items)// here u will get all the item in the CheckBoxList
            {
                if (li.Selected == true)// here i filter the selected from the list
                {
                    _itemlist = _itemlist += li.Text + ","; //here i add the selected item one by one separated by comma ex:tv,coller
                }
            }

here
Chk_ItemList is my Checkboxlist id
_itemlist is a string where i storing my checked item
and normally storing to database :) .and show the content in a grid i chosen here a datagrid.

On displaying back Which where items are checked 
 i taken the SelectedIndexChanged event of grid and 

        string _Item = grd.SelectedItem.Cells[1].Text.ToString(); // here i'm getting the string TV,Fridge
        string[] Seperator = new string[] { "," }; //i define here which one is ti be eliminated from that string and i'm calling the split function
        string[] strsplitarr = _Item.Split(Seperator, StringSplitOptions.RemoveEmptyEntries);//
RemoveEmptyEntries is for remove blankspace from the string and storing that string in an array
        foreach (string _chkitem in strsplitarr)
        {
            Chk_ItemList.Items.FindByText(_chkitem).Selected = true;// by the help of foreach i'm taking all the entry of an array and finding the text in checkboxlist if i find i mark it as selected :)
        } 

'n i created a demo of it u can download that from here 

Have a nice day... 'N happy Coding :)

TitanicTitanic (10th Anniversary Edition)Titanic (Three-Disc Special Collector's Edition)

No comments: