本人菜鸟,在后台拼了一个checkbox,但是想在前台实现删除和恢复删除,请大侠赐教,急急急,在线等,解决了30分
拼的代码 public string EasyMessary()
{
string EasyMessary = hideEasyMessary.Value;
string []arrMessary=EasyMessary.Split(',');
DataSet ds = nmbll.AllUseCheckboxDal();
StringBuilder str = new StringBuilder();
if (ds.Tables[0].Rows.Count > 0)
{
str.Append("<table><tr>");
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (arrMessary.Length> 0)
{
int count = 0;
for (int j = 0; j < arrMessary.Length; j++)
{
if (arrMessary[j]==ds.Tables[0].Rows[i]["MeasureID"].ToString())
{
count++;
str.Append("<td><input type='checkbox' checked='checked' ID='check' runat='server' value=" + ds.Tables[0].Rows[i]["MeasureID"].ToString() + " />" + ds.Tables[0].Rows[i]["MeasureName"].ToString() + "</td>");
break;
}
}
if (count==0)
{
str.Append("<td><input type='checkbox' onclick='Check('a','b')' ID='check' runat='server' value=" + ds.Tables[0].Rows[i]["MeasureID"].ToString() + " />" + ds.Tables[0].Rows[i]["MeasureName"].ToString() + "</td>");
if (i%2==0&&i!=0)
{
str.Append("<br/>");
}
}
}
}
str.Append("</tr></table>");
}
return str.ToString();
}
在前台怎么获取checkbox,然后进行删除和恢复删除(勾上没有被删,除掉勾即被删除)
[解决办法]
你先把下面的代码,保存为 a.html , 再运行看一下。
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
//------------- 1. jQuery ----------------------
//删除
function delAll(){
var $chkTrs = $("#tbodyList :checked").parents("tr");
if($chkTrs.length==0){
alert("请选择记录!");
return;
}
$chkTrs.remove();
}
//选择
function chkAll(obj){
//注: jQuery 1.6 之后应该用 prop 而不是用 attr 来控制属性checked
$("#tbodyList :checkbox").prop("checked",obj.checked);
}
//------------- 2. js ----------------------
//删除
function delAll_js(){
var chks = document.getElementById("tbodyList").getElementsByTagName("input");
var trArr = [];
for(var i=0, len = chks.length; i<len;i++){
var chk = chks[i];
if(chks[i].checked){
trArr.push(chks[i].parentNode.parentNode);
}
}
if(trArr.length==0){
alert("请选择记录!");
return;
}
for(var i=0,len=trArr.length;i<len;i++){
trArr[i].parentNode.removeChild(trArr[i]);
}
}
//选择
function chkAll_js(obj){
var chks = document.getElementById("tbodyList").getElementsByTagName("input");
for(var i=0, len = chks.length; i<len;i++){
chks[i].checked = obj.checked;
}
}
</script>
</head>
<body>
<input type="button" onclick="delAll_js()" value="删除" />
<table >
<thead>
<tr><th><input type="checkbox" onclick="chkAll_js(this)" /></th><th>Name</th></tr>
</thead>
<tbody id="tbodyList" >
<tr><td><input type="checkbox" /></td><td>Tom</td></tr>
<tr><td><input type="checkbox" /></td><td>Jack</td></tr>
<tr><td><input type="checkbox" /></td><td>Andy</td></tr>
<tr><td><input type="checkbox" /></td><td>Merry</td></tr>
</tbody>
</table>
</body>
</html>
<div id="div_test" runat="server">
</div>
CheckBox myCheckBox;
for (int i = 0; i < 20; i++)
{
myCheckBox=new myCheckBox();
myCheckBox.ID="cbo_check"+i.ToString();
//这里myCheckBox属性和方法随你定义啦
//........
//myCheckBox定义完毕后
把CheckBox添加到前台的DIV中就OK了!
this.div_test.Controls.Add(myCheckBox)
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" onclick="delFlag()" value="标识删除" />
<input type="button" onclick="delRestore()" value="恢复删除" />
<input type="button" onclick="del_Actual()" value="真正删除" />
<%= this.chkHtml %>
</div>
</form>
</body>
<script type="text/javascript">
//标识删除, 仅作标识,再隐藏
function delFlag(){
$("#tableList :checked").parents("tr").attr("delFlag","1").hide();
}
//恢复删除。将标识删除了的记录给恢复
function delRestore(){
$("#tableList tr[delFlag=1]").remove("delFlag").show();
}
//真正删除
function del_Actual(){
var idArr = [];
var $delTrs =$("#tableList tr[delFlag=1]");
idArr = $delTrs.find(":checkbox").map(function(){ return this.value; }).get();
//页面上移除
$delTrs.remove();
alert("取到的要删除的checkbox的值:"+idArr);
//后台删除:你可以用jQuery请求一个页面或者一般处理程序来做后台的删除
$.post("xxx.ashx?ids="+idArr, function(){});
}
</script>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
public string chkHtml = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BuildHtml();
}
}
private void BuildHtml()
{
StringBuilder sb = new StringBuilder();
sb.Append("<table id='tableList' >");
sb.Append("<tr><td><input type='checkbox' id='MeasureID1' checked='checked' value='MeasureID1' />MeasureName1</td></tr>");
sb.Append("<tr><td><input type='checkbox' id='MeasureID2' checked='checked' value='MeasureID2' />MeasureName2</td></tr>");
sb.Append("<tr><td><input type='checkbox' id='MeasureID3' checked='checked' value='MeasureID3' />MeasureName2</td></tr>");
sb.Append("</table>");
chkHtml = sb.ToString();
}
}