您的位置:首页 > 其它

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); //打印每个字符串
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: