How to set border color from code behind?

5 posts, 1 answers
  1. Prasad
    Prasad avatar
    8 posts
    Member since:
    Feb 2012

    Posted 17 Apr 2012 Link to this post

    Hi All,

    I am looking for a way to set the border color of ComboBox from code behind using PreRender handler of the ComboBox. I have tried the following code and it does not work. Can someone please help me with this?

    protected void rcbMyCombo_PreRender(object sender, EventArgs e)
    {
            rcbMyCombo.BorderColor = System.Drawing.Color.Red;
            rcbMyCombo.ToolTip = "Error occurred with the data of the control.";
    }

    Regards,
    Prasad
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Apr 2012 Link to this post

    Hi Prasad,

    Please try setting BorderWidth to get the border as follows.

    C#:
    protected void rcbMyCombo_PreRender(object sender, EventArgs e)
     {
        rcbMyCombo.BorderWidth = Unit.Pixel(1);
        rcbMyCombo.BorderColor = System.Drawing.Color.Red;
        rcbMyCombo.ToolTip = "Error occurred with the data of the control.";
     }

    Hope this helps.

    Thanks,
    Princy.
  3. Prasad
    Prasad avatar
    8 posts
    Member since:
    Feb 2012

    Posted 18 Apr 2012 Link to this post

    Thank you for your help. It worked.

    Regards,
    Prasad
  4. Wayne
    Wayne avatar
    5 posts
    Member since:
    Feb 2018

    Posted 16 Apr 2018 in reply to Princy Link to this post

    Thanks for this. How does one remove a set colour. that is reset back to none.

    In my case it is the BackColour I am working with.

     

  5. Marin Bratanov
    Admin
    Marin Bratanov avatar
    5140 posts

    Posted 17 Apr 2018 Link to this post

    Hello Wayne,

    I have just answered your support ticket with the same question and I am pasting the information here for anyone else having the same issue.

    As a general rule I would recommend using the CssClass and stylesheets to change the colors: https://docs.telerik.com/devtools/aspnet-ajax/general-information/controlling-visual-appearance/overview.

    The BackColor inherits the value of the control parent, so you can reset it to the .Parent.BackColor value. For example:

    <asp:Panel runat="server">
        <telerik:RadTextBox runat="server" ID="rtb1"></telerik:RadTextBox>
    </asp:Panel>
    <asp:Button Text="reset back color" ID="Button1" OnClick="Button1_Click" runat="server" />
    protected void Button1_Click(object sender, EventArgs e)
    {
        //important: the cast and control hierarchy may differ in your case
        rtb1.BackColor = (rtb1.Parent as System.Web.UI.WebControls.WebControl).BackColor;
    }
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) {
            rtb1.BackColor = System.Drawing.Color.Red;
        }
    }

     


    Regards,
    Marin Bratanov
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top