How to access viewstate in an ajax call

Posted by Sharpcnet under ASP.NET AJAX on 2/9/2014 | Points: 10 | Views : 7881 | Status : [Member] | Replies : 3
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;
}
}





Responses

Posted by: Sravan661 on: 2/10/2014 [Member] Bronze | Points: 25

Up
0
Down
Hi,
To access viewstate in Static Method
You can get the reference to the page via HttpContext.CurrentHandler.

public static string GetData(int CustomerID) {
Page page = HttpContext.Current.Handler as Page;
if (page != null)
{
string outputToReturn = "";
page.ViewState["MyVal"]="Hello";
return outputToReturn;
//btw, what a strange method!
}
}

OR
http://stackoverflow.com/questions/14950993/view-state-in-not-working-inside-the-static-method
OR
you can use UpdatePanel that let's you do Ajax. It will prevent full page reloads.
More over
Instead of hitting the database for every sort you better take the entire data from SP to a GenericList and store it in a viewstateand for every sort you can retreive data from viewstate and sort the list and update the datasource of grid.
In the ajax call you can use UpdatePanel to avoid full page reloads so use Update panel.

sravan

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sharpcnet on: 2/11/2014 [Member] Starter | Points: 25

Up
0
Down
Thank you sravan. I'm looking at this code from the link:
private static T GetViewState<T>(string name)
{
return (T) ((BasePage)HttpContext.Current.CurrentHandler).PageViewState[name];
}

Could you help, how I can use this to get the viewstate variable. The page name is Practice.aspx. The string stored in Viewstate["vSortDir"] is "desc".

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 2/11/2014 [Member] Starter | Points: 25

Up
0
Down
USE <%= this.vSortDir %> in aspx page:
Example:
//inside javascript function
var myStr = '<%= this.vSortDir %>';

I believe this is what u r looking for...

http://hashtagakash.wordpress.com/

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response