tags:

views:

161

answers:

2

Hi,

I am using vb.net code in which I am having a gridview control. Please see the below code

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" CssClass="innerGridTable" DataKeyNames="OrgID" DataSourceID="OrgGridViewDataSource">
    <Columns>
     <asp:CommandField ShowSelectButton="True" ButtonType="Button" SelectText="Select"
      ControlStyle-CssClass="Button">
      <ControlStyle CssClass="Button"></ControlStyle>
     </asp:CommandField>
     <asp:BoundField DataField="OrgName" HeaderText="Organisation Name" SortExpression="OrgName">
     </asp:BoundField>
     <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type"></asp:BoundField>
     <asp:BoundField Visible="true" DataField="OrgID" HeaderText="OrgID" InsertVisible="False"
      ReadOnly="True" SortExpression="OrgID"></asp:BoundField>
     <asp:TemplateField Visible="false" HeaderText="RAOG ID">
      <ItemTemplate>
       <asp:Label ID="lblRAOGID" runat="server" Text='<%# Bind("RAOGID") %>'></asp:Label>
      </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField Visible="false" HeaderText="RAO ID">
      <ItemTemplate>
       <asp:Label ID="lblRAOID" runat="server" Text='<%# Bind("RAOID") %>'></asp:Label>
      </ItemTemplate>
     </asp:TemplateField>
     <asp:BoundField Visible="false" DataField="Name" HeaderText="Name" SortExpression="Name">
     </asp:BoundField>
    </Columns>
</asp:GridView>

Now in paging if user opens a record from 3rd page, it opens perfectally. The problem is that when he comes back to this page again it starts showing first page, infact it would have shown the 3rd page of gridview.

Please suggest the code the same!

Thanks

Best Regards, MS

A: 

Add a handler for the PageIndexChanged event of the gridview, and store it in a cookie, or in the session.

Sub gridview_PageIndexChanged(...) 
    Session("currentpagenameGridPage") = e.NewPageIndex
End Sub

Then in your page load do something like

Sub Page_Load() 
    If Not IsPostBack AndAlso Session("currentpagenameGridPage") <> "" Then 
        GridView.CurrentPage = Val(Session("currentpagenameGridPage"))
        YourReBindGridFunction()
    End IF
End Sub

Note that this code is NOT complete or polished... i am not sure about the name e.NewPageIndex, it may be a different name, but that's the basic idea.

eidylon
Any Idea what I can use instead of e.NewPageIndex?
MKS
Oh, actually I just looked it up, and the argument "e" in the PageIndexChanged doesn't have any useful properties. (I was thinking of the Telerik grid control). The proper thing to use is GridView.PageIndex. Use this property to both set and get the current page index of the grid.
eidylon
A: 

hi Guys,

I solved my problem with below code by the help of EIDYLON. Thanks Mate!

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    ibtnEdit.Visible = False
    lblBreadCrumb.Visible = False
    Session("CurrentGridPageIndex") = e.NewPageIndex
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Session("CurrentGridPageIndex") Is Nothing Then
        GridView1.PageIndex = Val(Session("CurrentGridPageIndex"))
        GridView1.DataBind()
    End If
End Sub

Cheers!

Thanks again for your help EIDYLON

MKS