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

Set Style From Code Behind

7 Answers 400 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Ryan Grossman
Top achievements
Rank 1
Ryan Grossman asked on 22 Apr 2015, 03:02 PM

I'm updating an application from  the Classic controls to the newest version. Some of the scripts are working correctly. I need to set the border and background of a RadCombobox to Red to signify its a required field. I used to do the following:

 RadAjaxPanelContacts.ResponseScripts.Add("LoadCombobox(" & rcbContactTxt.ClientID & ");")

 function LoadCombobox(combobox)
{
combobox.InputDomElement.style.border = "solid 1px #a80532";
combobox.InputDomElement.style.color = "#a80532";
combobox.InputDomElement.style.background = "white";
}

 

This now triggers a javascript error type error element undefined. I tried replacing with:

 

function LoadCombobox(combobox)
{
    combobox.element.style.border = "solid 1px #ff0000";
    combobox.element.style.color = "#ff0000";
    combobox.element.style.background = "white";
}

 

 Doesn't work either. How do I do what the old code does with the newer version of the controls?

7 Answers, 1 is accepted

Sort by
0
Magdalena
Telerik team
answered on 27 Apr 2015, 07:42 AM
Hello Ryan,

We recommend you to apply styling by stylesheet and by JavaScript apply only a CSS class that can be used as a CSS selector. You can apply a custom CSS class to the RadComboBox by the following:
function pageLoad() {
    var combobox = $find("RadComboBox1").get_element();
    $telerik.$(combobox).addClass("custom");
}

After that you can apply color, border and background rules to the selector .RadComboBox.custom .rcbReadOnly {}. or .RadComboBox.custom .rcbInner {} - depending of the scenario.

Regards,
Magdalena
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Ryan Grossman
Top achievements
Rank 1
answered on 27 Apr 2015, 01:41 PM
I'm not using jquery. How do I do this without jquery? The old way worked fine with plain javascript.
0
Magdalena
Telerik team
answered on 28 Apr 2015, 11:18 AM
Hello Ryan,

The CSS class can be added to an element also by .className:
function pageLoad() {
    var comboboxElement = $find("RadComboBox1").get_element();
    comboboxElement.className += comboboxElement.className + " custom";
}

Regards,
Magdalena
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Ryan Grossman
Top achievements
Rank 1
answered on 28 Apr 2015, 03:19 PM

That worked. How can I do the same for a RadTextBox? I tried the following but maybe my CSS class is wrong and not applying.

 function ValueChanged(sender, eventArgs)
{
    sender.get_styles().EnabledStyle[1] += " custom3";
    sender.updateCssClass();
}

 .RadInput.custom3 .riTextBox {
    background-color: #e7ffe8 !important;
    border-color: #035105 !important;
    color: #035105 !important;
}

 

0
Magdalena
Telerik team
answered on 29 Apr 2015, 08:23 AM
Hi Ryan,

If you debug the ValueChanged function, you can see that the "sender" is TextBox object, not Input object. So the CSS selector will be .RadInput .riTextBox.custom3 {}.

The other approach is to set the class name in a similar way as in the first solution:
function ValueChanged(sender, eventArgs)
{
    var inputElement = sender.get_element();
    inputElement.className += inputElement.className + " custom";
}

Please, keep in mind, that if you do not use jQuery "addClass()", it is necessary to do verification if the class has not already been added. Otherwise the class will be added every time when the ValueChanged function is fired.

Regards,
Magdalena
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Ryan Grossman
Top achievements
Rank 1
answered on 08 May 2015, 12:27 PM

This is working good for the combo and textbox. Can you provide an example of how to change a raddatepicker as well?

 

<telerik:RadDatePicker ID="rdpShippedDate" Width="100%" runat="server" DateInput-Width="100%"></telerik:RadDatePicker>

0
Magdalena
Telerik team
answered on 11 May 2015, 02:58 PM
Hi Ryan,

It will be the same as ComboBox:
datePickerElement.className += datePickerElement.className + " custom";


Regards,
Magdalena
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
ComboBox
Asked by
Ryan Grossman
Top achievements
Rank 1
Answers by
Magdalena
Telerik team
Ryan Grossman
Top achievements
Rank 1
Share this question
or