标签:foreach uefi checkbox form charset partial sel edit default
复合控件:
多选框:checkbox - CheckBoxList
属性:RepeatLayout:Table 按表格排布 Flow流排布
RepeatDirection:Vertical 垂直排布 Horizontal 水平排布
RepeatColumns:每一行有几列 流式布局的时候使用
单选
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:CheckBoxList ID="CheckBoxList1" runat="server"><%--里面的每一项都是一个ListItem --%> <asp:ListItem Value="N001">汉族</asp:ListItem> <asp:ListItem Value="N002">满族</asp:ListItem> <asp:ListItem Value="N003">藏族</asp:ListItem> <asp:ListItem Value="N004">彝族</asp:ListItem> </asp:CheckBoxList> <asp:Button ID="Button1" runat="server" Text="Button" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; } private void Button1_Click(object sender, EventArgs e) { if(CheckBoxList1.SelectedItem!=null)//2、选中的SelectedItem不等于空的时候才执行 { Label1.Text = CheckBoxList1.SelectedItem.Value;//1、获取复选框选中的Vlue也可以选中的Text。如果什么也没选中获取的话会报错所以执行2 } } }
多选 默认选中项
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; if (! IsPostBack)//8、只有页面第一次加载才回走里面的代码。事件的委托要放在外面 { List<Users> ulist = new UsersData().SelectAll();//1、将数据的内容返回个ulist foreach (Users u in ulist)//2、循环遍历ulist { ListItem li = new ListItem(u.NickName, u.UserName);//括号里的第一个值为Text,第二个值为Valus if (u.UserName == "2589")//9、默认选中项 { li.Selected = true; } CheckBoxList1.Items.Add(li); } } } private void Button1_Click(object sender, EventArgs e) { //7、获取的话会在重新加载下数据库的内容所以执行8 if (CheckBoxList1.SelectedItem != null)//3、选中的SelectedItem不等于空的时候才执行如果等于空的时候回报错 { string s = ""; foreach (ListItem li in CheckBoxList1.Items) //4、循环遍历一下CheckBoxList1里的集合所以用Items类型ListItem { if (li.Selected)//5、获取选中的值 { s += li.Text + ",";//6将选中的项的Tect给上面的s } } } } }
多选无默认值
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Button1.Click += Button1_Click; if (!IsPostBack)//8、只有页面第一次加载才回走里面的代码。事件的委托要放在外面 { List<Users> ulist = new UsersData().SelectAll();//1、将数据的内容返回个ulist CheckBoxList1.DataSource = ulist;//2、ulist置顶到CheckBoxList1 CheckBoxList1.DataTextField = "NickName";//显示数据库里NickName列显示相当于Text CheckBoxList1.DataValueField = "Ucode";//隐藏数据库里的Ucode列隐藏相当于Value CheckBoxList1.DataBind();//CheckBoxList1绑定数据路 } } private void Button1_Click(object sender, EventArgs e) { //7、获取的话会在重新加载下数据库的内容所以执行8 if (CheckBoxList1.SelectedItem != null)//3、选中的SelectedItem不等于空的时候才执行如果等于空的时候回报错 { string s = ""; foreach (ListItem li in CheckBoxList1.Items) //4、循环遍历一下CheckBoxList1里的集合所以用Items类型ListItem { if (li.Selected)//5、获取选中的值 { s += li.Text + ",";//6将选中的项的Tect给上面的s } } } } }
单选框
radio - RadioButtonList
取值和复制和上面一样
设置默认选中项:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { RadioButtonList1.DataSource = new UsersData().SelectAll(); RadioButtonList1.DataTextField = "NickName"; RadioButtonList1.DataValueField = "Ucode"; RadioButtonList1.DataBind(); RadioButtonList1.SelectedIndex = 0;//默认选中索引为0的项 } } }
下拉菜单
select - DropDownList
单选
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DropDownList1.Items.Add(new ListItem("===请选择===","-1"));//在网页中不显示,下面自带清空,显示的话属性AppendDataBoundItems="True"显示 DropDownList1.DataSource = new UsersData().SelectAll(); DropDownList1.DataTextField = "NickName"; DropDownList1.DataValueField = "Ucode"; DropDownList1.DataBind(); } } }
多选
**AutoPostBack 相当于点击事件
标签:foreach uefi checkbox form charset partial sel edit default
原文地址:http://www.cnblogs.com/skyhorseyk/p/7294034.html