摘要:[使用HiddenField]記住多筆畫面資料給後端繫結
Javascript、JQuery
<script type="text/javascript" language="javascript">
$(function () {
//匯入中請等待(動畫)
$('#Process').hide();
$('.import').click(function () {
$('#Process').animate({ width: '100px', height: '50px' }, "slow");
$('#Process').show();
});
});
//記住多筆畫面資料給後端繫結
var mySubmit = function () {
var $trs = $('.ListView_Table tr');
var size = $trs.size();
if (size > 2) {
$trs = $trs.slice(1, size - 1);
var data = "";
$trs.each(function (n) {
var empno = $.trim($(this).find('td :hidden[name$=hidEmpno]')[0].value);
var ispocket = $.trim($(this).find('td :input[name$=ddlIsOK]')[0].value);
var remark = $.trim($(this).find('td :input[name$=txtRemark]')[0].value);
if (data.length != 0) {
data = data + "|";
}
data = data + empno + ":" + ispocket + ":" + remark;
});
//把值塞入ASP.NET HiddenField 控制項
$(':hidden[name$=hiddenData]').val(data);
}
}
</script>
C#
protected void lvMain_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.Equals("UpdateSelect"))
{
List<ABCVo> deleteList = new List<ABCVo>();
DataTable selectedDT = new DataTable();
ABCOds ods = new ABCOds();
string[] datas = this.hiddenData.Value.Split(new char[] { '|' });
//foreach (ListViewDataItem item in lvMain.Items)
foreach(string data in datas)
{
string[] _datas = data.Split(new char[] { ':' });
string empNo = _datas[0];
bool isPocket = (_datas[1].Equals("Y"));
string remark = _datas[2];
//檢查加班意願,備註欄位有無更動,有則更新
ABCVo oVo = ods.FindDataByKey(empNo);
ABCVo nVo = new ABCVo();
nVo.EMPNO = empNo;
nVo.REMARK = remark;
nVo.ISPOCKET = (isPocket ? "Y" : "N");
//加班意願,備註欄位都相同,表示無變動資料
if (!String.IsNullOrEmpty(oVo.EMPNO)) //資料庫已存在有資料
{
//加班意願,備註欄位都相同,表示無變動資料
if (oVo.ISPOCKET.Equals(nVo.ISPOCKET))
{
//備註欄位值,null和空字串當作相同
if (String.IsNullOrEmpty(oVo.REMARK) && String.IsNullOrEmpty(nVo.REMARK))
{
continue;
}
if (oVo.REMARK != null && oVo.REMARK.Equals(nVo.REMARK))
{
continue;
}
}
}
ods.SaveOrUpdate(nVo);
}
lvMain.SelectedIndex = -1;
lvMain.EditIndex = -1;
lvMain.DataBind();
}
}