Today, I have provided an article showing you how to change the color of text using a DropDownList in ASP. NET. There are many ways in which a user can change the color of text using a DropDownList. Here, we use the color class to change the color of text using a DropDownList. It's easy to implement. We drag and drop two DropDownList controls onto the form; one is for the foreground color and the second one is used to change the background color and also drag and drop a Label control for showing the changing color effect. First of all start Visual Studio .NET and make a new ASP.NET web site using Visual Studio 2010.
Now you have to create a web site.
- Go to Visual Studio 2010
- New-> Select a website application
- Click OK
Now add a new page to the website.
- Go to the Solution Explorer
- Right-click on the Project name
- Select add new item
- Add new web page and give it a name
- Click OK
Now drag and drop two DropDownList controls from the Toolbox to the page; one is for the foreground color and the second one is used to change the background color. Let's take a look at a practical example. The .aspx code will be as shown below.
<%@ 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;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table width="500px" cellpadding="5" cellspacing="5" align="center">
<tr>
<td>
<asp:LinkButton ID="Drop_lnkbtn" runat="server" OnClick="Drop_lnkbtn_Click">Click to show DropDownList </asp:LinkButton>
</td>
</tr>
</table>
<asp:PlaceHolder ID="Place_Dropdown" runat="server">
<table width="500px" cellpadding="5" cellspacing="5" align="center">
<tr>
<td>
Face Color :
<asp:DropDownList ID="drop_downforecolor" 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 :
<asp:DropDownList ID="Backgroundcolor" 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="LabelText" runat="server" BackColor="AliceBlue" Text="Label" Font-Size="18px"></asp:Label>
</td>
</tr>
</table>
</asp:PlaceHolder>
</form>
</body>
</html>
Now add the following namespace.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
Color class
This is a base class for color values. By adding a reference to the System.Drawing to use the color class. You can use many known colors using the named properties of the Color type.
FromName Method