I have been Googleing for an answer and have not yet found one so I thought I would ask this forum.
I have an application with a Gridview displaying pages of data with about 20 records on a page. For paging, I use First, Next, Previous, Last buttons. Each record has an Edit button. When the Edit button is pressed, the detail for the record is displayed for update on a separate page. The user can update data or press Cancel to return to the Gridview.
My problem is returning to the Gridview. Returning always returns to the first page of the Gridview. The user could be on page 50 of the Gridview, hit Edit on a record, get the detail displayed, decide to cancel or update and gets returned to page 1 of the Gridview.
Is there a way to return to the Gridview page from which the detail was generated and not page 1?
I'll keep looking but if anyone has a solution, I would very much appreciate it.
Loading
Kirtan PatelPosted Jan 15, 2010, 8:42 PM
here i m giving your solution if any problem arise let me know :)
friend, if my answer help you then please check "Do you like this answer" check box :-)
when you redirected by the Edit Button just Preserve the CurrentGridView page index in Session
now when you will be back to the same page after pressing Cancel button .. set the index of gridView to the pageindex that we saved in session
code will be like below
most of code i have written comments you will understand code your self by referring to comments written :-)
Code For Page Which is having GridView
-----------------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
if (Session["currentpage"] != null)
{
/*Set current page = retrvited query string from cancel button
if we dont have any current page in session then try catch will handle it */
int currentpage = Convert.ToInt32(Session["currentpage"].ToString());
GridView1.PageIndex = currentpage;
/*remove the session variable after setting index so next time if
page normally load then it will not set pageindex automatically */
Session.Remove("currentpage");
}
}
catch { }
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRecord")
{
string current_Page = GridView1.PageIndex.ToString();
//Store Current page index in session and Redirect page for editing
Session["currentpage"] = current_Page;
Response.Redirect("Edit.aspx");
}
}