选择类型控件[控件虽然好学,用起来还是不是那么容易的~!]

选择类型控件
图形类型控件
属性:CssClass、ReadOnly、

1、panel控件用来盛放一些控件。如果设定GroupingText属性那么就渲染为含有<fieldset>的

div标签,也就是GroupBox效果,否则渲染为<div>
shift+F6 生成

选择控件,又称数据绑定控件
1.ListBox 类表框,控件提供的是单选或多重选择列表。通过修改Selectionmode属性来实现C

heckBoxList或者RadioButtonList控件的功能。当用户选择一项或多项时,将会激发

SelectedIndexChanged事件,默认情况下该事件不会导致页面会发,但是可以通过设置

AutoPostBack为true来强制立即回发。
2.DropDownList 下拉列表框,只能单项选择

编辑项: 打开ListItem集合编辑器,可以进行添加、删除
启用AutopostBack:它的值有True/Fals

添加
1.在ListItem集合编辑器里进行添加
2.使用listItem在源里进行添加 <asp:ListItem Value="1">芬达</asp:ListItem>
3.动态添加:
protected void Page_Load(object sender, EventArgs e)
        {
            ListItem li = new ListItem("公牛", "3");
            ListBox1.Items.Add(li);
        }

 


1.


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

namespace WebApplication1
{
    public partial class Panel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
            //ListItem li = new ListItem("公牛", "3");
            //ListBox1.Items.Add(li);
                //ListItem li = new ListItem("北京", "3");
                //DropDownList1.Items.Add(li);
               
              }
        }
        protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write(ListBox1.SelectedValue);
            Response.Write(ListBox1.SelectedItem.Text + ":(" +

ListBox1.SelectedIndex+ ")");
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs

e)
        {
            if (DropDownList1.SelectedItem.Text=="河北")
           {
                ListBox1.Items.Clear();
                ListItem li = new ListItem("涿州", "1");
                ListBox1.Items.Add(li);
                li = new ListItem("保定", "2");
                ListBox1.Items.Add(li);
           }
            else if (DropDownList1.SelectedItem.Text == "河南")
            {
                ListBox1.Items.Clear();
                ListItem li = new ListItem("郑州", "1");
                ListBox1.Items.Add(li);
                li = new ListItem("周口", "2");
                ListBox1.Items.Add(li);
            }
            else if (DropDownList1.SelectedItem.Text == "北京")
            {
                ListBox1.Items.Clear();
                ListItem li = new ListItem("东城", "1");
                ListBox1.Items.Add(li);
                li = new ListItem("西城", "2");
                ListBox1.Items.Add(li);
            }

        }
    }
}

源码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListBox.aspx.cs"

Inherits="WebApplication1.Panel" %>

<!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:ListBox ID="ListBox1" runat="server" Height="105px" Width="103px"
            onselectedindexchanged="ListBox1_SelectedIndexChanged">
        </asp:ListBox>
   
        <br />
   
    </div>
    <asp:DropDownList ID="DropDownList1" runat="server" Height="102px"
        Width="122px" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
        AutoPostBack="True">
    <asp:ListItem Value="1">河南</asp:ListItem>
            <asp:ListItem Value="2">河北</asp:ListItem>
             <asp:ListItem Value="3">北京</asp:ListItem>
   </asp:DropDownList>
    <p>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </p>
    </form>
</body>
</html>


2.


将SelectionMo属性设为Multipie
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Lianxi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack )
            {
            ListItem li = new ListItem("吴志远", "1");
            ListBox1.Items.Add(li);
            li = new ListItem("李硕", "2");
            ListBox1.Items.Add(li);
            li = new ListItem("姚志超", "3");
            ListBox1.Items.Add(li);
            li = new ListItem("张少丹", "4");
            ListBox1.Items.Add(li);
            li = new ListItem("耿宇丹", "5");
            ListBox1.Items.Add(li);
            }
         
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //不管选的是单选的还是多选的都可以实现
            foreach (ListItem li in ListBox1.Items)//遍历类表ListBox1的每一项
            {
                if(li.Selected)//如果li这listItem类型的类表项被选中
                {
                    ListBox2.Items.Add(li);//把它添加到listBox2的Items集合中
                }
            }
          
           
            }

        protected void Button2_Click(object sender, EventArgs e)
        {
            for (int i = this.ListBox2.Items.Count - 1; i >= 0;i-- )
            {
                if(ListBox2.Items[i].Selected)
                {
                    this.ListBox2.Items.RemoveAt(ListBox2.Items.IndexOf

(ListBox2.Items[i]));
                }
            }

        }

        }
    }

你可能感兴趣的:(选择类型控件[控件虽然好学,用起来还是不是那么容易的~!])