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

User control edit form checkboxlist selecting items

3 Answers 120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 02 Mar 2009, 08:44 AM
Hi,

I have RadGrid which uses a user control (Companies.ascx) as edit form. User control contains chechboxlist and its items are populated dynamically from DB. Now I have a problem with selecting the correct checkboxlist items when grid row is in edit mode. The following code selects the items in user controls DataBinding event but after that in some phase those selection are "reset".

I have tried many different approaches and the following is just one of my tests. Can you help me with this one.

private object _dataItem;  
 
public object DataItem  
{  
    get 
    {  
        return this._dataItem;  
    }  
    set 
    {  
        this._dataItem = value;  
    }  
}  
 
 
protected override void OnInit(EventArgs e)  
{  
    base.OnInit(e);  
 
    this.DataBinding += new EventHandler(Companies_DataBinding);  
}  
 
 
 
 
protected void Companies_DataBinding(object sender, System.EventArgs e)  
{  
    txtName.Text = DataBinder.Eval(DataItem, "CompanyName").ToString();  
      
    using (DBService dms = new DBService())  
    {  
        cblProducts.DataSource = dms.GetProducts();  
        cblProducts.DataBind();  
    }  
 
 
    // get company's products -> string where products are separated with comma  
    object objProducts = DataBinder.Eval(DataItem, "Products");  
 
    if (objProducts != DBNull.Value)  
    {  
        foreach (string prod in objProducts.ToString().Split(','))  
        {  
            ListItem li = cblProducts.Items.FindByText(prod);  
 
            if (li != null)  
                li.Selected = true;  
        }  
    }  
}   


Regards,
Pete

3 Answers, 1 is accepted

Sort by
0
Pete
Top achievements
Rank 1
answered on 02 Mar 2009, 01:38 PM
Hi,

I managed to solve this one by implementing DataBound event for the CheckBoxList and set the listitems which should be selected in that event. I don't know is this the effecient way to do this but it works.

One thing confuses me is that I have also dropdownlist in the form which items are populated from the DB and I am able to set dropdown's selectedvalue in the DataBinding event just fine.

Regards,
Pete
0
Annie
Top achievements
Rank 1
answered on 18 May 2011, 02:27 PM
Thanks Pete.  I had the same problem and your post helped me solved it!

Annie
0
Thontesh
Top achievements
Rank 1
answered on 17 Nov 2011, 06:44 AM
Hi,
I have a problem in binding the selected value for rad combobox as you
described for CheckBoxList,could yu please find me a solution.

Regards,
Thontesh
Tags
Grid
Asked by
Pete
Top achievements
Rank 1
Answers by
Pete
Top achievements
Rank 1
Annie
Top achievements
Rank 1
Thontesh
Top achievements
Rank 1
Share this question
or