This is a migrated thread and some comments may be shown as answers.

[Solved] how to pre-check checkbox when using CheckBoxes for multi-item selection

2 Answers 236 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
steven
Top achievements
Rank 1
steven asked on 20 Jan 2010, 09:58 PM
I use <a href="http://www.telerik.com/community/code-library/aspnet-ajax/combobox/using-checkboxes-for-multi-item-selection.aspx">Using CheckBoxes for multi-item selection</a> to do a multi-item selection. but I want to pre-select some items base on a query from database.

to make myself clear, say I havea combo with there checkbox,

item1
item2
item3
item4

when page load, i want to pre-check some of these items based on which user is login, if user1 login, all items except item1 is checked, if user2 login, item1 and item4 is checked, and so on.

How to do I do that?  thanks.

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 21 Jan 2010, 01:13 PM
Hi Steven,

You can access the ComboboxItems and get the corresponding checkbox control to set Checked property accordingly. See the example shown below.

CS:
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!this.IsPostBack) 
        { 
            this.RadComboBox1.DataBind(); 
 
            string user = "user2"// Setting the user for this example 
            if (user == "user1"// Here check for user logged in 
            { 
                foreach (RadComboBoxItem item in RadComboBox1.Items) 
                { 
                    CheckBox chk = (CheckBox)item.FindControl("CheckBox"); 
                    if (item.Index != 0) 
                    { 
                        chk.Checked = true
                    } 
                } 
            } 
            else if (user == "user2")  // Here check for user logged in 
            { 
                CheckBox chk1 = (CheckBox)RadComboBox1.FindItemByText("One").FindControl("CheckBox"); 
                chk1.Checked = true
 
                CheckBox chk2 = (CheckBox)RadComboBox1.FindItemByText("Three").FindControl("CheckBox"); 
                chk2.Checked = true
            } 
        } 
    } 

Regards,
Shinu.
0
steven
Top achievements
Rank 1
answered on 22 Jan 2010, 02:54 PM
thanks, shinu, should have thought of that.
Tags
ComboBox
Asked by
steven
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
steven
Top achievements
Rank 1
Share this question
or