分享

DropDownList和ListBox下拉列表学习

 CoCO-Ebook 2013-11-04

 DropDownList控件

       DropDownList是一个下拉控件我们通常可以在设计视图里面给其添加我们预定的项,但是当这些项会动态变化时我们经常需要对其进行数据绑定。进行数据绑定的时候要注意先设定两个字段,DataTextField,DataValueField,他们的作用是设置我们要在下拉列表中显示的数据库的那个文字段,和该字段对应的主键。

        其常见属性有:

AutoPostBack属性:这个属性的用法在讲述基本控件的时候已经讲过,是用来设置当下拉列表项发生变化时是否主动向服务器提交整个表单,默认是false,即不主动提交。如果设置为true,就可以编写它的SelectedIndexChanged事件处理代码进行相关处理(注意:如果此属性为false即使编写了SelectedIndexChanged事件处理代码也不会马上起作用)。
DataTextField属性:设置列表项的可见部分的文字。
DataValueField属性:设置列表项的值部分。
Items属性:获取控件的列表项的集合。
SelectedIndex属性:获取或设置 DropDownList 控件中的选定项的索引注意是选定想到索引要和选定项对应的主键区别)。
SelectedItem属性:获取列表控件中索引最小的选定项(获取控件对应的选定项,有两个子属性SelectedItem.Text(对应的文字项)和SelectedItem.Value是对应的主键值和下面的SelectedValue一样)。
SelectedValue属性:取列表控件中选定项的值,或选择列表控件中包含指定值的项(选定想到主键值)。

ListBox控件

ListBox控件和DropDownList控件非常类似,ListBox控件是也是提供一组选项供用户选择的,只不过DropDownList控件只能有一个选项处于选中状态,并且每次只能显示一行(一个选项),而ListBox控件可以设置为允许多选,并且还可以设置为显示多行。

除了与DropDownList具有很多相似的属性之外,ListBox控件还有以下属性:

Rows属性:设置ListBox控件显示的行数。

SelectionMode属性:设置ListBox的选择模式,这是一个枚举值,它有Multiple和Single两个值,分别代表多选和单选,默认是Single,即同时只能有一个选项处于选中状态。如果要想实现多选,除了设置SelectionMode属性为Multiple外,在选择时需要按住Ctrl键。

需要说明的是,因为ListBox允许多选,所以如果ListBox的SelectionMode属性为Multiple,那么SelectedIndex属性指的是被选中的选项中索引最小的那一个,SelectedValue属性指的是被选中的选项集合中索引最小的那一个的值。

其他属性和DropDownList是一样的。

代码:

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;
using System.Data.SqlClient;
using MyPersonal.DBUtility;
public partial class Test : System.Web.UI.Page
{
   string sqlString="select * from dbo.Employees";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindData();
            BindData2();
            
        }
        this.DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
        this.ListBox1.SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
    }
    /// <summary>
    /// 当选中的时候把所有选中选都输出来
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

//遍历所有选中的项
        foreach (ListItem item in ListBox1.Items)
        {
            if (true==item.Selected)
            {

                Response.Write(item.Value);
                Response.Write(item.Text);
                
            }
        }
        //Response.Write(this.ListBox1.SelectedValue);
        //Response.Write(this.ListBox1.SelectedItem.Text);
        //throw new NotImplementedException();
    }

    void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //输出的项是选中项的主键字段
        Response.Write(this.DropDownList1.SelectedValue);
       //输出的是选中的项的文字项
        Response.Write(this.DropDownList1.SelectedItem.Text);
        //输出的是DropDownList的选中项的主键字段
        //输出的是DropDownList在ListItem的索引
        Response.Write(this.DropDownList1.SelectedIndex);
    }

    protected void BindData()
    {
        this.DropDownList1.DataTextField = "LastName";
        this.DropDownList1.DataValueField ="EmployeeID";
        SqlDataReader MyReader = SqlHelper.ExecuteReader(sqlString);
        this.DropDownList1.DataSource = MyReader;
        this.DropDownList1.DataBind();
        MyReader.Close();
    }


    protected void BindData2()
    {
        this.ListBox1.DataTextField = "LastName";
        this.ListBox1.DataValueField = "EmployeeID";
        SqlDataReader MyReader = SqlHelper.ExecuteReader(sqlString);
        this.ListBox1.DataSource=MyReader;
        this.ListBox1.DataBind();
        MyReader.Close();
    }
     
     
}

 

 

aspx文件代码:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www./1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
        </asp:DropDownList>
    </div>
    <div>
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" AutoPostBack="true"></asp:ListBox>
    </div>
    </form>
</body>
</html>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多