<div id="containerCenter" style="">
<%=ViewCenter %>
</div>
------------------------
//选择待定日志
protected void Button1_Click(object sender, EventArgs e)
{
string returnvalue = "";
returnvalue = "<table style='vertical-align:top;' id='center'>";
foreach (DataListItem dli in this.DataList1.Items)
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)dli.FindControl("ck");
if (cb.Checked)
{
int value = Convert.ToInt32(cb.Value);
int uid = 255;//user.uid();
string sql = string.Format("select * from tb_log where uid={0} and id={1}", uid, value);
DataAccess dataAccess = new DataAccess();
SqlDataReader dr = dataAccess.GetDataReader(sql);
if (dr.Read())
{
returnvalue += "<tr><td>";
returnvalue += "<input type='checkbox' runat ='server' id='centerck' value=" + dr["id"] + "/>";
returnvalue += dr["ibblog1"];
returnvalue += "</td></tr>";
}
}
}
returnvalue += "</table>";
ViewCenter = returnvalue;
}
---------------------------------
//选定的日志 protected void Button6_Click(object sender, EventArgs e)
{
string returnvalue = "";
for (int i=0; i < 7; i++)
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)FindControl("centerck");
//我想逐个获得id='centerck' 的值,获得不了???
if (cb.Checked)
{
Response.Write("aaa");
}
}
}
<%=ViewCenter %>
</div>
------------------------
//选择待定日志
protected void Button1_Click(object sender, EventArgs e)
{
string returnvalue = "";
returnvalue = "<table style='vertical-align:top;' id='center'>";
foreach (DataListItem dli in this.DataList1.Items)
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)dli.FindControl("ck");
if (cb.Checked)
{
int value = Convert.ToInt32(cb.Value);
int uid = 255;//user.uid();
string sql = string.Format("select * from tb_log where uid={0} and id={1}", uid, value);
DataAccess dataAccess = new DataAccess();
SqlDataReader dr = dataAccess.GetDataReader(sql);
if (dr.Read())
{
returnvalue += "<tr><td>";
returnvalue += "<input type='checkbox' runat ='server' id='centerck' value=" + dr["id"] + "/>";
returnvalue += dr["ibblog1"];
returnvalue += "</td></tr>";
}
}
}
returnvalue += "</table>";
ViewCenter = returnvalue;
}
---------------------------------
//选定的日志 protected void Button6_Click(object sender, EventArgs e)
{
string returnvalue = "";
for (int i=0; i < 7; i++)
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)FindControl("centerck");
//我想逐个获得id='centerck' 的值,获得不了???
if (cb.Checked)
{
Response.Write("aaa");
}
}
}
CheckBox cb = dli.FindControl("ck") as CheckBox
不要用ChckBox, 这个只能得到on或者off这两个值用CheckBoxList控件可以得到值如:(用逗号分割多个值)string valuestr="";
for(int i=0;i<checkboxlist.items.count;i++)
{
if(checkboxlist.items.checked=true)
{
valuestr+=checkboxlist.items[i].value.tostring()+",";
}
}
JS
====================================================
function allcheck(input1,input2)
{
document.getElementById("check_ids").value = "";
var num=0;
var objForm = document.forms[input1];
var objLen = objForm.length;
for (var iCount = 0; iCount < objLen; iCount++)
{
if (input2.checked == true)
{
if (objForm.elements[iCount].type == "checkbox")
{
objForm.elements[iCount].checked = true;
num++;
if (num>1)
{
if(objForm[iCount].type=="checkbox")
{
document.getElementById("check_ids").value = document.getElementById("check_ids").value + objForm[iCount].value+",";
}
}
}
}
else
{
if (objForm.elements[iCount].type == "checkbox")
{
objForm.elements[iCount].checked = false;
num--;
if (num<-1)
{
document.getElementById("check_ids").value = "";
}
}
}
}
}
===========================================================================
页面隐藏域和单击事件
====================================
<input type="hidden" id="check_ids" name="check_ids"/><input type="checkbox" name="all" id="all" onclick='allcheck("你的页面的from名字",this)' style="width:20px;height:20px;" />===================================
后台代码取值并且去掉多余的逗号
=============================
String deleteid = Request.Form["check_ids"].TrimEnd(',');能实现全选并且取到所有checkbox的值
既然能取出来所有的id那么在判断一下id是'centerck' 的不就行了么