asp.net中DropDownList控件各种属性研究汇总

.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownList_demo.aspx.cs" Inherits="DropDownList_demo" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="DropDownList1" runat="server"<span style="color:#ff0000;"> AutoPostBack="True" </span>
           >
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server"  <span style="color:#ff0000;">AutoPostBack="true"</span>
            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
            <asp:ListItem Value="red">红色</asp:ListItem>
            <asp:ListItem Value="green">绿色</asp:ListItem>
            <asp:ListItem Value="black">黑色</asp:ListItem>
            <asp:ListItem Value="yellow">黄色</asp:ListItem>
        </asp:DropDownList>
    
        <asp:Button ID="Button1" runat="server" Text="第二个下拉列表框的索引号和标题" 
            οnclick="Button1_Click" />
    
        <asp:TextBox ID="TextBox1" runat="server" Width="373px"></asp:TextBox>
    
    </div>
    </form>
</body>
</html>
.aspx.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class DropDownList_demo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //将数组绑定到DropDownList中
        if (!IsPostBack) {
            ArrayList arrl = new ArrayList();
            arrl.Add("星期日");
            arrl.Add("星期一");
            arrl.Add("星期二");
            arrl.Add("星期三");
            arrl.Add("星期四");
            arrl.Add("星期五");
            arrl.Add("星期六");

            DropDownList1.DataSource = arrl;
            DropDownList1.DataBind();
           
        
        }

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string color = DropDownList2.SelectedItem.Value;//注意是显示的文本,要和case对应
        //后台控制前端显示的方法:DropDownList1.BackColor = System.Drawing.Color.Red;
        switch (color) { 
            case"红色":
                DropDownList1.BackColor = System.Drawing.Color.Red;
                break;
            case "绿色":
                DropDownList1.BackColor = System.Drawing.Color.Green;
                break;
            case "黑色":
                DropDownList1.BackColor = System.Drawing.Color.Black;
                break;
            case "黄色":
                DropDownList1.BackColor = System.Drawing.Color.Yellow;
                break;
            default:
                this.DropDownList1.BackColor = System.Drawing.Color.White;
                break;
        
        
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string index = DropDownList2.SelectedIndex.ToString();
        //value1和value2是获取的值
        string value1 = DropDownList2.SelectedValue;
        string value2 = DropDownList2.SelectedItem.Value;
        //text1和text2是获取的文本显示的值
        string text1 = DropDownList2.SelectedItem.Text; 
        string text3 = DropDownList2.SelectedItem.ToString();
        TextBox1.Text = "索引号为:" + index + ",值为:" + value1 + ",标题为:" + text1;
    }
}

效果如下图:

其他属性方法如图:



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值