为什么会选一次追加一次?
aspx代码:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Height="38px" Width="127px"
AutoPostBack="True" Font-Bold="True" ForeColor="Black"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
</form>
</body>
</html>
aspx.cs代码
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;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DropDownList1.Items.Add("Red");
DropDownList1.Items.Add("Green");
DropDownList1.Items.Add("Blue");
DropDownList1.Items.Add("LightGray");
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string color = this.DropDownList1.SelectedItem.Value;
switch (color)
{
case "Red":
this.DropDownList1.BackColor = System.Drawing.Color.Red;
break;
case "Green":
this.DropDownList1.BackColor = System.Drawing.Color.Green;
break;
case "Blue":
this.DropDownList1.BackColor = System.Drawing.Color.Blue;
break;
case "LightGray":
this.DropDownList1.BackColor = System.Drawing.Color.LightGray;
break;
default:
this.DropDownList1.BackColor = System.Drawing.Color.White;
break;
}
}
}