Asp. NET program to achieve a three-color alternating drop-down list box

Source: Internet
Author: User
Asp.net| Program | dropdown | The drop-down list shows a drop-down box with alternating three colors, as shown in the following illustration.


  


  


Implementation method:


1. Use the System.Web.UI.HtmlControls control to run it as a server control.


2. Binds the data to this select control in the background.


3. Then call a write-yourself function to specify the color for its items.


Implementation code (main implementation part):


Front Desk (WebForm1.aspx):


<select id= "Select1" style= "width:300px" name= "Select1" runat= "Server" >


<option selected></option>


</SELECT>


Backstage (WebForm1.aspx.cs):


private void Page_Load (object sender, System.EventArgs e)


  {


if (! IsPostBack)


  {


SqlConnection cnn = new SqlConnection ();


CNN. ConnectionString = "Data source=localhost;initial catalog=northwind;password=;"


+ "Persist security info=true;user id=sa;workstation id=apj062;packet size=4096";


string sqlstr = "Select Top CustomerID, CompanyName from Customers";


CNN. Open ();


SqlDataAdapter ad = new SqlDataAdapter (SQLSTR,CNN);


DataTable dt = new DataTable ();


AD. Fill (DT);


select1.datasource = DT;


Select1.datatextfield = "CompanyName";


Select1.datavaluefield = "CustomerID";


Select1.databind ();


Bindselectcolor (SELECT1);


  }


  }


//Add a style to a project for a select control


void Bindselectcolor (HtmlSelect Select)


  {


int num = 0;


string stylestring = "";


for (int i = 0; i < SELECT. Items.Count; i + +)


  {


num = i% 3;


switch (num)


  {


Case 0:


  {


stylestring = "COLOR: #000099; Background-color: #F4FAFF ";


break;


  }


Case 1:


  {


stylestring = "COLOR: #990000; Background-color: #FFFAFB ";


break;


  }


Case 2:


  {


stylestring = "COLOR: #009900; Background-color: #F4FFFA ";


break;


  }


  }


Select. Items[i]. Attributes.Add ("style", stylestring);


  }


  }





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.