费话不多说,代码说明问题
前台HTML代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<
asp:Repeater
ID
="Repeater1"
runat
="server"
>
< ItemTemplate >
<!-- 循环要处理的数据,数据放入HTML控件即可 -->
</ ItemTemplate >
</ asp:Repeater >
< ItemTemplate >
<!-- 循环要处理的数据,数据放入HTML控件即可 -->
</ ItemTemplate >
</ asp:Repeater >
<input type="button" id="btn_save" value="保存" οnclick="SaveData()"/>
<!-- 添加AJAX控件,EnablePageMethods设为true才可使用此功能 -->
< asp:ScriptManager ID ="ScriptManager1" EnablePageMethods ="true" runat ="server" />
javascript代码
function
SaveData() {
var tbl = document.getElementById( " tbl " );
var datas = new Array(tbl.rows.length - 1 );
if (datas.length < 1 ) {
alert( " 不存在预定明细,不允许保存! " );
return ;
}
for ( var i = 1 ; i < tbl.rows.length; i ++ ) {
txt = tbl.rows[i].getElementsByTagName( " input " );
sel = tbl.rows[i].getElementsByTagName( " select " );
// alert(sel[0].value);return false;
datas[i - 1 ] = txt[ 5 ].value + " , " + txt[ 0 ].value + " , " + txt[ 1 ].value + " , " + txt[ 2 ].value + " , " + txt[ 3 ].value + " , " + txt[ 4 ].value + " , " + sel[ 0 ].value;
}
// alert(cost);return false;
// 后台方法,此处核心
PageMethods.GetData(datas, showResult);
}
// 回调函数
function showResult(msg) {
if (msg == " 0 " ) {
alert( " 保存成功 " );
this .location = " List.aspx?currpage= " + $( " #HiddenCurrpage " ).val();
}
else {
alert( " 保存失败 " );
}
}
}
var tbl = document.getElementById( " tbl " );
var datas = new Array(tbl.rows.length - 1 );
if (datas.length < 1 ) {
alert( " 不存在预定明细,不允许保存! " );
return ;
}
for ( var i = 1 ; i < tbl.rows.length; i ++ ) {
txt = tbl.rows[i].getElementsByTagName( " input " );
sel = tbl.rows[i].getElementsByTagName( " select " );
// alert(sel[0].value);return false;
datas[i - 1 ] = txt[ 5 ].value + " , " + txt[ 0 ].value + " , " + txt[ 1 ].value + " , " + txt[ 2 ].value + " , " + txt[ 3 ].value + " , " + txt[ 4 ].value + " , " + sel[ 0 ].value;
}
// alert(cost);return false;
// 后台方法,此处核心
PageMethods.GetData(datas, showResult);
}
// 回调函数
function showResult(msg) {
if (msg == " 0 " ) {
alert( " 保存成功 " );
this .location = " List.aspx?currpage= " + $( " #HiddenCurrpage " ).val();
}
else {
alert( " 保存失败 " );
}
}
}
后台代码:
[System.Web.Services.WebMethod]
public static string GetData( string [] datas)
{
// 对数组进行操作,存入数据库,主从用事务处理。
public static string GetData( string [] datas)
{
// 对数组进行操作,存入数据库,主从用事务处理。
//return "0";
}
}
OK 完成AJAX 操作,处理多行,没有那么烦索,也避免了页面刷新;
使用此方法javascript 要有基础,数据构造通过js完成
感受如题。