注册 登录
编程论坛 ASP.NET技术论坛

为什么会选一次追加一次?

Teem 发布于 2013-07-30 15:43, 655 次点击
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;

        }
    }
}
3 回复
#2
shangsharon2013-07-30 23:53
DropDownList1.Items.Add("Red");
        DropDownList1.Items.Add("Green");
        DropDownList1.Items.Add("Blue");
        DropDownList1.Items.Add("LightGray");

!IsPostBack
#3
飞年合2013-08-06 14:04
if (!IsPostBack)
{
        DropDownList1.Items.Add("Red");
        DropDownList1.Items.Add("Green");
        DropDownList1.Items.Add("Blue");
        DropDownList1.Items.Add("LightGray");

}
#4
zhan108441202013-08-19 13:58
摇钱树网吧管理软件经过四十多个版本升级,目前国内外2~3万家用户使用中,
稳定 安全性强!24小时免费技术支持
1