CheckBoxList 的使用方法
2012-08-07 10:15
302 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="SWJ.Web.Admin.WebForm2" %> <!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:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="2" RepeatDirection="Horizontal" Width="34px"> </asp:CheckBoxList> <asp:Button ID="Button1" runat="server" Text="获取" onclick="Button1_Click" /> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="复选框赋值" /> <asp:Button ID="Button3" runat="server" Text="不能用" onclick="Button3_Click" /> <asp:Button ID="Button4" runat="server" Text="去除字段串的重复字符" onclick="Button4_Click" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace SWJ.Web.Admin { public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack)//第一次加载绑定数据 { DataTable dt = new DataTable(); DataColumn dc = null; dc = dt.Columns.Add("FwText", Type.GetType("System.String")); //列名 dc = dt.Columns.Add("FwValue", Type.GetType("System.String")); DataRow newRow=null; newRow = dt.NewRow(); //新行 newRow["FwText"] = "1"; newRow["FwValue"] = "1"; dt.Rows.Add(newRow); newRow = dt.NewRow(); //新行 newRow["FwText"] = "2"; newRow["FwValue"] = "2"; dt.Rows.Add(newRow); newRow = dt.NewRow(); //新行 newRow["FwText"] = "3"; newRow["FwValue"] = "3"; dt.Rows.Add(newRow); CheckBoxList1.DataSource = dt; CheckBoxList1.DataTextField = "FwText"; CheckBoxList1.DataValueField = "FwValue"; CheckBoxList1.DataBind(); } } /// <summary> /// 获得选择 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) Response.Write("你选的是" + CheckBoxList1.Items[i].Value + CheckBoxList1.Items[i].Text + "<br>"); } } /// <summary> /// 复选框赋值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button2_Click(object sender, EventArgs e) { string SelectStr = "1,3"; for (int i = 0; i < SelectStr.Split(',').Length; i++)//给CheckBoxList选中的复选框 赋值 { for (int j = 0; j < CheckBoxList1.Items.Count; j++) { if (SelectStr.Split(',')[i] == CheckBoxList1.Items[j].Value) { CheckBoxList1.Items[j].Selected = true; } } } } /// <summary> /// 设置不能用 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button3_Click(object sender, EventArgs e) { string SelectStr = "3"; for (int i = 0; i < SelectStr.Split(',').Length; i++) { for (int j = 0; j < CheckBoxList1.Items.Count; j++) { if (SelectStr.Split(',')[i] == CheckBoxList1.Items[j].Value) { CheckBoxList1.Items[j].Enabled = false; //选项不能用 } } } } /// <summary> /// 去除字段串的重复字符 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button4_Click(object sender, EventArgs e) { string stringArray = "aaa,bbb,aaa,ccc,bbb,ddd,ccc,aaa,bbb,ddd"; //List用于存储从数组里取出来的不相同的元素 List<string> listString = new List<string>(); for (int i = 0; i < stringArray.Split(',').Length; i++) { if (!listString.Contains(stringArray.Split(',')[i])) listString.Add(stringArray.Split(',')[i]); } //最后从List里取出各个字符串进行操作 foreach (string eachString in listString) { Response.Write(eachString); //打印每个字符串 } } } }
相关文章推荐
- 使用JavaScript实现CheckBoxList单选两种方法
- ASP.NET中CheckBoxList复选框列表控件详细使用方法
- ASP.NET中CheckBoxList复选框列表控件详细使用方法
- ReactNative之FlatList的具体使用方法
- List 的subList()方法使用注意事项
- VC++中List Control控件的使用方法介绍
- CSS: list-style 和 inline使用方法详解
- java.util.List接口的方法subList()的使用注意事项
- javascript中checkbox使用方法简单实例演示
- springMVC中使用List和Map绑定对象作为参数的使用方法
- Apache下的FileUtils.listFiles方法简单使用技巧
- jquery 中$.post获取MVC Controller中JsonResult返回包含LIst<Model>类型的子List<Model>的高级使用方法
- List对象remove方法的使用
- 理解可变参数va_list、va_start、va_arg、va_end原理及使用方法
- java.util.List里的subList方法使用
- WPF datagrid 中 DataGridComboBoxColumn 和 DataGridCheckBoxColumn 使用方法
- CheckBox 多选按钮的使用方法
- Java asList方法使用
- 使用 ResultSet 遍历查询结果(返回list结果集,重新toString()方法)
- CMake 使用方法 & CMakeList.txt<转>