How can I implement jQuery DataTables plugin using C#, ASP.NET, SQL Server side processing?

后端 未结 3 1923
无人共我
无人共我 2020-12-08 03:51

How can I implement jQuery DataTables plugin using C#, ASP.NET, SQL Server side processing with ajax and webservices?

Would like to implement a Datatables grid using

3条回答
  •  温柔的废话
    2020-12-08 04:13

    This version is for older SqlServer, For newer version ,try the other one.

    This technique uses stored procedure, you can improve the performance by using other methods than #temp Main features are

    • Stored Procedure
    • Injection Free and easily adaptable sql structure
    • Ajax

    Functional and Very Useful

    Step 1:(HTML)

     
     
     
    
     
    
     
    # Name Base Discount Additional Discount Special Discount Action
    # Name Base Discount Additional Discount Special Discount Action

    STEP :2 (Stored Procedure)

    Create procedure [dbo].[category_post]
     @srno int=null, -- from here 
     @user_srno int=null,
     @catSrno int=null,
     @name varchar(200)=null,
     @baseDiscount numeric(18,2)=null,
     @additionalDiscount numeric(18,2)=null,
     @specialDiscount numeric(18,2)=null,
     @status int null,
     @Action_by int null,
     @option varchar(20) = null, -- to here personnel parameters
     @orderColumn  int =null, 
     @orderDir  varchar(20)=null,
     @start  int =null,
     @limit  int =null,
     @searchKey varchar(20) -- personnel parameter
      as 
      BEGIN
    
    
    
        select IDENTITY(int,1,1) as SnoID, null as abc,specialDiscount, additionalDiscount, baseDiscount, name,cast(srno as varchar(20)) as srno
                --this method is userful for all sql server version (it can be made better by using fetch)
        into #tempCategory
         from categoryStd where [status] not in(4,14) and categoryStd.name like '%'+@searchKey+'%'
    
        declare @to as int = @start+@limit  
    
         select * from #tempCategory where SnoID>@start and SnoID<=@to
    
            order by
                        CASE WHEN @orderColumn = 1 AND @orderdir = 'desc' THEN #tempCategory.[name] END DESC,
                        CASE WHEN @orderColumn = 1 AND @orderdir = 'asc' THEN #tempCategory.[name] END ASC,
                        CASE WHEN @orderColumn = 2 AND @orderdir = 'desc' THEN #tempCategory.[name] END DESC,
                        CASE WHEN @orderColumn = 2 AND @orderdir = 'asc' THEN #tempCategory.[name] END ASC
    
    
    
            select count(*) from #tempCategory
    
    
    END
    

    STEP:3 (AJAX Page) C# form

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Configuration;
    using AppBlock;
    using System.Data.SqlClient;
    using Newtonsoft.Json;
    
    namespace alfest.Ajax
    {
      public partial class Category : System.Web.UI.Page
      {
        string mode, option, user, limit, start, searchKey, orderByColumn, orderByDir, estMstSrno, pnlsrno, draw, jsonString;
        CommonClass cmnCls = new CommonClass();
        protected void Page_Load(object sender, EventArgs e)
        {
          mode = Request.QueryString["mode"] == null ? "" : Request.QueryString["mode"].ToString();
          option = Request.QueryString["option"] == null ? "" : Request.QueryString["option"].ToString();
          limit = Request.QueryString["length"] == null ? "" : Request.QueryString["length"].ToString();
          start = Request.QueryString["start"] == null ? "" : Request.QueryString["start"].ToString();
          user = Request.QueryString["user"] == null ? "" : Request.QueryString["user"].ToString();
          searchKey = Request.QueryString["search[value]"] == null ? "" : Request.QueryString["search[value]"].ToString();
          orderByColumn = Request.QueryString["order[0][column]"] == null ? "" : Request.QueryString["order[0][column]"].ToString();
          orderByDir = Request.QueryString["order[0][dir]"] == null ? "" : Request.QueryString["order[0][dir]"].ToString();
          estMstSrno = Request.QueryString["estMstSrno"] == null ? "" : Request.QueryString["estMstSrno"].ToString();
          pnlsrno = Request.QueryString["pnlsrno"] == null ? "" : Request.QueryString["pnlsrno"].ToString();
          draw = Request.QueryString["draw"] == null ? "" : Request.QueryString["draw"].ToString();
    
    
    
           // Cls_Category CatgObj = new Cls_Category();
           // CatgObj.orderColumn = Convert.ToInt32(orderByColumn);
           // CatgObj.limit = Convert.ToInt32(limit);
           // CatgObj.orderDir = orderByDir;
           // CatgObj.start = Convert.ToInt32(start);
           // CatgObj.searchKey = searchKey;
           // CatgObj.option = "GetAllAdminCategory";
    
          // or user your own method to get data (just fill the dataset)
    
          //  DataSet ds = cmnCls.PRC_category(CatgObj);
    
            dynamic newtonresult = new
              {
                status = "success",
                draw = Convert.ToInt32(draw == "" ? "0" : draw),
                recordsTotal = ds.Tables[1].Rows[0][0],
                recordsFiltered = ds.Tables[1].Rows[0][0],
                data = ds.Tables[0]
              };
            jsonString = JsonConvert.SerializeObject(newtonresult);
    
            Response.Clear();
            Response.ContentType = "application/json";
            Response.Write(jsonString);
    
        }
      }
    }
    

    FINAL RESULT :

提交回复
热议问题