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

Styling items programmatically

3 Answers 240 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 25 Apr 2012, 02:49 PM
Hi

I am building the list of items in the itemcreated event, as suggested in a reply to another post, using this approach:

combo.Items.Add(New RadComboBoxItem(sb.ToString, MemberID))


This is working fine so far, but I would like to change the background color (or text color) of the item based on some criteria. (Such as Member not paid up!)

My question is how to add the relevant style attribute to the item I am adding . I was expecting to find something like...

combo.Items.AddAttribute( something here)

but I didn't find it the right approach anywhere in the docs. Sorry if it is there somewhere.

Can someone help me out?

Thanks

Clive


3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Apr 2012, 03:40 PM
Hi Clive,

Here is a sample code that i tried to change the Backgroundcolor of RadComboBoxItem in itemCreated event.

ASPX:
<telerik:RadComboBox ID="radCombobox1" runat="server" EnableLoadOnDemand="true" OnItemCreated="radcombobox_itemCreated" >
</telerik:RadComboBox>

C#:
protected void Page_Load(object sender, EventArgs e)
    {
        radCombobox1.Items.Add(new RadComboBoxItem("Item1", "1");
        radCombobox1.Items.Add(new RadComboBoxItem("Item2", "2"));
        radCombobox1.Items.Add(new RadComboBoxItem("Item3", "3"));
        radCombobox1.Items.Add(new RadComboBoxItem("Item4", "4"));
        radCombobox1.Items.Add(new RadComboBoxItem("Item5", "5"));
    }
protected void radcombobox_itemCreated(object sender, Telerik.Web.UI.RadComboBoxItemEventArgs e)
    {
        RadComboBoxItem item = e.Item;
        if (item.Text == "Item1")
        {
            item.BackColor = System.Drawing.Color.Red;
            item.ForeColor = System.Drawing.Color.White;
        }
    }

Hope this helps.

Regards,
Princy.
0
Clive Hoggar
Top achievements
Rank 1
answered on 25 Apr 2012, 05:15 PM
Hi

Thanks

However the combo box is in the edit form of a grid, so this does not seem to work. Visual Studio cannot find the radcombobox_itemCreated event as a page event.

I tried inserting the sub and adding the call to the event in the combo box declaration. There were no errors but it seemed to
ignore it.

Can you give me a hint for this situation?

Thanks

Clive
0
Princy
Top achievements
Rank 2
answered on 26 Apr 2012, 05:34 AM
Hello Clive,

Try the following code to populate combobox in editform and to change the color of the text.
aspx:
<EditFormSettings EditFormType="Template">
 <FormTemplate>
   <telerik:RadComboBox ID="combo" runat="server" onitemcreated="combo_ItemCreated"></telerik:RadComboBox>
 </FormTemplate>
</EditFormSettings>
C#:
protected void Radgrid1_ItemCreated(object sender, GridItemEventArgs e)
{
  if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
  {
   GridEditFormItem item = (GridEditFormItem)e.Item;
   RadComboBox combo = (RadComboBox)item.FindControl("combo");
   combo.Items.Add(new RadComboBoxItem("Item1", "1"));
   combo.Items.Add(new RadComboBoxItem("Item2", "2"));
   combo.Items.Add(new RadComboBoxItem("Item3", "3"));
   combo.Items.Add(new RadComboBoxItem("Item4", "4"));
   combo.Items.Add(new RadComboBoxItem("Item5", "5"));
  }
}
protected void combo_ItemCreated(object sender, RadComboBoxItemEventArgs e)
{
  RadComboBoxItem item = e.Item;
  if (item.Text == "Item1")
  {
   item.BackColor = System.Drawing.Color.Red;
   item.ForeColor = System.Drawing.Color.White;
  }
}

Thanks,
Princy.
Tags
ComboBox
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Clive Hoggar
Top achievements
Rank 1
Share this question
or