62,046
社区成员
发帖
与我相关
我的任务
分享
/// <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;
}
}
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));
}
}
<%@ 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();
}