Repeater控件使用CheckBox
一、使用客户端CheckBox
<asp:Repeater runat=server ID=repeater1 DataSourceID="SqlDataSource1">
<ItemTemplate>
<table width=400>
<tr>
<td>
<input id="Checkbox1" name="Checkbox1" type="checkbox" value='<%#Eval("ProductID")%>' /></td>
<td><asp:Label runat=server Text='<%#Eval("ProductName")%>' ></asp:Label></td>
<td></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="select top 20 * from [products]"></asp:SqlDataSource>
<asp:Button ID="ButtonSubmit" runat="server" onclick="ButtonSubmit_Click"
Text="提交" />
<ItemTemplate>
<table width=400>
<tr>
<td>
<input id="Checkbox1" name="Checkbox1" type="checkbox" value='<%#Eval("ProductID")%>' /></td>
<td><asp:Label runat=server Text='<%#Eval("ProductName")%>' ></asp:Label></td>
<td></td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="select top 20 * from [products]"></asp:SqlDataSource>
<asp:Button ID="ButtonSubmit" runat="server" onclick="ButtonSubmit_Click"
Text="提交" />
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
if (Request.Form["Checkbox1"] != null)
{
string s = Request.Form["Checkbox1"].ToString();
Response.Write(s);
}
}
{
if (Request.Form["Checkbox1"] != null)
{
string s = Request.Form["Checkbox1"].ToString();
Response.Write(s);
}
}
二、使用服务器端CheckBox
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeater.aspx.cs" Inherits="repeater" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width=400>
<asp:Repeater runat=server ID=Repeater1 DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="CheckBox2" runat="server" />
<asp:Label ID="LabelHiddenID" runat="server" Text='<%#Eval("ProductID")%>' Visible=false></asp:Label>
</td>
<td><asp:Label ID="Label1" runat=server Text='<%#Eval("ProductName")%>' ></asp:Label></td>
<td></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="select top 20 * from [products]"></asp:SqlDataSource>
<asp:Button ID="ButtonSubmit" runat="server" onclick="ButtonSubmit_Click"
Text="提交" />
</div>
</form>
</body>
</html>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width=400>
<asp:Repeater runat=server ID=Repeater1 DataSourceID="SqlDataSource1">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="CheckBox2" runat="server" />
<asp:Label ID="LabelHiddenID" runat="server" Text='<%#Eval("ProductID")%>' Visible=false></asp:Label>
</td>
<td><asp:Label ID="Label1" runat=server Text='<%#Eval("ProductName")%>' ></asp:Label></td>
<td></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="select top 20 * from [products]"></asp:SqlDataSource>
<asp:Button ID="ButtonSubmit" runat="server" onclick="ButtonSubmit_Click"
Text="提交" />
</div>
</form>
</body>
</html>
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
List<string> IDlist = new List<string>();
foreach (RepeaterItem item in this.Repeater1.Items)
{
Control ctrl = item.FindControl("CheckBox2");
Control ctrlHide = item.FindControl("LabelHiddenID");
CheckBox ck = ctrl as CheckBox;
Label lb = ctrlHide as Label;
if (ck != null && lb != null)
{
if (ck.Checked)
{
IDlist.Add(lb.Text);
}
}
}
//取出IDlist中的ID值
string IDs = "";
foreach (string s in IDlist)
{
IDs += s + " ";
}
Response.Write(IDs);
}
{
List<string> IDlist = new List<string>();
foreach (RepeaterItem item in this.Repeater1.Items)
{
Control ctrl = item.FindControl("CheckBox2");
Control ctrlHide = item.FindControl("LabelHiddenID");
CheckBox ck = ctrl as CheckBox;
Label lb = ctrlHide as Label;
if (ck != null && lb != null)
{
if (ck.Checked)
{
IDlist.Add(lb.Text);
}
}
}
//取出IDlist中的ID值
string IDs = "";
foreach (string s in IDlist)
{
IDs += s + " ";
}
Response.Write(IDs);
}
本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利
This posting is provided "AS IS" with no warranties, and confers no rights.
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号