Maintaining GridView Position After Postback

J

Jason

Hello,

I have a web page with a GridView. When I scroll down to the entry I
desire, then click submit, I would like the grid to maintain this
position and show it in the browser after the postback. Currently the
grid is displaying the first row, not the row I selected before the
postback.

How do I do this?

Thought this might work:

MaintainScrollPositionOnPostBack="true"

but it does not seem to work.

-Jason
 
G

Guest

Hello,

I have a web page with a GridView.  When I scroll down to the entry I
desire, then click submit, I would like the grid to maintain this
position and show it in the browser after the postback.  Currently the
grid is displaying the first row, not the row I selected before the
postback.

How do I do this?

Thought this might work:

MaintainScrollPositionOnPostBack="true"

but it does not seem to work.

-Jason

It seems that you did something wrong there. The
MaintainScrollPositionOnPostBack property does exactly what you need.
Let's do a test

Put this to the aspx

<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication1.WebForm3"
MaintainScrollPositionOnPostBack="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:gridview id="GW" runat="server"
AutoGenerateSelectButton="true" selectedindex="0" >
<selectedrowstyle backcolor="LightCyan"
forecolor="DarkBlue"
font-bold="true"/>
</asp:gridview>
<asp:Button ID="Button1" runat="server" Text="Button" /> </form>
</body>
</html>

and in to code-behind

protected void Page_Load(object sender, EventArgs e)
{
DataSet oDs = new DataSet();
oDs.ReadXml(Request.PhysicalApplicationPath +
"XMLFile1.xml");
GW.DataSource = oDs;
GW.DataBind();
}

I used xml as a datasource but you can change it to your own
datasource.

For me this example returns the same scrolling position as before the
postback.

Please check.
 
J

Jerry T.

It seems that you did something wrong there. The
MaintainScrollPositionOnPostBack property does exactly what you need.
Let's do a test


MaintainScrollPositionOnPostBack only maintains the scroll position
within the page itself, not within the GridView control.

I'm trying to find a method for this, myself, right now. I have a
grid that starts off with a certain # of rows and then once a row is
clicked, a FormView will show below the grid and the grid height is
shortened. This means the clicked row could possibly be on a
subsequent page. My row highlighting works after I mouseover the row
but, as a default, it seems the GridView is placing the "cursor" (so
to speak) on the first row in the grid, not the .SelectedIndex row.

-- Jerry
 
J

Jerry T.

I got it working for my own purposes. I'm doing some special stuff (as
I mentioned above) but the key seems to be making sure
the .SelectedIndex is set properly and in the DataBound event having
to reassign the .SelectedIndex back to itself (kludgy, but it works)


Protected Sub grdMyGrid_DataBound(ByVal sender As Object, ByVal e
As System.EventArgs) Handles grdMyGrid.DataBound
If Not IsBlankGrid(sender) Then
If Not (Me.grdMyGrid.SelectedRow Is Nothing) Then
Me.grdMyGrid.SelectedIndex =
Me.grdMyGrid.SelectedRow.RowIndex
End If
End If

AddEmptyRows(sender)
End Sub


Protected Sub grdMyGrid_RowClicked(ByVal sender As Object, ByVal e
As XcalaburControls.GridViewRowClickedEventArgs) Handles
grdMyGrid.RowClicked
If (grdMyGrid.SelectedIndex <> -1) AndAlso
(grdMyGrid.SelectedIndex <= grdMyGrid.Rows.Count) Then
If (grdMyGrid.SelectedIndex Mod 2 = 0) Then
grdMyGrid.SelectedRow.Attributes("onmouseout") =
"this.className='RowStyleNoBorder';this.style.textDecoration='none';"
Else
grdMyGrid.SelectedRow.Attributes("onmouseout") =
"this.className='AltRowStyleNoBorder';this.style.textDecoration='none';"
End If
End If

Me.grdMyGrid.SelectedIndex = e.Row.RowIndex

e.Row.Attributes("onmouseout") =
"this.className='RowHoverNoBorder';this.style.textDecoration='none';"

If (Not frmNoteDetail.Visible) Then
Dim selectKey As Integer = grdMyGrid.SelectedIndex
Dim selectNewPage As Integer

grdMyGrid.PageSize = grdPageSizeNew
frmNoteDetail.Visible = True

selectNewPage = CInt(selectKey / grdPageSizeNew)
If selectKey Mod grdMyGrid.PageSize = 0 Then selectNewPage
-= 1

