Tuesday, March 8, 2011

GridView paging, retaining the old page afetr navigating

Hi,
This can be achived by storing the page index in the session variable and reassign the page index to the grid while populating data back.
Here I have an example how we can achive this .

AspxPage:(RememberPageGrid.aspx)

<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        if (!IsPostBack) 
        { 
            if (Session["PageIndex"] != null && !string.IsNullOrEmpty(Session["PageIndex"].ToString())) 
                GridView1.PageIndex = (int)Session["PageIndex"]; 
        } 
    } 
 
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
        Session["PageIndex"] = e.NewPageIndex; 
    } 
 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        Response.Redirect("OtherPage.aspx"); 
    } 
     
</script> 
<html> 
<head id="Head1" runat="server"> 
    <title>GridView example to remember page number</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:GridView ID="GridView1" runat="server" DataKeyNames="ProductID" AutoGenerateColumns="False" 
            AllowPaging="True" AllowSorting="True" OnPageIndexChanging="GridView1_PageIndexChanging" 
            DataSourceID="sqlDataSource"> 
            <Columns> 
                <asp:BoundField ReadOnly="True" HeaderText="ProductID" DataField="ProductID" SortExpression="ProductID"> 
                </asp:BoundField> 
                <asp:BoundField HeaderText="ProductName" DataField="ProductName" SortExpression="ProductName"> 
                </asp:BoundField> 
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" /> 
            </Columns> 
        </asp:GridView> 
        <asp:SqlDataSource ID="sqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Alphabetical list of products]"> 
        </asp:SqlDataSource> 
    </div> 
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Go to some other page" /> 
    </form> 
</body> 
</html>

RedirectedPage(OtherPage.aspx):

<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 
 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
        Response.Redirect("RememberPageGrid.aspx"); 
    } 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:Button ID="Button1" runat="server" Text="Go back" onclick="Button1_Click"  
            style="height: 26px" /> 
    </div> 
    </form> 
</body> 
</html>

No comments:

Post a Comment