Avatar of NorthReno
NorthRenoFlag for United States of America asked on

Retain Gridview position (SelectedItem) after a refresh

I am using a Gridview in ASP.NET 2.0 with Visual Basic 2005.  The gridview is composed of drop down lists, textboxes and labels.  Each row contains the contents of one row from a table in a database.  It is displayed so the user can edit or delete the row or rows.

After the initial criteria is entered, the gridview is populated with data meeting the basic criteria.  Then the user selects the specific row they wish to edit by clicking "Select" on the left most column of the row.

When the "Select" is clicked, the gridview refreshes and the row they selected has changed color so it is easy to identify.  However, after the refresh the gridview displays as it originally did with the original first row at the top of the list.

If there are 200 rows and the user selects row 190, after the refresh the user has to scroll all the way down to row 190 again in order to work on it.  What I would like is for the row to display in the active window so the user is not forced to scroll down to find it.

How can this be accomplished?  it seem like it should be simple... bit I am completely flummoxed.

Thanks in advance
.NET ProgrammingVisual Basic.NETASP.NET

Avatar of undefined
Last Comment
NorthReno

8/22/2022 - Mon
rafayali

In the aspx page, you can use the Page object's MaintainScrollPositionOnPostBack property.

Example:

In the page declarations at the top put this
<%@Page Language="" MaintainScrollPositionOnPostBack="true"


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" MaintainScrollPositionOnPostBack="true" %>

Open in new window

rafayali

Additionally, to select a row what you can do is to store the selected row's index in session and retrieve it whenever you need it. Here is some sample code:

In Row_Created event do this:

  If e.Row.RowIndex = CType(Session("selectedrow"), Int32) Then
            e.Row.RowState = DataControlRowState.Selected
        End If

In SelectedIndexChanged event of Gridview do this:

  Session("selectedrow") = GridView1.SelectedIndex

This should help.
ASKER CERTIFIED SOLUTION
rafayali

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
NorthReno

I utilized concepts from both solutions presented.  I set the focus on a control in the GridView1_SelectedIndexChanged event like this:
GridView1.SelectedRow.Controls(0).Focus().  It works great.
Thanks you two!
Your help has saved me hundreds of hours of internet surfing.
fblack61