共享网

Jquery插件 Jqgrid在ASP.Net下的使用方法与实例【转】
2010-8-20 8:08:15 文章来源: http://www.chr114.com 文章作者: chr114 点击率:
核心提示: Jquery插件 Jqgrid在ASP.Net下的使用方法与实例

     刚接触jqgrid的时候被它的效果震撼了,不过找到使用文档都是针对php的,asp.net的都是编译好的dll,很不方便自定义的开发。后来多次搜索和研究才在c# .net下实现了。以下简单扼要的说一下实现原理。

      先说明一下,我这个例子的实现过程大概是这样的。因为我要实现很多表格,所以,这些加载都是动态的,先用.net输出jquer调用jqgrid代码,jqgrid再调用后台代码实现数据输出。

      首先,写一个方法,功能就是实现输出jqgrid调用模块。

JavaScript代码
  1. ////////////////////////////////////方法:生成调用Jqgrid的代码//////    
  2. private void bindProd_Sup(string tbl)   
  3. {   
  4.      string id = Request["id"];   
  5.      string jsc ="<table id='" + tbl + "_list'></table>"+     
  6.                 "<div id='" + tbl + "_pagersr'></div>" +   
  7.                 "<script  type="text/javascript">" +   
  8.                 "jQuery(\"#\" + tbl + \"_list\").jqGrid({" +    
  9.                 " url:'CompData.aspx?datatype="+(tbl=="cont_cst"?"cst":"sup")+"&oper=listADD&ID=" + id + "'," +   
  10.                  ///这个是后台数据处理文件,请求这个文件,将得到json格式的数据。   
  11.                 " height:520," +   
  12.                 " datatype: "json"," +   
  13.                 " colNames:[ 'ADD','ID','SUP NUM','NAME']," +   
  14.                 " colModel:[" +   
  15.                 " {name:'ADD',index:'ADD', width:80, sortable:false,search:false}, " +   
  16.                 " {name:'ComID',index:'ComID', width:45 ,hidden:true}," +   
  17.                 " {name:'CompNum',index:'CompNum', width:65}," +   
  18.                 " {name:'CompName',index:'CompName', width:320}" +   
  19.                 " ]," +   
  20.                 " multiselect: true," +   
  21.                 " rowNum:20," +   
  22.                 " rowList:[20,40,80]," +   
  23.                 " pager: '#" + tbl + "_pagersr'," +   
  24.                 " sortname: 'ComID'," +   
  25.                 " viewrecords: true," +   
  26.                 " sortorder: "desc"," +   
  27.                 " caption:"COMPANY LIST"," +   
  28.             //  "{sopt:['cn','bw','eq','ne','lt','gt','ew']}," +   
  29.                 " toolbar : [true,"top"] " +   
  30.                 " });" +   
  31.             // "jQuery("#"+tbl+"_list").jqGrid('"+tbl+"_listGrid',{sopt:['cn','bw','eq','ne','lt','gt','ew']});"+   
  32.                 "jQuery("#" + tbl + "_list").jqGrid('navGrid',"#" + tbl + "_pagersr",{edit:false,add:false,del:false}); " +   
  33.                 " </script>";   
  34.         jscS.InnerHtml = jsc; ////将jqgrid调用代码输出   
  35.     }   
  36. //////////////////////////////////////////   
  37.   
  38. 以下是处理数据后台文件代码   
  39.   
  40.  private void jsondata()   
  41.     {   
  42.         string DataType = Request.QueryString["datatype"] != null ? Request.QueryString["datatype"].ToString() :    
  43.         string searchFiled = "";   
  44.         string indexFiled = "";   
  45.         string searchFiled_Sp1 = "";   
  46.         string searchFiled_Sp1_tbl = "";   
  47.         string searchFiled_Sp2 = "";   
  48.         string searchFiled_Sp2_tbl = "";   
  49.         string Sql = "";   
  50.         string SqlAnd = "";   
  51.         string Edit="";   
  52.         string Add="";   
  53.         string count = "";   
  54.         string countSql = "";   
  55.         string id="";   
  56.         string ids = "";   
  57.         switch (DataType)   
  58.         {   
  59.             case "SUP":   
  60.             case "CST":   
  61.             case "cst":   
  62.             case "sup":   
  63.             if (Request["oper"] == "listADD")   
  64.             {   
  65.                 string ID = Request["ID"];   
  66.                 string INV_ID = "";   
  67.                 if (DataType == "sup")   
  68.                 {   
  69.                     INV_ID = as_init.AS_GetValue("SELECT ProdNum FROM tblproduct where ProdID= " + ID);   
  70.                 }   
  71.                 else if (DataType == "cst")   
  72.                 {   
  73.                     INV_ID = as_init.AS_GetValue("SELECT ContNum FROM tblcontact where ContID= " + ID);   
  74.                  }   
  75.                 earchFiled = " A.ComID,A.CompNum,A.CompName";   
  76.                 indexFiled = "A.ComID";   
  77.                 Sql = " select  A.ComID,A.CompNum,A.CompName From tblcompany A";   
  78.                 SqlAnd = "  WHERE  A.CompNum like '%" + DataType + "%' ";   
  79.                 countSql = "select count(*) from tblcompany A ";   
  80.                 Add = INV_ID + "|" + ID + "|" + DataType;   
  81.            }   
  82.            else  
  83.           {   
  84.              searchFiled = "A.ComID,A.CompNum,A.CompCreationDate,A.CompActivity,A.CompName, A.CompPostalCode,A.CompCountry";   
  85.              indexFiled = "A.ComID";   
  86.              Sql = " select A.ComID,A.CompNum,A.CompCreationDate,A.CompActivity,A.CompName, A.CompPostalCode,A.CompCountry From tblcompany A";   
  87.              SqlAnd = "  WHERE  A.CompNum like '%" + DataType + "%' ";   
  88.              countSql = "select count(*) from tblcompany A ";   
  89.              Edit = "800|580|AjaxEdit.aspx?Act=Edit|" + DataType;   
  90.            }   
  91.            break;   
  92.            case "prod_sup_l":   
  93.                 id=Request["id"];   
  94.                 ids = as_init.AS_GetValue("select ProdNum from tblproduct where ProdID= " + id);   
  95.                 searchFiled = " A.SupNum,B.CompName,B.CompPhone,B.CompAddress,A.Sp_ID ";   
  96.                 indexFiled = "Sp_ID";   
  97.                 searchFiled_Sp1 = ",CompName,CompPhone,CompAddress";   
  98.                 searchFiled_Sp1_tbl = "B";   
  99.                 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   ";   
  100.                 SqlAnd = "  WHERE  A.ProdNum='" + ids + "' ";   
  101.                 countSql = "select count(*) from tblproductsupplier A LEFT JOIN tblcompany B ON A.SupNum=B.CompNum  ";   
  102.                 Edit = "";   
  103.                 break;   
  104.             case "inv_carton":   
  105.                 id = Request["id"];   
  106.                 ids = as_init.AS_GetValue("select ActNum from tblactivity where ActIndex= " + id);   
  107.                 searchFiled = "A.cartonID, A.NCarton, A.Gweight,A.GVolume ";   
  108.                 indexFiled = "cartonID";   
  109.                 Sql = " select A.cartonID,A.ActNum, A.NCarton, A.Gweight,A.GVolume  FROM  tblactivitycarton A";   
  110.                 SqlAnd = " WHERE ActNum='" + ids + "'";   
  111.                 countSql = "select count(*) from tblactivitycarton ";   
  112.                 Edit = "";   
  113.                 break;   
  114.         }   
  115.         string keyw = Request.QueryString["searchString"] != null ? Request.QueryString["searchString"].ToString() : "";   
  116.         string page = Request.QueryString["page"] != null ? Request.QueryString["page"].ToString() : "1"// get the requested page   
  117.         string limit = Request.QueryString["rows"] != null ? Request.QueryString["rows"].ToString() : "10"// get how many rows we want to have into the grid   
  118.         string sidx = Request.QueryString["sidx"] != null ? Request.QueryString["sidx"].ToString() : indexFiled; // get index row - i.e. user click to sort   
  119.         string sord = Request.QueryString["sord"] != null ? Request.QueryString["sord"].ToString() : "asc"// get the direction   
  120.         int start = Convert.ToInt32(limit) * Convert.ToInt32(page) - Convert.ToInt32(limit);   
  121.         ///////////////////sql where'///////////////////////////////////////   
  122.         string sqlw = "";   
  123.         string searchOn = Request.QueryString["_search"] != null ? Request.QueryString["_search"].ToString() : "";   
  124.   
  125.         // if(searchOn=="true") {   
  126.         string fld = Request.QueryString["searchField"] != null ? Request.QueryString["searchField"].ToString() : "";   
  127.        // Response.Write(searchFiled.IndexOf(fld)+fld);   
  128.         if (searchFiled.IndexOf(fld) > 0 && fld != "" && fld !=null)   
  129.         {   
  130.             string fldata = Request.QueryString["searchString"] != null ? Request.QueryString["searchString"].ToString() : "";   
  131.             string foper = Request.QueryString["searchOper"] != null ? Request.QueryString["searchOper"].ToString() : "";   
  132.             // costruct where   
  133.             if ( searchFiled_Sp1.IndexOf(fld)>=0)   
  134.             {   
  135.                 sqlw += searchFiled_Sp1_tbl + "." + fld;   
  136.             }   
  137.             else if (searchFiled_Sp2.IndexOf(fld)>=0)   
  138.             {   
  139.                 sqlw += searchFiled_Sp2_tbl + "." + fld;   
  140.             }   
  141.             else  
  142.             {   
  143.                 sqlw += "  A." + fld;   
  144.             }   
  145.             switch (foper)   
  146.             {   
  147.                 case "bw":   
  148.                     fldata += "%";   
  149.                     sqlw += " LIKE '" + fldata + "'";   
  150.                     break;   
  151.                 case "eq":   
  152.                     if (is_numeric(fldata))   
  153.                     {   
  154.                         sqlw += " = " + fldata;   
  155.                     }   
  156.                     else  
  157.                     {   
  158.                         sqlw += " = '" + fldata + "'";   
  159.                     }   
  160.                     break;   
  161.                 case "ne":   
  162.                     if (is_numeric(fldata))   
  163.                     {   
  164.                         sqlw += " <> " + fldata;   
  165.                     }   
  166.                     else  
  167.                     {   
  168.                         sqlw += " <> '" + fldata + "'";   
  169.                     }   
  170.                     break;   
  171.                 case "lt":   
  172.                     if (is_numeric(fldata))   
  173.                     {   
  174.                         sqlw += " < " + fldata;   
  175.                     }   
  176.                     else  
  177.                     {   
  178.                         sqlw += " < '" + fldata + "'";   
  179.                     }   
  180.                     break;   
  181.                 case "le":   
  182.                     if (is_numeric(fldata))   
  183.                     {   
  184.                         sqlw += " <= " + fldata;   
  185.                     }   
  186.                     else  
  187.                     {   
  188.                         sqlw += " <= '" + fldata + "'";   
  189.                     }   
  190.                     break;   
  191.                 case "gt":   
  192.                     if (is_numeric(fldata))   
  193.                     {   
  194.                         sqlw += " > " + fldata;   
  195.                     }   
  196.                     else  
  197.                     {   
  198.                         sqlw += " > '" + fldata + "'";   
  199.                     }   
  200.                     break;   
  201.                 case "ge":   
  202.                     if (is_numeric(fldata))   
  203.                     {   
  204.                         sqlw += " >= " + fldata;   
  205.                     }   
  206.                     else  
  207.                     {   
  208.                         sqlw += " >= '" + fldata + "'";   
  209.                     }   
  210.                     break;   
  211.                 case "ew":   
  212.                     sqlw += " LIKE '" + fldata + "%'";   
  213.                     break;   
  214.   
  215.                 case "cn":   
  216.                     sqlw += " LIKE '%" + fldata + "%'";   
  217.                     break;   
  218.             }   
  219.         }   
  220.         //Response.Write(sqlw);   
  221.         if (sqlw != "")   
  222.         {   
  223.             sqlw=(SqlAnd!=""? SqlAnd+" and "+sqlw : " where "+sqlw);   
  224.         }   
  225.         else  
  226.         {   
  227.             sqlw = (SqlAnd != "" ? SqlAnd  :"");   
  228.         }   
  229.         Sql = Sql + sqlw + "  ORDER BY " + sidx + " " + sord + " LIMIT " + start + " , " + limit;   
  230.         DataTable datasinv = as_init.as_DataTab(Sql);   
  231.         count = as_init.AS_GetValue(countSql + sqlw);   
  232.         string dataJs = as_init.GetJson(datasinv, page, count, Edit, Add);   
  233.         Response.ContentType = "text/plain";   
  234.         Response.Buffer = true;   
  235.         Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);   
  236.         Response.AddHeader("pragma""no-cache");   
  237.         Response.AddHeader("cache-control""");   
  238.         Response.CacheControl = "no-cache";   
  239.         Response.Write(dataJs);   
  240.     }   
  241.   
  242.     
  243.   

 

欢迎访问编程之路,请在评论时遵守国家相关法律法规。评论不代表本站观点

  • 访客名称:

2010 编程之路 www.chr114.com All Rights Reserved