SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Poonu Verma Articles | Current Affairs November 12, 2010
In this article you will learn how to change through dropdown list in asp.net
Reader Level:
Download Files:
 

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;
    }
}

Login to add your contents and source code to this article
share this article :
post comment
 

why only on one color is possible cant we make both fore color and background color how if we store previous value somewhere and apply both

Posted by Harshit Vyas Nov 16, 2010

Article title is wrong...change it.

Posted by Raj Kumar Nov 12, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor