刚接触jqgrid的时候被它的效果震撼了,不过找到使用文档都是针对php的,asp.net的都是编译好的dll,很不方便自定义的开发。后来多次搜索和研究才在c# .net下实现了。以下简单扼要的说一下实现原理。
先说明一下,我这个例子的实现过程大概是这样的。因为我要实现很多表格,所以,这些加载都是动态的,先用.net输出jquer调用jqgrid代码,jqgrid再调用后台代码实现数据输出。
首先,写一个方法,功能就是实现输出jqgrid调用模块。
////////////////////////////////////方法:生成调用Jqgrid的代码//////
private void bindProd_Sup(string tbl)
{
string id = Request["id"];
string jsc ="<table id='" + tbl + "_list'></table>"+
private void bindProd_Sup(string tbl)
{
string id = Request["id"];
string jsc ="<table id='" + tbl + "_list'></table>"+
"<div id='" + tbl + "_pagersr'></div>" +
"<script type="text/javascript">" +
"jQuery(\"#\" + tbl + \"_list\").jqGrid({" +
" url:'CompData.aspx?datatype="+(tbl=="cont_cst"?"cst":"sup")+"&oper=listADD&ID=" + id + "'," +
///这个是后台数据处理文件,请求这个文件,将得到json格式的数据。
" height:520," +
" datatype: "json"," +
" colNames:[ 'ADD','ID','SUP NUM','NAME']," +
" colModel:[" +
" {name:'ADD',index:'ADD', width:80, sortable:false,search:false}, " +
" {name:'ComID',index:'ComID', width:45 ,hidden:true}," +
" {name:'CompNum',index:'CompNum', width:65}," +
" {name:'CompName',index:'CompName', width:320}" +
" ]," +
" multiselect: true," +
" rowNum:20," +
" rowList:[20,40,80]," +
" pager: '#" + tbl + "_pagersr'," +
" sortname: 'ComID'," +
" viewrecords: true," +
" sortorder: "desc"," +
" caption:"COMPANY LIST"," +
// "{sopt:['cn','bw','eq','ne','lt','gt','ew']}," +
" toolbar : [true,"top"] " +
" });" +
// "jQuery("#"+tbl+"_list").jqGrid('"+tbl+"_listGrid',{sopt:['cn','bw','eq','ne','lt','gt','ew']});"+
"jQuery("#" + tbl + "_list").jqGrid('navGrid',"#" + tbl + "_pagersr",{edit:false,add:false,del:false}); " +
" </script>";
jscS.InnerHtml = jsc; ////将jqgrid调用代码输出
}
//////////////////////////////////////////
" height:520," +
" datatype: "json"," +
" colNames:[ 'ADD','ID','SUP NUM','NAME']," +
" colModel:[" +
" {name:'ADD',index:'ADD', width:80, sortable:false,search:false}, " +
" {name:'ComID',index:'ComID', width:45 ,hidden:true}," +
" {name:'CompNum',index:'CompNum', width:65}," +
" {name:'CompName',index:'CompName', width:320}" +
" ]," +
" multiselect: true," +
" rowNum:20," +
" rowList:[20,40,80]," +
" pager: '#" + tbl + "_pagersr'," +
" sortname: 'ComID'," +
" viewrecords: true," +
" sortorder: "desc"," +
" caption:"COMPANY LIST"," +
// "{sopt:['cn','bw','eq','ne','lt','gt','ew']}," +
" toolbar : [true,"top"] " +
" });" +
// "jQuery("#"+tbl+"_list").jqGrid('"+tbl+"_listGrid',{sopt:['cn','bw','eq','ne','lt','gt','ew']});"+
"jQuery("#" + tbl + "_list").jqGrid('navGrid',"#" + tbl + "_pagersr",{edit:false,add:false,del:false}); " +
" </script>";
jscS.InnerHtml = jsc; ////将jqgrid调用代码输出
}
//////////////////////////////////////////
以下是处理数据后台文件代码
private void jsondata()
{
string DataType = Request.QueryString["datatype"] != null ? Request.QueryString["datatype"].ToString() :
string searchFiled = "";
string indexFiled = "";
string searchFiled_Sp1 = "";
string searchFiled_Sp1_tbl = "";
string searchFiled_Sp2 = "";
string searchFiled_Sp2_tbl = "";
string Sql = "";
string SqlAnd = "";
string Edit="";
string Add="";
string count = "";
string countSql = "";
string id="";
string ids = "";
switch (DataType)
{
case "SUP":
case "CST":
case "cst":
case "sup":
if (Request["oper"] == "listADD")
{
string ID = Request["ID"];
string INV_ID = "";
if (DataType == "sup")
{
INV_ID = as_init.AS_GetValue("SELECT ProdNum FROM tblproduct where ProdID= " + ID);
}
else if (DataType == "cst")
{
INV_ID = as_init.AS_GetValue("SELECT ContNum FROM tblcontact where ContID= " + ID);
}
earchFiled = " A.ComID,A.CompNum,A.CompName";
indexFiled = "A.ComID";
Sql = " select A.ComID,A.CompNum,A.CompName From tblcompany A";
SqlAnd = " WHERE A.CompNum like '%" + DataType + "%' ";
countSql = "select count(*) from tblcompany A ";
Add = INV_ID + "|" + ID + "|" + DataType;
}
else
{
searchFiled = "A.ComID,A.CompNum,A.CompCreationDate,A.CompActivity,A.CompName, A.CompPostalCode,A.CompCountry";
indexFiled = "A.ComID";
Sql = " select A.ComID,A.CompNum,A.CompCreationDate,A.CompActivity,A.CompName, A.CompPostalCode,A.CompCountry From tblcompany A";
SqlAnd = " WHERE A.CompNum like '%" + DataType + "%' ";
countSql = "select count(*) from tblcompany A ";
Edit = "800|580|AjaxEdit.aspx?Act=Edit|" + DataType;
}
break;
case "prod_sup_l":
id=Request["id"];
ids = as_init.AS_GetValue("select ProdNum from tblproduct where ProdID= " + id);
searchFiled = " A.SupNum,B.CompName,B.CompPhone,B.CompAddress,A.Sp_ID ";
indexFiled = "Sp_ID";
searchFiled_Sp1 = ",CompName,CompPhone,CompAddress";
searchFiled_Sp1_tbl = "B";
Sql = " select A.Sp_ID, A.SupNum,B.CompName,B.CompPhone,B.CompAddress FROM tblproductsupplier A LEFT JOIN tblcompany B ON A.SupNum=B.CompNum ";
SqlAnd = " WHERE A.ProdNum='" + ids + "' ";
countSql = "select count(*) from tblproductsupplier A LEFT JOIN tblcompany B ON A.SupNum=B.CompNum ";
Edit = "";
break;
case "inv_carton":
id = Request["id"];
ids = as_init.AS_GetValue("select ActNum from tblactivity where ActIndex= " + id);
searchFiled = "A.cartonID, A.NCarton, A.Gweight,A.GVolume ";
indexFiled = "cartonID";
Sql = " select A.cartonID,A.ActNum, A.NCarton, A.Gweight,A.GVolume FROM tblactivitycarton A";
SqlAnd = " WHERE ActNum='" + ids + "'";
countSql = "select count(*) from tblactivitycarton ";
Edit = "";
break;
}
string keyw = Request.QueryString["searchString"] != null ? Request.QueryString["searchString"].ToString() : "";
string page = Request.QueryString["page"] != null ? Request.QueryString["page"].ToString() : "1"; // get the requested page
string limit = Request.QueryString["rows"] != null ? Request.QueryString["rows"].ToString() : "10"; // get how many rows we want to have into the grid
string sidx = Request.QueryString["sidx"] != null ? Request.QueryString["sidx"].ToString() : indexFiled; // get index row - i.e. user click to sort
string sord = Request.QueryString["sord"] != null ? Request.QueryString["sord"].ToString() : "asc"; // get the direction
int start = Convert.ToInt32(limit) * Convert.ToInt32(page) - Convert.ToInt32(limit);
///////////////////sql where'///////////////////////////////////////
string sqlw = "";
string searchOn = Request.QueryString["_search"] != null ? Request.QueryString["_search"].ToString() : "";
// if(searchOn=="true") {
string fld = Request.QueryString["searchField"] != null ? Request.QueryString["searchField"].ToString() : "";
// Response.Write(searchFiled.IndexOf(fld)+fld);
if (searchFiled.IndexOf(fld) > 0 && fld != "" && fld !=null)
{
string fldata = Request.QueryString["searchString"] != null ? Request.QueryString["searchString"].ToString() : "";
string foper = Request.QueryString["searchOper"] != null ? Request.QueryString["searchOper"].ToString() : "";
// costruct where
if ( searchFiled_Sp1.IndexOf(fld)>=0)
{
sqlw += searchFiled_Sp1_tbl + "." + fld;
}
else if (searchFiled_Sp2.IndexOf(fld)>=0)
{
sqlw += searchFiled_Sp2_tbl + "." + fld;
}
else
{
sqlw += " A." + fld;
}
switch (foper)
{
case "bw":
fldata += "%";
sqlw += " LIKE '" + fldata + "'";
break;
case "eq":
if (is_numeric(fldata))
{
sqlw += " = " + fldata;
}
else
{
sqlw += " = '" + fldata + "'";
}
break;
case "ne":
if (is_numeric(fldata))
{
sqlw += " <> " + fldata;
}
else
{
sqlw += " <> '" + fldata + "'";
}
break;
case "lt":
if (is_numeric(fldata))
{
sqlw += " < " + fldata;
}
else
{
sqlw += " < '" + fldata + "'";
}
break;
case "le":
if (is_numeric(fldata))
{
sqlw += " <= " + fldata;
}
else
{
sqlw += " <= '" + fldata + "'";
}
break;
case "gt":
if (is_numeric(fldata))
{
sqlw += " > " + fldata;
}
else
{
sqlw += " > '" + fldata + "'";
}
break;
case "ge":
if (is_numeric(fldata))
{
sqlw += " >= " + fldata;
}
else
{
sqlw += " >= '" + fldata + "'";
}
break;
case "ew":
sqlw += " LIKE '" + fldata + "%'";
break;
case "cn":
sqlw += " LIKE '%" + fldata + "%'";
break;
}
}
//Response.Write(sqlw);
if (sqlw != "")
{
sqlw=(SqlAnd!=""? SqlAnd+" and "+sqlw : " where "+sqlw);
}
else
{
sqlw = (SqlAnd != "" ? SqlAnd :"");
}
Sql = Sql + sqlw + " ORDER BY " + sidx + " " + sord + " LIMIT " + start + " , " + limit;
DataTable datasinv = as_init.as_DataTab(Sql);
count = as_init.AS_GetValue(countSql + sqlw);
string dataJs = as_init.GetJson(datasinv, page, count, Edit, Add);
Response.ContentType = "text/plain";
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
Response.AddHeader("pragma", "no-cache");
Response.AddHeader("cache-control", "");
Response.CacheControl = "no-cache";
Response.Write(dataJs);
}
{
string DataType = Request.QueryString["datatype"] != null ? Request.QueryString["datatype"].ToString() :
string searchFiled = "";
string indexFiled = "";
string searchFiled_Sp1 = "";
string searchFiled_Sp1_tbl = "";
string searchFiled_Sp2 = "";
string searchFiled_Sp2_tbl = "";
string Sql = "";
string SqlAnd = "";
string Edit="";
string Add="";
string count = "";
string countSql = "";
string id="";
string ids = "";
switch (DataType)
{
case "SUP":
case "CST":
case "cst":
case "sup":
if (Request["oper"] == "listADD")
{
string ID = Request["ID"];
string INV_ID = "";
if (DataType == "sup")
{
INV_ID = as_init.AS_GetValue("SELECT ProdNum FROM tblproduct where ProdID= " + ID);
}
else if (DataType == "cst")
{
INV_ID = as_init.AS_GetValue("SELECT ContNum FROM tblcontact where ContID= " + ID);
}
earchFiled = " A.ComID,A.CompNum,A.CompName";
indexFiled = "A.ComID";
Sql = " select A.ComID,A.CompNum,A.CompName From tblcompany A";
SqlAnd = " WHERE A.CompNum like '%" + DataType + "%' ";
countSql = "select count(*) from tblcompany A ";
Add = INV_ID + "|" + ID + "|" + DataType;
}
else
{
searchFiled = "A.ComID,A.CompNum,A.CompCreationDate,A.CompActivity,A.CompName, A.CompPostalCode,A.CompCountry";
indexFiled = "A.ComID";
Sql = " select A.ComID,A.CompNum,A.CompCreationDate,A.CompActivity,A.CompName, A.CompPostalCode,A.CompCountry From tblcompany A";
SqlAnd = " WHERE A.CompNum like '%" + DataType + "%' ";
countSql = "select count(*) from tblcompany A ";
Edit = "800|580|AjaxEdit.aspx?Act=Edit|" + DataType;
}
break;
case "prod_sup_l":
id=Request["id"];
ids = as_init.AS_GetValue("select ProdNum from tblproduct where ProdID= " + id);
searchFiled = " A.SupNum,B.CompName,B.CompPhone,B.CompAddress,A.Sp_ID ";
indexFiled = "Sp_ID";
searchFiled_Sp1 = ",CompName,CompPhone,CompAddress";
searchFiled_Sp1_tbl = "B";
Sql = " select A.Sp_ID, A.SupNum,B.CompName,B.CompPhone,B.CompAddress FROM tblproductsupplier A LEFT JOIN tblcompany B ON A.SupNum=B.CompNum ";
SqlAnd = " WHERE A.ProdNum='" + ids + "' ";
countSql = "select count(*) from tblproductsupplier A LEFT JOIN tblcompany B ON A.SupNum=B.CompNum ";
Edit = "";
break;
case "inv_carton":
id = Request["id"];
ids = as_init.AS_GetValue("select ActNum from tblactivity where ActIndex= " + id);
searchFiled = "A.cartonID, A.NCarton, A.Gweight,A.GVolume ";
indexFiled = "cartonID";
Sql = " select A.cartonID,A.ActNum, A.NCarton, A.Gweight,A.GVolume FROM tblactivitycarton A";
SqlAnd = " WHERE ActNum='" + ids + "'";
countSql = "select count(*) from tblactivitycarton ";
Edit = "";
break;
}
string keyw = Request.QueryString["searchString"] != null ? Request.QueryString["searchString"].ToString() : "";
string page = Request.QueryString["page"] != null ? Request.QueryString["page"].ToString() : "1"; // get the requested page
string limit = Request.QueryString["rows"] != null ? Request.QueryString["rows"].ToString() : "10"; // get how many rows we want to have into the grid
string sidx = Request.QueryString["sidx"] != null ? Request.QueryString["sidx"].ToString() : indexFiled; // get index row - i.e. user click to sort
string sord = Request.QueryString["sord"] != null ? Request.QueryString["sord"].ToString() : "asc"; // get the direction
int start = Convert.ToInt32(limit) * Convert.ToInt32(page) - Convert.ToInt32(limit);
///////////////////sql where'///////////////////////////////////////
string sqlw = "";
string searchOn = Request.QueryString["_search"] != null ? Request.QueryString["_search"].ToString() : "";
// if(searchOn=="true") {
string fld = Request.QueryString["searchField"] != null ? Request.QueryString["searchField"].ToString() : "";
// Response.Write(searchFiled.IndexOf(fld)+fld);
if (searchFiled.IndexOf(fld) > 0 && fld != "" && fld !=null)
{
string fldata = Request.QueryString["searchString"] != null ? Request.QueryString["searchString"].ToString() : "";
string foper = Request.QueryString["searchOper"] != null ? Request.QueryString["searchOper"].ToString() : "";
// costruct where
if ( searchFiled_Sp1.IndexOf(fld)>=0)
{
sqlw += searchFiled_Sp1_tbl + "." + fld;
}
else if (searchFiled_Sp2.IndexOf(fld)>=0)
{
sqlw += searchFiled_Sp2_tbl + "." + fld;
}
else
{
sqlw += " A." + fld;
}
switch (foper)
{
case "bw":
fldata += "%";
sqlw += " LIKE '" + fldata + "'";
break;
case "eq":
if (is_numeric(fldata))
{
sqlw += " = " + fldata;
}
else
{
sqlw += " = '" + fldata + "'";
}
break;
case "ne":
if (is_numeric(fldata))
{
sqlw += " <> " + fldata;
}
else
{
sqlw += " <> '" + fldata + "'";
}
break;
case "lt":
if (is_numeric(fldata))
{
sqlw += " < " + fldata;
}
else
{
sqlw += " < '" + fldata + "'";
}
break;
case "le":
if (is_numeric(fldata))
{
sqlw += " <= " + fldata;
}
else
{
sqlw += " <= '" + fldata + "'";
}
break;
case "gt":
if (is_numeric(fldata))
{
sqlw += " > " + fldata;
}
else
{
sqlw += " > '" + fldata + "'";
}
break;
case "ge":
if (is_numeric(fldata))
{
sqlw += " >= " + fldata;
}
else
{
sqlw += " >= '" + fldata + "'";
}
break;
case "ew":
sqlw += " LIKE '" + fldata + "%'";
break;
case "cn":
sqlw += " LIKE '%" + fldata + "%'";
break;
}
}
//Response.Write(sqlw);
if (sqlw != "")
{
sqlw=(SqlAnd!=""? SqlAnd+" and "+sqlw : " where "+sqlw);
}
else
{
sqlw = (SqlAnd != "" ? SqlAnd :"");
}
Sql = Sql + sqlw + " ORDER BY " + sidx + " " + sord + " LIMIT " + start + " , " + limit;
DataTable datasinv = as_init.as_DataTab(Sql);
count = as_init.AS_GetValue(countSql + sqlw);
string dataJs = as_init.GetJson(datasinv, page, count, Edit, Add);
Response.ContentType = "text/plain";
Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
Response.AddHeader("pragma", "no-cache");
Response.AddHeader("cache-control", "");
Response.CacheControl = "no-cache";
Response.Write(dataJs);
}
原创:美芯科技(欢迎转载,但请保留链接)