datalist 中的 CheckBoxList 如何判断当前选了几个选项 顶者有分 提供实例者可得50分

baiyashan 2008-05-13 11:11:22

我是要完成这样一件事,就是如果一个多选题限制最多只能选N个, 哪么当选题时超出N个时就不能在选择了.
我的checkboxList 是嵌套在datalist里面的.
...全文
349 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyin_liu 2008-05-16
  • 打赏
  • 举报
回复
GridView和CheckBox结合
http://blog.csdn.net/liyin_liu/archive/2008/05/08/2415145.aspx
LutzMark 2008-05-16
  • 打赏
  • 举报
回复
/// <summary>
/// 获取页面上已选择的复选框已选择数量
/// </summary>
/// <param name="dg">DataGrid实例</param>
/// <returns>返回一个int已选择复选框的数目</returns>
public static int GetCheckBoxNum(DataList dg)
{
int checkNum = 0;
int editIndex = 0;
for (int i = 0; i < dg.Items.Count; i++)
{
CheckBox chk = (CheckBox)dg.Items[i].Cells[0].Controls[1];
if (chk.Checked)
{
checkNum = checkNum + 1;
editIndex = i;
}
}
if (checkNum == 1)
{
return checkNum;
}
else if (checkNum == 0)
{
return -1;
}
else
{
return checkNum;
}
}
baiyashan 2008-05-16
  • 打赏
  • 举报
回复
amandag 同志.你用得 <asp:UpdatePanel /> 控件在我这不好用呀.你用得是 vs05吗?
GengWH 2008-05-14
  • 打赏
  • 举报
回复
for (int i= 0; i < CreateMyDataTable().Rows.Count; i++)
{
rdb_unreg_now = (System.Web.UI.WebControls.RadioButton)DataGrid1.Items[i].Cells[7].Controls[1];
chb_unreg_now1= (System.Web.UI.WebControls.CheckBox)DataGrid1.Items[i].Cells[8].Controls[1];
if(rdb_unreg_now.Checked==true)
{
chb_unreg_now1.Checked=true;
sel_zzid=DataGrid1.Items[i].Cells[0].Text.Trim();
lb_now =(System.Web.UI.WebControls.LinkButton)DataGrid1.Items[i].Cells[1].Controls[1];
zzxm=lb_now.Text.Trim();
}
}
philchang718 2008-05-14
  • 打赏
  • 举报
回复
 gi.ROLE_ID = new System.Collections.Generic.List<int>();
for (int i = 0; i < this.CheckBoxList1.Items.Count; i++)
{
if (this.CheckBoxList1.Items[i].Selected == true)
{
gi.ROLE_ID.Add(int.Parse(CheckBoxList1.Items[i].Value));
}
}
wxg22526451 2008-05-14
  • 打赏
  • 举报
回复
学习帮顶
Snobish 2008-05-14
  • 打赏
  • 举报
回复
jf
FishFlying1985 2008-05-14
  • 打赏
  • 举报
回复
<script>
function choose()
{
var inputs = document.all.tags("INPUT");
var temp = 0;
for (var i = 0; i < inputs.length; i++)
{
if (inputs[i].type == "checkbox" && inputs[i].id.indexOf("CheckBoxArea")>= 0 )
{
if(inputs[i].checked)
{
inputs[i].color = "blue";
temp ++;
}
else
{
inputs[i].parentElement.className = '';
}
}
if(temp == 12)
{
alert('你选择了所有的小区!') //控制选择的记录数目
return ;
}
}
}
</script>



利用脚本判断!!
amandag 2008-05-14
  • 打赏
  • 举报
回复
代码我已经写了,楼主有什么问题?
万小萌 2008-05-14
  • 打赏
  • 举报
回复
通过遍历一个一个判断是否被钩选,可以得到结果
baiyashan 2008-05-14
  • 打赏
  • 举报
回复
还没有决解呢....顶..
xiangyu5945 2008-05-14
  • 打赏
  • 举报
回复
ding xuexi
amandag 2008-05-13
  • 打赏
  • 举报
回复
....

不过看看代码应该查不多的..
baiyashan 2008-05-13
  • 打赏
  • 举报
回复
不好意思,我用得是reqeate


我是要完成这样一件事,就是如果一个多选题限制最多只能选N个, 哪么当选题时超出N个时就不能在选择了.
我的checkboxList 是嵌套在reqeate 里面的.
popeyepower 2008-05-13
  • 打赏
  • 举报
回复
客户端判断,服务端验证.
amandag 2008-05-13
  • 打赏
  • 举报
回复
用Ajax实现的版本

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListDemo.aspx.cs" Inherits="DataListDemo" %>

<!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">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:DataList ID="DataList1" runat="server" RepeatLayout="Flow">
<ItemTemplate>
<%#Eval("OrderID") %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False" RenderMode="Inline" UpdateMode="Conditional">
<ContentTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSource='<%# GetOrderDetails(Convert.ToInt32(Eval("OrderID"))) %>' DataTextField="UnitPrice" DataValueField="ProductID" RepeatLayout="Flow" RepeatDirection="Horizontal" AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:DataList>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="False" RenderMode="Inline" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>



private void BindDataList()
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=Northwind");
SqlDataAdapter da = new SqlDataAdapter("select top 10 orderid, CustomerID, OrderDate from Orders", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
DataList1.DataSource = ds.Tables[0].DefaultView;
DataList1.DataBind();
}

protected DataTable GetOrderDetails(int orderID)
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=;database=Northwind");
SqlDataAdapter da = new SqlDataAdapter("select ProductID, UnitPrice from [Order Details] where orderid = @orderid", cn);
da.SelectCommand.Parameters.AddWithValue("@orderid", orderID);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
return ds.Tables[0];
}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
CheckBoxList chk = sender as CheckBoxList;
int count = 0;
foreach (ListItem li in chk.Items)
{
if (li.Selected)
{
count++;
if (count >= 3)
{
Label1.Text = "Wrong";
UpdatePanel2.Update();
break;
}
else
{
Label1.Text = string.Empty;
UpdatePanel2.Update();
}
}
}
((UpdatePanel)((DataListItem)chk.NamingContainer).FindControl("UpdatePanel1")).Update();
}
amandag 2008-05-13
  • 打赏
  • 举报
回复
如果要在服务器端实现最好用Ajax..
lovehongyun 2008-05-13
  • 打赏
  • 举报
回复
如果用脚本检察datalist中的checkboxlist的话.挺麻烦的.

....
baiyashan 2008-05-13
  • 打赏
  • 举报
回复
这是一套问卷调查系统如果提交判断哪可能让做题的人不知哪的问题呀.
amandag 2008-05-13
  • 打赏
  • 举报
回复
最好是在提交的时候做检查,否则较为麻烦
加载更多回复(11)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