Change Color of text, control through dropdown list in asp.net


1. Aspx File :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Drop_Down.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Working with Dropdown Controls </title>
    <style type="text/css">
        table
        {
            font-family: Arial;
            font-size: 12px;
            border: solid 1px #ccc;
        }
        td
        {
            height: 30px;
            text-align: center;
            font-weight: bold;
            border: solid 1px #ccc;
        }
        a
        {
            color: Black;
            text-decoration: none;
        }
        a:hover
        {
            color: Red;
            text-decoration: underline;
        }
        a:visited
        {
            color: Purple;
            text-decoration: underline;
        }
    </style>
</head>
<
body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <table width="500px" cellpadding="5" cellspacing="5" align="center">
        <tr>
            <td colspan="2" style="background: #999; color: #ffffff;">
                <b>Change Face Color & Background Color through below Example </b>
            </td>
        </tr>
        <tr>
            <td>
                <asp:LinkButton ID="Drop_lnkbtn" runat="server" OnClick="Drop_lnkbtn_Click">Dropdown list Example </asp:LinkButton>
            </td>
            <td>
                <asp:LinkButton ID="Radio_lnkbtn" runat="server"
OnClick="Radio_lnkbtn_Click">Radio Button Example </asp:LinkButton>
            </td>
        </tr>
    </table>
    <asp:PlaceHolder ID="Place_Dropdown" runat="server">
        <table width="500px" cellpadding="5" cellspacing="5" align="center">
            <tr>
                <td colspan="4">
                    Change Face Color & Background Color through Dropdown List
                </td>
            </tr>
            <tr>
                <td>
                    Face Color :
                </td>
                <td>
                    <asp:DropDownList ID="drop_down" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drop_down_SelectedIndexChanged">
                        <asp:ListItem> Red </asp:ListItem>
                        <asp:ListItem> Green </asp:ListItem>
                        <asp:ListItem> Blue </asp:ListItem>
                        <asp:ListItem> Black </asp:ListItem>
                        <asp:ListItem> Yellow </asp:ListItem>
                        <asp:ListItem> Orange </asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                    Back Color :
                </td>
                <td>
                    <asp:DropDownList ID="drop_Background" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drop_Background_SelectedIndexChanged">
                        <asp:ListItem> Red </asp:ListItem>
                        <asp:ListItem> Green </asp:ListItem>
                        <asp:ListItem> Blue </asp:ListItem>
                        <asp:ListItem> Black </asp:ListItem>
                        <asp:ListItem> Yellow </asp:ListItem>
                        <asp:ListItem> Orange </asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <asp:Label ID="lbl" runat="server" BackColor="AliceBlue" Text="Label" Font-Size="16px"></asp:Label>
                </td>
            </tr>
        </table>
    </asp:PlaceHolder>
    <asp:PlaceHolder ID="Place_Radiobtn" runat="server">
        <table width="500px" cellpadding="5" cellspacing="5" align="center">
            <tr>
                <td colspan="4">
                    Change Face Color & Background Color through Radio Button List
                </td>
            </tr>
            <tr>
                <td>
                    Face Color :
                </td>
                <td>
                    <asp:RadioButtonList ID="face_radio" runat="server" AutoPostBack="true" OnSelectedIndexChanged="face_radio_SelectedIndexChanged">
                        <asp:ListItem> Red </asp:ListItem>
                        <asp:ListItem> Green </asp:ListItem>
                        <asp:ListItem> Blue </asp:ListItem>
                        <asp:ListItem> Black </asp:ListItem>
                        <asp:ListItem> Yellow </asp:ListItem>
                        <asp:ListItem> Orange </asp:ListItem>
                    </asp:RadioButtonList>
                </td>
                <td>
                    Back Color :
                </td>
                <td>
                    <asp:RadioButtonList ID="back_radio" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="back_radio_SelectedIndexChanged">
                        <asp:ListItem> Red </asp:ListItem>
                        <asp:ListItem> Green </asp:ListItem>
                        <asp:ListItem> Blue </asp:ListItem>
                        <asp:ListItem> Black </asp:ListItem>
                        <asp:ListItem> Yellow </asp:ListItem>
                        <asp:ListItem> Orange </asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <asp:Label ID="lblradio" runat="server" BackColor="AliceBlue" Text="Label"
Font-Size="16px"></asp:Label>
                </td>
            </tr>
        </table>
    </asp:PlaceHolder>
    </form>
</body>
</
html> 

2. CS File :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lbl.Visible = false;
            lblradio.Visible = false;
            Place_Dropdown.Visible = false;
            Place_Radiobtn.Visible = false;
        }
        
    }
    protected void drop_down_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbl.Visible = true;
        string s = drop_down.SelectedItem.Text.ToString();
        lbl.ForeColor = System.Drawing.Color.FromName(s) ;
        lbl.Text ="This Color is :" + s;
        drop_down.ForeColor = System.Drawing.Color.FromName(s);
        
    }
    protected void drop_Background_SelectedIndexChanged(object sender, EventArgs e)
    {
        string s1 = drop_Background.SelectedItem.Text.ToString();
        lbl.BackColor = System.Drawing.Color.FromName(s1);
        lbl.Text = "Background Color is :" + s1;
        drop_Background.BackColor = System.Drawing.Color.FromName(s1);
    }
    protected void face_radio_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblradio.Visible = true;
        string face = face_radio.SelectedItem.Text.ToString();
        lblradio.ForeColor = System.Drawing.Color.FromName(face);
        lblradio.Text = "Selected Color is :" + face;
        face_radio.ForeColor = System.Drawing.Color.FromName(face);
    }
    protected void back_radio_SelectedIndexChanged(object sender, EventArgs e)
    {
        string back = back_radio.SelectedItem.Text.ToString();
        lblradio.BackColor = System.Drawing.Color.FromName(back);
        lblradio.Text = "Background Color is :" + back;
        back_radio.BackColor = System.Drawing.Color.FromName(back);
    }
    protected void Drop_lnkbtn_Click(object sender, EventArgs e)
    {
        Place_Dropdown.Visible = true;
        Place_Radiobtn.Visible = false;
    }
    protected void Radio_lnkbtn_Click(object sender, EventArgs e)
    {
        Place_Dropdown.Visible = false;
        Place_Radiobtn.Visible = true;
    }
}


Similar Articles