grdMyGrid.PageIndex = selectNewPage
selectKey = selectKey Mod grdPageSizeNew
grdMyGrid.SelectedIndex = selectKey
End If
End Sub


Protected Sub grdMyGrid_RowDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
grdMyGrid.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
e.Row.Attributes.Add("onmouseover",
"this.className='RowHoverNoBorder';")

If (e.Row.RowIndex = grdMyGrid.SelectedIndex) Then
e.Row.Attributes.Add("onmouseout",
"this.className='RowHoverNoBorder';this.style.textDecoration='none';")
Else
If (e.Row.RowIndex Mod 2) = 0 Then
e.Row.Attributes.Add("onmouseout",
"this.className='RowStyleNoBorder';this.style.textDecoration='none';")
Else
e.Row.Attributes.Add("onmouseout",
"this.className='AltRowStyleNoBorder';this.style.textDecoration='none';")
End If
End If
End If
End Sub
 
G

Guest

MaintainScrollPositionOnPostBack only maintains the scroll position
within the page itself, not within the GridView control.

I'm trying to find a method for this, myself, right now.  I have a
grid that starts off with a certain # of rows and then once a row is
clicked, a FormView will show below the grid and the grid height is
shortened.  This means the clicked row could possibly be on a
subsequent page.  My row highlighting works after I mouseover the row
but, as a default, it seems the GridView is placing the "cursor" (so
to speak) on the first row in the grid, not the .SelectedIndex row.

-- Jerry

He didn't say anything about how his grid is built. For "standard"
grids MaintainScrollPositionOnPostBack does exactly what its name
said. If you look at my example you will see that position will be the
same after a post back.
 
K

Konstantin Salavatov

to have SelectedIndex property working, you need to have you grid filled
with data on post back load too, not only on initial page load. Although it
might look that grid persists its content, it done through viewstate, if it
was not rebound to data and SelectedIndex doesn't work.
 
J

Jerry T.

He didn't say anything about how his grid is built. For "standard"
grids MaintainScrollPositionOnPostBack does exactly what its name
said. If you look at my example you will see that position will be the
same after a post back.- Hide quoted text -

The title and the OP both mention using a GridView. This is the
ASP.NET control, not some custom-written grid.

The OP reads that the grid, itself, has a scrollbar, separate from the
page. MaintainScrollPositionOnPostBack will maintain the position of
an entire page (so, for example, if the grid was toward the bottom of
a page that required a user to scroll the entire page in order to see
it, MaintainScrollPositionOnPostBack would return the user to the
proper position within the page to see the grid) But, in this case,
the OP was wanting a way to maintain the highlighted row within the
grid after a postback. The code I posted is but one way, albeit a
very common way, to accomplish this and it also serves as an example
to show how a grid can be dynamically resized re: the PageSize and the
proper row highlighting can be maintained.

Jerry
 
G

Guest

The title and the OP both mention using a GridView.  This is the
ASP.NET control, not some custom-written grid.

The OP reads that the grid, itself, has a scrollbar, separate from the
page.  MaintainScrollPositionOnPostBack will maintain the position of
an entire page (so, for example, if the grid was toward the bottom of
a page that required a user to scroll the entire page in order to see
it, MaintainScrollPositionOnPostBack would return the user to the
proper position within the page to see the grid)  But, in this case,
the OP was wanting a way to maintain the highlighted row within the
grid after a postback.  The code I posted is but one way, albeit a
very common way, to accomplish this and it also serves as an example
to show how a grid can be dynamically resized re: the PageSize and the
proper row highlighting can be maintained.

Jerry

Jerry, maybe I got it wrong but where do you see "that the grid,
itself, has a scrollbar"?
 
J

Jerry T.

Jerry, maybe I got it wrong but where do you see "that the grid,
itself, has a scrollbar"?- Hide quoted text -

- Show quoted text -

I was making an assumption since that's a common issue with GridView
controls when they have their own scrollbar. Although, the scrollbar
typically comes from a Panel control acting as a parent container to
the GridView.

Like in the application I'm currently developing, we are trying to
keep the page itself from being scrollable so some pages end up with a
GridView inside a Panel control with a limited height and the
ScrollBars attribute of the Panel set to Vertical so only the GridView
scrolls, not the entire page.

Another option is to setup paging on the GridView but that can be more
time-consuming for a user to find the appropriate data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top