Repeater JS全选。单选。同适用于DataList

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html > <head runat="server"> <title></title> <mce:script type="text/javascript"><!-- function selectAll() { // 获得用户页面中的所有的 输入功能的控件getElementById("ChkSelect"). var checkbox = document.getElementsByTagName("input"); if (checkbox[0].checked == true) { for (var i = 0; i < checkbox.length; i++) checkbox[i].checked = false; } else { for (var i = 0; i < checkbox.length; i++) checkbox[i].checked = true; } } // --></mce:script> <mce:style type="text/css"><!-- /* CSS Document */ #mytable {width: 700px;padding: 0;margin: 0;} th { color: #4f6b72;border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;border-top: 1px solid #C1DAD7; letter-spacing: 2px;text-transform: uppercase;text-align: left;padding: 6px 6px 6px 12px;background: #CAE8EA no-repeat; } th.nobg {border-top: 0;border-left: 0;border-right: 1px solid #C1DAD7;background: none;} td { border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;background: #fff;font-size:11px; padding: 6px 6px 6px 12px;color: #4f6b72; } /*---------for IE 5.x bug*/ html>body td{ font-size:11px;} body,td,th { font-family: 宋体, Arial; font-size: 12px; } --></mce:style><style type="text/css" mce_bogus="1">/* CSS Document */ #mytable {width: 700px;padding: 0;margin: 0;} th { color: #4f6b72;border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;border-top: 1px solid #C1DAD7; letter-spacing: 2px;text-transform: uppercase;text-align: left;padding: 6px 6px 6px 12px;background: #CAE8EA no-repeat; } th.nobg {border-top: 0;border-left: 0;border-right: 1px solid #C1DAD7;background: none;} td { border-right: 1px solid #C1DAD7;border-bottom: 1px solid #C1DAD7;background: #fff;font-size:11px; padding: 6px 6px 6px 12px;color: #4f6b72; } /*---------for IE 5.x bug*/ html>body td{ font-size:11px;} body,td,th { font-family: 宋体, Arial; font-size: 12px; }</style> </head> <body> <form id="form1" runat="server"> <table id="mytable" border="0"> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <tr> <th scope="col"><input id="Checkbox1" type="checkbox" onclick='selectAll()' /></th> <th scope="col">Product</th> <th scope="col">Version</th> <th scope="col">Description</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td ><input type='checkbox' id='ChkSelect' runat="server" value='<%#Eval("ID")%>'/></td> <td ><%#Eval("Product")%></td> <td ><%#Eval("Version")%></td> <td ><%#Eval("Description")%></td> </tr> </ItemTemplate> </asp:Repeater> </table> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="测试选中的编号ID" /> </form> </body> </html>

using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Web.UI.HtmlControls; public partial class Repeater : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindTest(); } } public void BindTest() { DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32")); dc.AutoIncrement = true;//自动增加 dc.AutoIncrementSeed = 1;//起始为1 dc.AutoIncrementStep = 1;//步长为1 dc.AllowDBNull = false;// dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; newRow = tblDatas.NewRow(); newRow["Product"] = "大话西游"; newRow["Version"] = "2.0"; newRow["Description"] = "我很喜欢"; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); Repeater1.DataSource = tblDatas; Repeater1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { string s = ""; for (int i = 0; i < this.Repeater1.Items.Count; i++) { //客户端 HtmlInputCheckBox chb = (HtmlInputCheckBox)this.Repeater1.Items[i].FindControl("ChkSelect"); //CheckBox chb = (CheckBox)this.Repeater1.Items[i].FindControl("CheckBox2"); //服务器端 if (chb.Checked == true) { s = s + chb.Value;//chb.Text //服务器端 } } Response.Write(s); } }

你可能感兴趣的:(server,XHTML,object,服务器,input,button)