New to Ajax and here's a question.
The Aspx page has a grid view (with sorting allowed). Below the grid is a dropdownlist with page no.s. User can select a number to go to that page. At initial page load, the grid shows records, with column - 'OrderId' sorted in 'desc' order.
The current sorted order and the name of this column are stored as ViewState variables. When any column is sorted, these variables are updated.
The stored procedure to get the data, accepts columnName, SortOrder, PageNo. and Page size as parameters.
This code is working fine. Now, I would like to use this with an Ajax call. If changed to 'public static', it cannot access the ViewState variables.
So, please guide me how I can use this function with Ajax.
If it is only possible that the ColumnName & sortOrder should be sent as parameters from the Ajax function, then how to do that.
private void BindOrders(int index)
{
int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["gridPageSize"]);
SqlCommand cmd = new SqlCommand("GetOrderDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@SortOrder", ViewState["vSortDirection"].ToString());
cmd.Parameters.AddWithValue("@ColName", ViewState["vSortExpression"].ToString());
cmd.Parameters.AddWithValue("@StartIndex", index);
cmd.Parameters.AddWithValue("@PageSize", pageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
con.Open();
da.Fill(dt);
con.Close();
int rowcount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
if (rowcount > 0)
{
gvOrders.DataSource = dt;
gvOrders.DataBind();
int n = (int)Math.Ceiling((decimal)rowcount / (decimal)pageSize);
ddlPage.Items.Clear();
for (int i = 1; i <= n; i++)
{
ddlPage.Items.Add(new ListItem(i.ToString(), i.ToString()));
}
ddlPage.SelectedIndex = index - 1;
}
}