All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
asp.net2.0服务器控件之DropDownList控件

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/9 1:37:32 Browse times: 387 Comment times: 0

asp.net2.0服务器控件之DropDownList控件


Head photo

Go homepage
Upload pictures
Write articles

asp.net2.0服务器控件之DropDownList控件

DropDownList控件
1.功能
DropDownList 控件与ListBox控件的使用方法类似,但DropDownList 控件只允许用户每次从列表中选择一项的下拉列表控件。DropDownList 控件只在框中显示选定项,同时还显示下拉按钮。当用户单击此按钮时,将显示项的列表。
2.属性
DropDownList控件常用属性及描述如表1所示。

表1 DropDownList控件常用属性及描述
DropDownList控件的属性大部分与ListBox控件相同,这里只介绍一下DataSource属性。
通过使用DataSource属性可以从数组或集合中获取列表项并将其添加到控件中。当编程人员希望从数组或集合中填充列表时,可以使用此属性。
语法:
object1.DataSource[= object2]
参数说明如下。
object1:对象表达式。
object2:对象表达式,其值是被添加到列表中的数据源对象。
例如,先创建一个数组arrList,代码如下:
ArrayList arrList = new ArrayList();
arrList.Add("red");
arrList.Add("blue");
arrList.Add("green");
arrList.Add("yellow");
使用DataSource属性将该数组附加到要填充的DropDownList控件中,代码如下:
this.ddlTest.DataSource = arrList;
3.方法
DropDownList控件最常用方法及描述如表2所示。

表2 DropDownList控件常用方法及描述
DataBind方法是在DropDownList控件使用DataSource属性附加了数据源后,将数据源绑定到被调用到DropDownList控件。
语法:
object. DataBind([bool]);
参数说明如下。
object:必需的,是一个对象表达式。
bool:可选的,是一个布尔表达式,它指定绑定数据源后是否引发DataBinding事件。
例如,将上例中的arrList数组绑定到DropDownList控件,代码如下:
this.ddlTest.DataBind();
4.事件
DropDownList控件常用事件及描述如表3所示。

表3 DropDownList控件常用事件及描述
DropDownList的事件与ListBox的事件十分相似,这里仅对SelectedIndexChanged事件作详细介绍。
与ListBox控件相同,当DropDownList控件的选项在向服务器发送过程中更改时,将引发 SelectedIndexChanged 事件。
* 示例
DropDownList控件SelectedIndexChanged事件的使用
本示例演示了在选取列表框中的某项时,将触发DropDownList控件SelectedIndex Changed事件,从而改变DropDownList控件的背景色。程序代码如下:
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
{
string color=this.ddlTest .SelectedValue ;
if(color =="red")
this.ddlTest.BackColor=System .Drawing .Color.Red ;
if (color == "blue")
this.ddlTest.BackColor = System.Drawing.Color.Blue;
if (color == "green")
this.ddlTest.BackColor = System.Drawing.Color.Green ;
if (color == "yellow")
this.ddlTest.BackColor = System.Drawing.Color.Yellow;
}
运行结果如图1所示

图1 SelectedIndexChanged事件示例运行结果
完整程序代码如下:
★ ★★★★DropDownListTest.aspx代码文件完整程序代码★★★★★
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownListTest.aspx.cs" Inherits="DropDownListTest" %>



DropDownListTest








There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.