标签:
Web jquery表格组件 JQGrid 的使用 - 4.JQGrid参数、ColModel API、事件及方法
Web jquery表格组件 JQGrid 的使用 - 5.Pager翻页、搜索、格式化、自定义按钮
Web jquery表格组件 JQGrid 的使用 - 6.准备工作 & Hello JQGrid
Web jquery表格组件 JQGrid 的使用 - 7.查询数据、编辑数据、删除数据
Web jquery表格组件 JQGrid 的使用 - 8.Pager、新增数据、查询、刷新、查看数据
Web jquery表格组件 JQGrid 的使用 - 全部代码
Web jquery表格组件 JQGrid 的使用 - 11.问题研究
public void ProcessRequest(HttpContext context)
{
//查找
if (context.Request.Params.Get("_search") == "true")
{
return;
}
NameValueCollection forms = context.Request.Form;
string strOperation = forms.Get("oper");
string strResponse = string.Empty;
if (strOperation == null)
{
//排序
if (context.Request.Params.Get("sidx") != null &&
!string.IsNullOrEmpty(context.Request.Params.Get("sidx").ToString()) &&
context.Request.Params.Get("sord") != null &&
!string.IsNullOrEmpty(context.Request.Params.Get("sord").ToString()))
{
return;
}
strResponse = GetJson(GetUserDT()); //load data
}
context.Response.Write(strResponse);
}
private DataTable GetUserDT()
{
string cmdText = "SELECT UserId, UserCode, Password FROM T_User";
SQLHelper sqlhelper = new SQLHelper();
DataTable dt = sqlhelper.Selectinfo(cmdText);
return dt;
}
private string GetJson(DataTable dt)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
List<Dictionary<string, object>> rows = new List<Dictionary<string,
object>>();
Dictionary<string, object> row = null;
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
string s = dr[col].ToString(); //特殊情况
if (col.ColumnName == "IsAdmin")
{
s = s == "1" ? "是" : "否";
}
if (col.ColumnName == "HasWrite")
{
s = s == "1" ? "是" : "否";
}
row.Add(col.ColumnName.Trim(), s);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
loadComplete: function (xhr) {
var users = $(grid_selector).jqGrid(‘getDataIDs‘);
if (users) {
for (var i = 0, j = users.length; i < j; i++) {
ParkId = $(grid_selector).jqGrid(‘getCell‘, users[i],
‘UserCode‘);
$(grid_selector).jqGrid(‘setRowData‘, users[i], {‘UserCode‘:
i});
}
}
},
{
name: ‘myac‘, index: ‘‘, width: 80, fixed: true, sortable:
false, resize: false,
formatter: ‘actions‘,
formatoptions: {
keys: true,
//editbutton: true,
editformbutton: true,
// extraparam: { oper: ‘edit‘ },
editOptions: { //编辑操作,这个很重要,实现编辑时传送参数
什么的。
reloadAfterSubmit: true,
//editData: {
// editkey: function () {
// var sel_id =
$(grid_selector).jqGrid(‘getGridParam‘, ‘selrow‘);
// var value = $(grid_selector).jqGrid(‘getCell‘,
sel_id, ‘UserId‘);
// return value;
// }
//},
closeAfterEdit: true,
afterSubmit: function (response, postdata) {
if (response.responseText != "") {
alert(response.responseText);
$(this).jqGrid(‘setGridParam‘, { datatype:
‘json‘ }).trigger(‘reloadGrid‘);
return [true, response.responseText]
}
},
},
delOptions: { //删除操作,这个很重要,实现删除时传送参数什
么的。 这处网上没有例子的。
reloadAfterSubmit: true,
delData: {
delId: function () {
var sel_id =
$(grid_selector).jqGrid(‘getGridParam‘, ‘selrow‘);
var value = $(grid_selector).jqGrid(‘getCell‘,
sel_id, ‘UserId‘);
return value;
}
},
afterSubmit: function (response, postdata) {
if (response.responseText != "") {
alert(response.responseText);
$(this).jqGrid(‘setGridParam‘, { datatype:
‘json‘ }).trigger(‘reloadGrid‘);
return [true, response.responseText]
}
}
},
}
},
var sel_id = [];
sel_id = $(grid_selector).jqGrid(‘getGridParam‘,
‘selarrrow‘);
var value = ‘‘;
for (var i = 0; i < sel_id.length; i++) {
value = value + ‘,‘ + $(grid_selector).jqGrid(‘getCell‘,
sel_id[i], ‘ParkId‘);
}
if (value.charAt(0) == ‘,‘) {
value = value.substr(1);
}
if (strOperation == "edit")
{
user.UserId = int.Parse(forms.Get("UserId").ToString());
if (CheckUserExist(user.UserCode, user.UserId.ToString()))
{
strResponse = "用户名重复,请确认!";
}
else
{
strResponse = UpdateUser(user) ? "用户更新成功!" : "用户更新失败,
请确认!";
}
context.Response.Write(strResponse);
}
/// <summary>
/// 更新用户信息
/// </summary>
/// <param name="objuser"></param>
/// <returns>更新成功与否</returns>
private bool UpdateUser(User objuser)
{
string sql = "UPDATE T_User SET UserCode = ‘" + objuser.UserCode +
"‘,Password = ‘" + objuser.Password + "‘ WHERE UserId=" + objuser.UserId;
SQLHelper sqlhelper = new SQLHelper();
return sqlhelper.AddDelUpdate(sql) > 0;
}
private bool CheckUserExist(string UserCode, string UserId)
{
string sql = "select * from T_User where UserCode = ‘" + UserCode + "‘ and
UserId <> " + UserId;
SQLHelper sqlhelper = new SQLHelper();
DataTable dt = sqlhelper.Selectinfo(sql);
return dt == null || (dt != null && dt.Rows.Count == 1);
}
if (strOperation == "del")
{
strResponse = DeleteUser(forms.Get("delId").ToString()) ? "删除成功!" :
"删除失败,请确认!";
}
context.Response.Write(strResponse);
private bool DeleteUser(string userId)
{
bool flag = true;
string[] idlist = userId.Split(‘,‘);
foreach (var sid in idlist)
{
string sql = "DELETE FROM T_User WHERE UserId=" + userId;
SQLHelper sqlhelper = new SQLHelper();
flag = sqlhelper.AddDelUpdate(sql) > 0;
}
return flag;
}
Web jquery表格组件 JQGrid 的使用 - 7.查询数据、编辑数据、删除数据
标签:
原文地址:http://www.cnblogs.com/jhlong/p/5627784.html