Thursday, 23 August 2012

How To Add All Color Name in DropDownList and change background Color


How To Add All Color Name in DropDownList and change background Color
Page ASPX Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="change page color.aspx.cs" Inherits="change_page_color" %>

<!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>Untitled Page</title>
</head>
<body id="pgbody" runat ="server" >
    <form id="form1" runat="server">
   <center >
    <div id="emp" style ="vertical-align :middle ;">
    <table style ="vertical-align :middle ;" >
  
   
    <tr>
    <td>Select Color :-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
    <td>
   
   
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
        onselectedindexchanged="DropDownList1_SelectedIndexChanged" Height="22px"
            Width="216px">
    </asp:DropDownList>
                    </td>
   
    </tr>
   
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
   
    </tr>
    </table>
    </div>
    </center>
    </form>
</body>
</html>

C# Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ccolor = System.Drawing.Color;
using System.Data.SqlClient;
using System.Drawing;

public partial class change_page_color : System.Web.UI.Page
{
       protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
                 
       
            string[] clrNm = System.Enum.GetNames(typeof(System.Drawing.KnownColor));

            DropDownList1.DataSource = clrNm;
            DropDownList1.DataBind();
       
          
        }
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        pgbody.Style.Add("background-color", DropDownList1.SelectedItem.Text);
     this.form1.Style.Add("background-color", DropDownList1.SelectedItem.Text);
      
    }


}

0 comments: