ASP.NET+ashx+jQuery动态添加删除表格

时间:2021-11-13 02:51:50

aspx:

<script src="../script/jquery-1.4.4.min.js" type="text/javascript" language="javascript"></script>
<script language="javascript" type="text/javascript">
var proID = <%=id %>;
var flag = 'True'; $(function(){
readStage();
}); function readStage(){
$.get(
'readstage.ashx?ajax=true&random=' + Math.random(),{'proID':proID},
function(result){
// var result = [
// {"StageName":"Brett","ProName":"McLaughlin","ID":"1"},
// {"StageName":"Jason","ProName":"Hunter","ID":"2"},
// {"StageName":"Elliotte","ProName":"Harold","ID":"3"}
// ];
addnewRow(eval(result));
}
);
} function addnewRow(values){
clearRow();
if(values.length == ){
var newTr = stageTable.insertRow();
var newTd1 = newTr.insertCell();
newTd1.setAttribute("colSpan",);
newTd1.innerHTML = '暂无数据记录...';
} if(flag == 'True'){
for(var i = ; i < values.length; i++){
//新行
var newTr = stageTable.insertRow(i+);
//序号
var newTd1 = newTr.insertCell();
newTd1.innerHTML = i + ;
//阶段名称
var newTd2 = newTr.insertCell();
newTd2.innerHTML = '<label id=' + values[i].StageName + ' type="text" alt="' + values[i].StageName + '" >' + values[i].StageName + '</label>';
//所属项目
var newTd3 = newTr.insertCell();
newTd3.innerHTML = values[i].ProName;
//修改
var newTd4 = newTr.insertCell();
newTd4.innerHTML = "<a href='#' id='edit'" + values[i].ID + "' onclick=\"editStage('" + values[i].ID + "','" + values[i].StageName + "')\">修改</a>";
//删除
var newTd5 = newTr.insertCell();
newTd5.innerHTML = "<a href='#' id='del'" + values[i].ID + "' onclick=\"delStage('" + values[i].ID + "','" + values[i].StageName + "','Del')\">删除</a>";
}
}else{
for(var i = ; i < values.length; i++){
//新行
var newTr = stageTable.insertRow(i+);
//序号
var newTd1 = newTr.insertCell();
newTd1.innerHTML = i + ;
//阶段名称
var newTd2 = newTr.insertCell();
newTd2.innerHTML = '<label id=' + values[i].StageName + ' type="text" alt="' + values[i].StageName + '" >' + values[i].StageName + '</label>';
//所属项目
var newTd3 = newTr.insertCell();
newTd3.innerHTML = values[i].ProName;
//修改
var newTd4 = newTr.insertCell();
newTd4.innerHTML = "<a href='#' id='edit'" + values[i].ID + "' disabled = 'true' onclick=\"editStage('" + values[i].ID + "','" + values[i].StageName + "')\">修改</a>";
//删除
var newTd5 = newTr.insertCell();
newTd5.innerHTML = "<a href='#' id='del'" + values[i].ID + "' disabled = 'true' onclick=\"delStage('" + values[i].ID + "','" + values[i].StageName + "','Del')\">删除</a>";
} //隐藏阶段操作条
document.getElementById('searchtitle').className = 'hidediv';
}
$('#stageTable').show();
//清空hidden
document.getElementById('hidStageID').value = ;
} //添加阶段
function addStage(operate){
var stageName = document.getElementById('txtStageName').value;
var id = document.getElementById('hidStageID').value;
if(stageName.length <= ){
alert('阶段名称不能为空!');
return;
}
$.get(
'savestage.ashx?ajax=true&random=' + Math.random(),{'proID':proID,'stageName':stageName,'ID':id,'operate':operate},
function(result){
if(result == 'S'){
alert('保存成功!');
readStage();
}
else if(result == 'E'){
alert('保存失败!');
}
else if(result == 'M')
{
alert('修改成功!');
readStage();
}
else
{
alert('修改失败!');
}
}
);
} //清除多余的行
function clearRow(){
var signFrame = document.getElementById("stageTable");
var rowscount = signFrame.rows.length;
//循环删除行,从最后一行往前删除
for(i=rowscount - ;i >; i--){
signFrame.deleteRow(i);
}
} //修改
function editStage(id,stagename){
document.getElementById('hidStageID').value = id;
document.getElementById('txtStageName').value = stagename;
} //删除
function delStage(id,stageName,operate){
if(confirm('确定要删除此信息?')){
$.get(
'delstage.ashx?ajax=true&random=' + Math.random(),{'proID':proID,'stageName':stageName,'ID':id,'operate':operate},
function(result){
if(result == 'S'){
alert('删除成功!');
readStage();
}else{
alert('删除失败!');
}
});
}
} function delMsg()
{
if(confirm('确定删除此项?'))
{
$.get("delproject.ashx?ajax=true&random="+Math.random(),{"proID":proID},
function(result){
if(result == 'S')
{
alert('删除成功!');
window.location="projectlist.aspx";
}
else
{
alert('删除失败!');
}
});
}
} </script> <div id="searchtitle">
<table width="66%" style="margin-top:10px" border="" cellpadding="" cellspacing="">
<tr>
<td width="23%">阶段名称:<asp:TextBox ID="txtStageName" runat="server" class="input"></asp:TextBox></td>
<td style="padding-left:10px" align="left"><input id="btnPost" type="button" value="保存" class="button" onclick="addStage('Add&Edit')"/></td>
</tr>
</table>
</div>
<asp:HiddenField ID="hidStageID" runat="server" />
<table border="" id="stageTable" class="infolist" style="width:65%; margin-left:0" cellpadding="" cellspacing="">
<thead>
<tr>
<th width="10%">序号</th>
<th width="25%">阶段名称</th>
<th>所属项目</th>
<th style="border-right:1px solid #999999;" colspan="" width="20%">操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

readstage.ashx:

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
int projectID = Convert.ToInt32(context.Request["proID"]);
//PM.BLL.tb_Stage stagebll = new PM.BLL.tb_Stage();
//DataSet ds = stagebll.GetList(" ProjectID="+projectID);
//context.Response.Write(ds);
StringBuilder sb = new StringBuilder();
string sql = "select * from tb_Stage where ProjectID="+projectID;
DataSet ds = PM.DBUtility.DbHelperSQL.Query(sql);
sb.Append("[");
for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
sb.Append("{");
string stagename = ds.Tables[].Rows[i]["StageName"].ToString();
sb.Append("\"StageName\":\"" + stagename + "\"");
sb.Append(",");
string proname = returnProName(ds.Tables[].Rows[i]["ProjectID"].ToString());
sb.Append("\"ProName\":\"" + proname + "\"");
sb.Append(",");
string id = ds.Tables[].Rows[i]["StageID"].ToString();
sb.Append("\"ID\":\"" + id + "\"");
sb.Append("}");
if (i != ds.Tables[].Rows.Count - )
{
sb.Append(",");
}
}
sb.Append("]");
context.Response.Write(sb);
} public string returnProName(string proid)
{
string str = new PM.BLL.tb_Project().GetModel(int.Parse(proid)).ProjectName;
return str;
}

savestage.ashx:

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
string stageName = context.Request["stageName"];
int projectId = Convert.ToInt32(context.Request["proID"]);
string operate = context.Request["operate"];
int id = Convert.ToInt32(context.Request["ID"]);
PM.Model.tb_Stage stagemod = new PM.Model.tb_Stage();
stagemod.StageName = stageName;
stagemod.ProjectID = projectId;
stagemod.StageID = id; PM.BLL.tb_Stage stagebll = new PM.BLL.tb_Stage();
if (id == )
{
int i = stagebll.Add(stagemod);
if (i > )
{
context.Response.Write("S");//保存成功
}
else
{
context.Response.Write("E");//保存失败
}
}
else
{
bool j = stagebll.Update(stagemod);
if (j == true)
{
context.Response.Write("M");//修改成功
}
else
{
context.Response.Write("ME");//修改失败
}
}
}

delstage.ashx:

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
int stageId = Convert.ToInt32(context.Request["ID"]);
PM.BLL.tb_Stage stagebll = new PM.BLL.tb_Stage();
bool res = stagebll.Delete(stageId);
if (res == true)
{
context.Response.Write("S");
}
else
{
context.Response.Write("E");
}
}

ASP.NET+ashx+jQuery动态添加删除表格