Server Side Processing using asp.net -Control is only moving to my ajax source if I bind my gridview

Server Side Processing using asp.net -Control is only moving to my ajax source if I bind my gridview

sasha406sasha406 Posts: 10Questions: 0Answers: 0
edited November 2014 in Free community support

Hi guys,

I'm facing with a weird issue.
I have used server side processing for a grid view in our project's asp.net site. My ajax data source is an aspx page. I have to display a table of data when a button is clicked. In the GetSearchDetails.aspx page i am preparing the JSON required by quering the required records from ta SQL table . The problem is that the control is only moving to GetSearchDetails.aspx when I bind the grid on the server side of the aspx page where the datatables initialization takes place. In other words method that prepares the JSON is only triggering when I bind the grid. Is this an anomaly or is this normal ? if its not normal then what am I doing wrong ?

I can't post the webpage link as the webpage is present in an intranet site.

Client code

//////////////Client side Code
$('#<%= grdPCsbyLocation.ClientID %>').dataTable({

            "deferRender": true,
            "fnInitComplete": function (oSettings, json) {
                $('#divGrid').show();
                $('#gridLegend').show();
            },
            "bPaginate": true,

            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "GetSearchDetails.aspx?sSql=" + $('#<%= hdnsSQL.ClientID%>').val()});

Server code

////////////////////Server side Code
public void BindGridPCsbyLocation(string sSql)
{

        int echo = Int32.Parse(Request.Params["sEcho"]);
        int displayLength = Int32.Parse(Request.Params["iDisplayLength"]);
        int displayStart = Int32.Parse(Request.Params["iDisplayStart"]);
        string search = Request.Params["sSearch"];
        getCircleSizesfromDB();
        ///////////
        //SEARCH (filter)
        //- build the where clause
        ////////
        StringBuilder sb = new StringBuilder();
        string whereClause = string.Empty;
        if (!String.IsNullOrEmpty(search))
        {
            sb.Append(" WHERE MachineName LIKE '%");
            sb.Append(search);
            sb.Append("%' OR MaxAlertSeverity LIKE '%");
            sb.Append(search);
            sb.Append("%' OR AlertCount LIKE '%");
            sb.Append(search);
            sb.Append("%' OR Auto-Restart LIKE '%");
            sb.Append(search);
            sb.Append("%' OR ResourceId LIKE '%");
            sb.Append(search);
            sb.Append("%' OR UserId LIKE '%");
            sb.Append(search);
            sb.Append("%'");
            whereClause = sb.ToString();
        }

        ///////////////
        //ORDERING
        //- build the order by clause
        //////////////
        sb.Clear();
        string orderByClause = string.Empty;
        //Check which column is to be sorted by in which direction
        for (int i = 0; i < 11; i++)
        {
            if (Request.Params["bSortable_" + i] == "true")
            {
                sb.Append(Request.Params["iSortCol_" + i]);
                sb.Append(" ");
                sb.Append(Request.Params["sSortDir_" + i]);
            }
        }
        orderByClause = sb.ToString();
        //Replace the number corresponding the column position by the corresponding name of the column in the database
        if (!String.IsNullOrEmpty(orderByClause))
        {
            orderByClause = orderByClause.Replace("0", ", MaxAlertSeverity");
            orderByClause = orderByClause.Replace("1", ", UserId");
            orderByClause = orderByClause.Replace("2", ", MachineName");
            orderByClause = orderByClause.Replace("3", ", Auto-Restart");

            //Eliminate the first comma of the variable "order"
            orderByClause = orderByClause.Remove(0, 1);
        }
        else
            orderByClause = "MaxAlertSeverity ASC";
        orderByClause = "ORDER BY " + orderByClause;

        /////////////
        //T-SQL query
        //- ROW_NUMBER() is used for db side pagination
        /////////////
        sb.Clear();


        try
        {

            string query =  sSql;



            DataTable Results = ClsDBFunctions.GetDetailsFromDB(CommandType.Text, String.Format("select * from (select top {0} * from (select top {0}+{1} {2}))", displayLength, displayStart, sSql), null, ClsDBFunctions.GetDataSource()).Tables[0];
            /////////////
            /// JSON output
            /// - build JSON output from DB results
            /// ///////////
            sb.Clear();

        string outputJson = string.Empty;
        int totalDisplayRecords = 0;
        int totalRecords = 0;
        if (totalRecords == 0)
            totalRecords = Results.Rows.Count;
        if (totalDisplayRecords == 0)
            totalDisplayRecords = Results.Rows.Count;
        int cnt = totalDisplayRecords;
        int i = 0;

     foreach(DataRow row in Results.Rows)
     {

                sb.Append("[");
                sb.Append(string.Format("\"{0}\",", ShowCircle(Convert.ToInt32(row["MaxAlertSeverity"]),Convert.ToInt32(row["AlertCount"]))));
                sb.Append(string.Format("\"{0}\",",row["UserId"] ));
                sb.Append(string.Format("\"{0}\",", row["MachineName"]));
         //row["AutoRestart"]
                sb.Append(string.Format("\"{0}\",", getToggle(Convert.ToInt32(row["AutoRestart"]), row["ResourceId"].ToString(),i)));
                sb.Append(string.Format("\"{0}\",", row["ResourceId"]));
                sb.Append(string.Format("\"{0}\"", row["AlertCount"]));
                sb.Append("],");

                i++;
            }

     i = 0;
        outputJson = sb.ToString();
        outputJson = outputJson.Remove(outputJson.Length - 1);
        sb.Clear();

        sb.Append("{");
        sb.Append("\"sEcho\": ");
        sb.Append(echo);
        sb.Append(",");
        sb.Append("\"iTotalRecords\": ");
        sb.Append(totalRecords);
        sb.Append(",");
        sb.Append("\"iTotalDisplayRecords\": ");
        sb.Append(totalDisplayRecords);
        sb.Append(",");
        sb.Append("\"aaData\": [");
        sb.Append(outputJson);
        sb.Append("]}");
        outputJson = sb.ToString();

        /////////////
        /// Write to Response
        /// - clear other HTML elements
        /// - flush out JSON output
        /// ///////////
        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.Write(outputJson);
        Response.Flush();
        Response.End();
        }
        catch (Exception ex)
        {
            string x = ex.ToString();
        }
    }
This discussion has been closed.