CheckBoxList设置为单选 (含js和不用js两者方法实现CheckBoxList单选)
给添加Javascript脚本
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" language="javascript">
//竖排
function CheckSelect()
{
//alert("123");
var tb=document.getElementById("CheckBoxList1");
for(var i=0;i<tb.rows.length;i++)
{
var chk = tb.rows[i].firstChild.firstChild;
if(chk != event.srcElement)
{
chk.checked=false;
}
}
}
//指定横向
function CheckSelect1()
{
var tb = document.getElementById("CheckBoxList1");
for(var i=0;i < tb.rows.length;i++)
{
for(var j =0; j < tb.rows[i].cells.length; j++)
{
var chk = tb.rows[i].cells[j].firstChild;
if(chk!= null && chk != event.srcElement)
{
chk.checked = false;
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True" onclick="CheckSelect()">
<asp:ListItem>First</asp:ListItem>
<asp:ListItem>Second</asp:ListItem>
<asp:ListItem>Three</asp:ListItem>
</asp:CheckBoxList>
</div>
</form>
</body>
</html>
如果不用JavaScript脚本,直接在后台代码实现也可以:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//先遍历CheckBoxList 取出只有1个选项时候的index
int iCount=0;int iTemp=0;
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
iCount++;
iTemp=i;
}
}
if (iCount == 1)
{
ViewState["oldSelect"] = iTemp;
}
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!CheckBox1.Checked)
{
int iCount = 0;
int iTemp = (Int32)ViewState["oldSelect"];
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
iCount++;
如果不用JavaScript脚本,直接在后台代码实现也可以: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //先遍历CheckBoxList 取出只有1个选项时候的index int iCount=0;int iTemp=0; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { iCount++; iTemp=i; } } if (iCount == 1) { ViewState["oldSelect"] = iTemp; } } protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) { if (!CheckBox1.Checked) { int iCount = 0; int iTemp = (Int32)ViewState["oldSelect"]; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { iCount++; if (i != iTemp) { ViewState["newSelect"] = i; } } } if (iCount > 1)//点下第二个 { int iTempOld = (Int32)ViewState["oldSelect"]; int iTempNew = (Int32)ViewState["newSelect"]; CheckBoxList1.Items[iTempOld].Selected = false; CheckBoxList1.Items[iTempNew].Selected = true; ViewState["oldSelect"] = ViewState["newSelect"]; } } } }
如果不用JavaScript脚本,直接在后台代码实现也可以: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //先遍历CheckBoxList 取出只有1个选项时候的index int iCount=0;int iTemp=0; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { iCount++; iTemp=i; } } if (iCount == 1) { ViewState["oldSelect"] = iTemp; } } protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) { if (!CheckBox1.Checked) { int iCount = 0; int iTemp = (Int32)ViewState["oldSelect"]; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { iCount++; if (i != iTemp) { ViewState["newSelect"] = i; } } } if (iCount > 1)//点下第二个 { int iTempOld = (Int32)ViewState["oldSelect"]; int iTempNew = (Int32)ViewState["newSelect"]; CheckBoxList1.Items[iTempOld].Selected = false; CheckBoxList1.Items[iTempNew].Selected = true; ViewState["oldSelect"] = ViewState["newSelect"]; } } } }
如果不用JavaScript脚本,直接在后台代码实现也可以: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //先遍历CheckBoxList 取出只有1个选项时候的index int iCount=0;int iTemp=0; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { iCount++; iTemp=i; } } if (iCount == 1) { ViewState["oldSelect"] = iTemp; } } protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) { if (!CheckBox1.Checked) { int iCount = 0; int iTemp = (Int32)ViewState["oldSelect"]; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { iCount++; if (i != iTemp) { ViewState["newSelect"] = i; } } } if (iCount > 1)//点下第二个 { int iTempOld = (Int32)ViewState["oldSelect"]; int iTempNew = (Int32)ViewState["newSelect"]; CheckBoxList1.Items[iTempOld].Selected = false; CheckBoxList1.Items[iTempNew].Selected = true; ViewState["oldSelect"] = ViewState["newSelect"]; } } } }
if (i != iTemp)
{
ViewState["newSelect"] = i;
}
}
}
if (iCount > 1)//点下第二个
{
int iTempOld = (Int32)ViewState["oldSelect"];
int iTempNew = (Int32)ViewState["newSelect"];
CheckBoxList1.Items[iTempOld].Selected = false;
CheckBoxList1.Items[iTempNew].Selected = true;
ViewState["oldSelect"] = ViewState["newSelect"];
}
}
}
}
posted on 2012-02-28 10:41 Edward Yang 阅读(527) 评论(1) 编辑 收藏 举报