Wednesday, August 3, 2016

How to convert from Hexadecimal Colour Code in ASP.NET?

Scenario:


In order to convert HTML Hexadecimal Colour Code in ASP.NET we have to use
System.Drawing.ColorTranslator Class. In this example there is a textbox to provide HTML Hexadecimal Value and change the Label's colour on Button Click. And on Second Button's Click it is shown how to display System Colour.

HTML Code (.aspx):


<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtColour" runat="server"></asp:TextBox>&nbsp;
        <asp:Button ID="btn1" runat="server" Text="From Hexadecimal" OnClick="btn1_Click" />&nbsp;&nbsp;
        <asp:Button ID="btn2" runat="server" Text="From System Colour" OnClick="btn2_Click" /><br />
        <br />
        <br />
        <asp:Label ID="lblDisplay" runat="server" Text="Hello..." Font-Bold="true" Font-Size="Large"></asp:Label>
    </div>
    </form>

</body>

C# Code (.aspx.cs) :


  protected void btn1_Click(object sender, EventArgs e)

    {
        string value=txtColour.Text;
        lblDisplay.ForeColor = System.Drawing.ColorTranslator.FromHtml(value);
    }
    protected void btn2_Click(object sender, EventArgs e)
    {
        string value = txtColour.Text;
        lblDisplay.ForeColor = System.Drawing.Color.Green;
    }

Output :


From Hexa decimal :














From System Colour :


No comments:

Post a Comment