return to web page position

kdeutsch
kdeutsch used Ask the Experts™
on
Ok, my users gave me a request that I don't think I can do but I am asking the experts.  I have a datagrid with aoout 10,000 entries and they want to see everyone on the first page and not page them.  but I have a manage button that hides the datagrid, they change the information and then the datgrid refreshes to show the information  but when that happens if they are on row 500 they want to go back to that row and not start at the top agian.  Still using asp.net 1.1 on this project.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kyle AbrahamsSenior .Net Developer
Commented:
you can do it.  Just grab the page that you're on before you refresh.

page = Grid.page
'do refresh
if count <= Grid.Pages.Count
  Grid.Page = count
else
   Grid.Page = 0
end if
If the users are aware of what row they were on, you could add a 'jump to' link / textbox combination, using the <a name="Row_500"></a>[REST OF DATA].

If they dont know which row they were editing, it may be worth having a look at this article;
http://www.4guysfromrolla.com/articles/111704-1.aspx

Thanks
AdministratorTools_Net
Christopher KileSenior Software Analyst
Commented:
You'll have to use Javascript to do this in the web page itself, which means you'll have to identify the HTML table generated to display the DataGrid, assign IDs to every row on every page refresh.  Also, you might want to attach an onfocus() handler to each table cell that saves the row ID to a hidden text field for use on the next postback.  

When you post back the page, you attach Javascript to the page that retrieves the table row object by its ID, gets its .scrollLeft and .scrollTop values, then you use the window.scroll(x, y) function to scroll the window down until the requested row is displayed.

If this is enough for you to get the job done, great; otherwise, I'll be glad to help you develop the code you need.  In order for you to identify the table used for displaying the DataGrid, right-click on the page when it's displayed and select View Source, then search for the DIV with the same ID as your DataGrid.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
ged325:
Where would I put this code, in the page load.  I have a link button that hides the datagrid, and brings another panel to the front.  THey change the information and then the datgrid reloads with the refreshed data.

To refresh I have a linkbutton event that aves the changes or allows them to cancel changes and sends them back to previous page.


cpkilekofp,
Well I woould have to rely on a lot of your help, I have little javascript experience and tend not to use unless its a little function.


administratortools_net,
I already have a full datagrid , is there a way to capture the row number and go back to that number??
The link gives me a 404 error
 Private Sub myDataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles myDataGrid.ItemCommand

        If (e.CommandName = "Manage") Then
            'Get the Id and put in Hidden field
            lblHidden.Text = e.Item.Cells(1).Text
            Dim TaskName As String = e.Item.Cells(3).Text
            Dim Name As String = e.Item.Cells(7).Text
            Dim level As String = e.Item.Cells(11).Text
            Dim Notes As String = e.Item.Cells(12).Text
            pnlUnitSearch.Visible = False

            lblname.Text = "Manage Task  " & TaskName & " for  " & Name

            If Notes = "&nbsp;" Then
                Notes = ""
            End If

            txtNotes.Text = Notes

            If checkJ1Admin() = False And checkJ3Admin() = False Then
                ' Disable the link and then take away its attributes so they cannot click on it
                cbOverride.Visible = False
            Else
                cbOverride.Visible = True
            End If


            If (checkJ1Admin() = True Or checkJ3Admin() = True) Then
                cbCloseTask.Visible = True
            ElseIf (checkBDAdmin() = True AndAlso (level = "Brigade" Or level = "Battalion" Or level = "Unit")) Then
                cbCloseTask.Visible = True
            ElseIf (checkBNAdmin() = True AndAlso (level = "Battalion" Or level = "Unit")) Then
            ElseIf (level = "Unit") Then
                cbCloseTask.Visible = True
            Else
                cbCloseTask.Visible = False
            End If

            pnlDatagrid.Visible = False
            pnledit.Visible = True

        End If
    End Sub



Private Sub lnkSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSubmit.Click
        Dim userLogon As String = Split(Current.User.Identity.Name, "\")(1)
        Dim dtClosed As String = Date.Now

        sql = "Update tblPermTask set dtCompleted = " & IIf(cbCloseTask.Checked, "'" & dtClosed.ToString() & "'", "NULL") & ", " _
            & "strNotes = '" & Replace(txtNotes.Text, "'", "") & "', strClosedBy = " & IIf(cbCloseTask.Checked, "'" & userLogon & "'", "NULL") & ", " _
            & "bitOverride = " & IIf(cbOverride.Checked, "1", "NULL") & " where intPermTaskId = " & lblHidden.Text & ""

        insertUpdateDelete(sql)

        pnledit.Visible = False
        pnlDatagrid.Visible = True

        If ddlDeployment.SelectedValue = 0 Then

            FillDatagrid2()
        Else
            FillDataGrid()
        End If


        lblHidden.Text = String.Empty
        lblname.Text = String.Empty
        txtNotes.Text = String.Empty
        cbOverride.Checked = False
        cbCloseTask.Checked = False
        pnlUnitSearch.Visible = True

    End Sub

Open in new window

No worries! :) I've put the code from the page below. Essentially the below example is for a postback, which is what i believe you have running.

When the page loads, it will then automatically scroll to the stored position. Depending on the layout of your page, you could store the current scroll position in a session variable and then scroll to this on a page load if the below isn't what you were after.

<script language = "javascript">
<!--
   function sstchur_SmartScroller_GetCoords()
   {
      var scrollX, scrollY;
      
      if (document.all)
      {
         if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
         else
            scrollX = document.documentElement.scrollLeft;
               
         if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
         else
            scrollY = document.documentElement.scrollTop;
      }   
      else
      {
         scrollX = window.pageXOffset;
         scrollY = window.pageYOffset;
      }
   
      document.forms[formID].xCoordHolder.value = scrollX;
      document.forms[formID].yCoordHolder.value = scrollY;
   }
   
   function sstchur_SmartScroller_Scroll()
   {
      var x = document.forms[formID].xCoordHolder.value;
      var y = document.formsformID].yCoordHolder.value;
      window.scrollTo(x, y);
   }
   
   window.onload = sstchur_SmartScroller_Scroll;
   window.onscroll = sstchur_SmartScroller_GetCoords;
   window.onkeypress = sstchur_SmartScroller_GetCoords;
   window.onclick = sstchur_SmartScroller_GetCoords;
// -->
<script>

Open in new window

Christopher KileSenior Software Analyst

Commented:
kdeutsch,

administratortools_net is using the same technique that I'm using, and from the look of the code it appears he got his code from the same source that I did.  I've modified the functions to fit my own usage, but you should be able to use his code to achieve the same thing.
Kyle AbrahamsSenior .Net Developer

Commented:
You would do my suggesting right before the datagrid is refreshed.   Code below should work for remember grid while paging.

If you wanted to do it in a single page you can simply say:  (or you could combine)
<%@ Page language="C#" maintainscrollpositiononpostback=true %>

(I believe smart navigation also works)

If you added AJAX to the page, that would definitely clear up issues regarding one page.

Sub RemeberPage
  session("PageIndex") = myGrid.CurrentPageIndex 
End Sub

Sub RecallPage
   dim page as integer
   if session("PageIndex") <> nothing then
      page = Session("PageIndex")
      if page < mygrid.PageCount
            mygrid.CurrentPageIndex = page
      else
            mygrid.CurrentPageIndex = mygrid.PageCount - 1
      end if
      mygrid.databind()
   end if
end sub

Open in new window

Kyle AbrahamsSenior .Net Developer

Commented:
Call remember page right before here:
  If ddlDeployment.SelectedValue = 0 Then

call recall page in your fillgrid subs.  (do it right before your databind).

Would the number of rows change between:
            FillDatagrid2()
            FillDataGrid()
(EG: just different columns or do rowcounts change?)  If so remember the grid is kind of useless when switching between the 2.  

Author

Commented:
ged325,

Ok here is the 2 subs you gave me changed as so.  The problem is that I guess I am not getting it right.  I put the remeber sub as part of the Manage link button, when that is clicked  it should rememeber where on page it was.  The recall i put as part of the datagrid by databind.  The rows could change if they close a record but as long as they get near where they where its not a problem.

 Private Sub RemeberPage()
        Session("PageIndex") = myDataGrid.CurrentPageIndex
    End Sub

    Private Sub RecallPage()
        Dim page As Integer
        If Session("PageIndex") <> Nothing Then
            page = Session("PageIndex")
            If page < myDataGrid.PageCount Then
                myDataGrid.CurrentPageIndex = page
            Else
                myDataGrid.CurrentPageIndex = myDataGrid.PageCount - 1
            End If
            myDataGrid.DataBind()
        End If
    End Sub




 If (e.CommandName = "Manage") Then
            'Get the Id and put in Hidden field
            lblHidden.Text = e.Item.Cells(1).Text
            Dim TaskName As String = e.Item.Cells(3).Text
            Dim Name As String = e.Item.Cells(7).Text
            Dim level As String = e.Item.Cells(11).Text
            Dim Notes As String = e.Item.Cells(12).Text
            pnlUnitSearch.Visible = False

            RemeberPage()

            lblname.Text = "Manage Task  " & TaskName & " for  " & Name

            If Notes = "&nbsp;" Then
                Notes = ""
            End If

            txtNotes.Text = Notes

            If checkJ1Admin() = False And checkJ3Admin() = False Then
                ' Disable the link and then take away its attributes so they cannot click on it
                cbOverride.Visible = False
            Else
                cbOverride.Visible = True
            End If


            If (checkJ1Admin() = True Or checkJ3Admin() = True) Then
                cbCloseTask.Visible = True
            ElseIf (checkBDAdmin() = True AndAlso (level = "Brigade" Or level = "Battalion" Or level = "Unit")) Then
                cbCloseTask.Visible = True
            ElseIf (checkBNAdmin() = True AndAlso (level = "Battalion" Or level = "Unit")) Then
            ElseIf (level = "Unit") Then
                cbCloseTask.Visible = True
            Else
                cbCloseTask.Visible = False
            End If

            pnlDatagrid.Visible = False
            pnledit.Visible = True

        End If
    End Sub


Private Sub FillDataGrid()
        Dim DeployId As String = Convert.ToInt16(ddlDeployment.SelectedValue.ToString())
        Dim Uic As String = ddlUic.SelectedValue
        Dim Filter As String = ddlFilter.SelectedValue
        Dim Bn As String = ddlBn.SelectedValue
        Dim theCounter As Integer
        Dim SqlWhere As String
        lnkSoldierCancel.Visible = True
        pnlDatagrid.Visible = True


        If CInt(DeployId) > 0 And Bn = "0" And ddlUic.SelectedValue = "" Then
            SqlWhere = "ud.intMobilizationId = " & ddlDeployment.SelectedValue
        ElseIf CInt(DeployId) > 0 And ddlBn.SelectedValue > "" And ddlUic.SelectedValue = "0" Then
            SqlWhere = "ud.intMobilizationId = " & ddlDeployment.SelectedValue & " and ud.strAAUic = '" & ddlBn.SelectedValue & "'"
        ElseIf CInt(DeployId) > 0 And ddlBn.SelectedValue > "" And CInt(ddlUic.SelectedValue) > 0 And ddlFilter.SelectedValue = "0" Then
            SqlWhere = "ud.intMobilizationId = " & ddlDeployment.SelectedValue & " and ud.strAAUic = '" & ddlBn.SelectedValue & "' and ud.intUnitMobId = " & ddlUic.SelectedValue
        ElseIf CInt(DeployId) > 0 And ddlBn.SelectedValue > "" And CInt(ddlUic.SelectedValue) > 0 And CInt(ddlFilter.SelectedValue) > 0 Then
            SqlWhere = "ud.intMobilizationId = " & ddlDeployment.SelectedValue & " and ud.strAAUic = '" & ddlBn.SelectedValue & "' and ud.intUnitMobId = " & ddlUic.SelectedValue & " and " _
                & "sf.intFilterId = " & ddlFilter.SelectedValue
        End If


        'Fill the datagrid from the filter drop down menu

        sql = "Select t.intPermTaskID, ud.strUic, t.strTaskName, f.strPriority, f.strFilterPage, Convert(Varchar(12), t.dtFound, 106) as dtFound, t.strPositionData, t.strSoldierData, t.strName, " _
            & "f.strApprovalLevel, t.strNotes From tblPermTask as t INNER JOIN tblSoldierFilters as sf on sf.intAssignedId = t.intAssignedId INNER JOIN tblFilter as f on f.intFilterID = " _
            & "t.intFilterID INNER JOIN tblUnitPersonnel as up on up.strSSN = t.strSSN Inner Join tblUnitPosition as p on p.intPositionId = up.intPositionId Inner join tblUnitDeployData as ud on " _
            & "ud.intUnitMobId = p.intUnitMobId where t.dtCompleted IS NULL AND (up.bitrefrad IS NULL AND up.bitRearDet IS NULL) AND " & SqlWhere & " Order by strUic, strTaskName, strName, dtFound"
       
        myDataTable = New DataTable
        myDataTable = getData(sql)

        RecallPage()

        myDataGrid.DataSource = myDataTable
        myDataGrid.DataBind()

        For theCounter = 0 To myDataGrid.Items.Count - 1

        Next

        lblCount.Text = "<b>There are </b> " & theCounter & "&nbsp;&nbsp;<b>issues</b> "

    End Sub

Open in new window

Rufus WilliamSenior Software Architect

Commented:
as someone suggested... smartnavigation should work... in case that fails..
have a hidden HTML control... and when the refresh button is pressed.. assign the value of the scrollTop of the div to the hidden HTML control.... when the page is loaded again... pick the value from the hidden HTML control and assign it back to the scrollTop..

Note: for this to work, u have to postback the page.. not refresh the page.

Author

Commented:
administratortools_net,
Ok tired the code provided, I changed everywhere you had forms to frm, because my form Id is frm, but I still got errors with each version on
Line 37
Character 31
 expected ';'

I treid a colon in different spots but then got errors agian.
<script language = "javascript"> 
			
			function sstchur_SmartScroller_GetCoords() 
			{ 
				var scrollX, scrollY; 
			       
				if (document.all) 
				{ 
					if (!document.documentElement.scrollLeft) 
						scrollX = document.body.scrollLeft; 
					else 
						scrollX = document.documentElement.scrollLeft; 
			                
					if (!document.documentElement.scrollTop) 
						scrollY = document.body.scrollTop; 
					else 
						scrollY = document.documentElement.scrollTop; 
				}    
				else 
				{ 
					scrollX = window.pageXOffset; 
					scrollY = window.pageYOffset; 
				} 
			    
				document.frm[formID].xCoordHolder.value = scrollX; 
				document.frm[formID].yCoordHolder.value = scrollY; 
			} 
			    
			function sstchur_SmartScroller_Scroll() 
			{ 
				var x = document.frm[formID].xCoordHolder.value; 
				var y = document.frmformID].yCoordHolder.value; 
				window.scrollTo(x, y); 
			} 
			    
			window.onload = sstchur_SmartScroller_Scroll; 
			window.onscroll = sstchur_SmartScroller_GetCoords; 
			window.onkeypress = sstchur_SmartScroller_GetCoords; 
			window.onclick = sstchur_SmartScroller_GetCoords; 

			</script> 

Open in new window

Author

Commented:
administratortools_net,
Ok tired the code provided, I changed everywhere you had forms to frm, because my form Id is frm, but I still got errors with each version on
Line 37
Character 31
 expected ';'

I treid a colon in different spots but then got errors agian.
<script language = "javascript"> 
			
			function sstchur_SmartScroller_GetCoords() 
			{ 
				var scrollX, scrollY; 
			       
				if (document.all) 
				{ 
					if (!document.documentElement.scrollLeft) 
						scrollX = document.body.scrollLeft; 
					else 
						scrollX = document.documentElement.scrollLeft; 
			                
					if (!document.documentElement.scrollTop) 
						scrollY = document.body.scrollTop; 
					else 
						scrollY = document.documentElement.scrollTop; 
				}    
				else 
				{ 
					scrollX = window.pageXOffset; 
					scrollY = window.pageYOffset; 
				} 
			    
				document.frm[formID].xCoordHolder.value = scrollX; 
				document.frm[formID].yCoordHolder.value = scrollY; 
			} 
			    
			function sstchur_SmartScroller_Scroll() 
			{ 
				var x = document.frm[formID].xCoordHolder.value; 
				var y = document.frmformID].yCoordHolder.value; 
				window.scrollTo(x, y); 
			} 
			    
			window.onload = sstchur_SmartScroller_Scroll; 
			window.onscroll = sstchur_SmartScroller_GetCoords; 
			window.onkeypress = sstchur_SmartScroller_GetCoords; 
			window.onclick = sstchur_SmartScroller_GetCoords; 

			</script> 

Open in new window

Author

Commented:
administratortools_net,
Ok I solved it there was a missing bracket that I found compared to first row, but is there some javascipt command I need to put somewhere, because when it reposts it still goes to top of the page.

Author

Commented:
administratortools_net,
ok
I got a FormId error, so I changed these to match frm which is what my form Id is.  but now I get this error on line 30 character 5 documents.forms[...].xcordHolder is nul or not an object

document.forms[frm].xCoordHolder.value = scrollX;
                        document.forms[frm].yCoordHolder.value = scrollY;
                  }
                      
                  function sstchur_SmartScroller_Scroll()
                  {
                        var x = document.forms[frm].xCoordHolder.value;
                        var y = document.forms[frm].yCoordHolder.value;
                        window.scrollTo(x, y);
Try defining xCoordHolder and yCoordHolder as hidden form elements in the form that is posted. This means that when the form is posted back to the page, it will be picked up by the JavaScript.

Just put this code somewhere in between your form tags; it shouldn't show up on the page at all.
<input type="hidden" id="xCoordHolder" value="">
<input type="hidden" id="yCoordHolder" value="">

Open in new window

Kyle AbrahamsSenior .Net Developer

Commented:
kdeutsch:

Remove this line from myRecall sub:
myDataGrid.DataBind()

You only need to call one databind.  Ensure you are calling the fillgrid page load.  Can you confirm via setting break points the the grid's page is actually being set?
Commented:
All,
It it now working.  the follwoing is waht I did to further administratortools_net: javascript example.
I modified to use the scroll bar instead, it worked better than corrdiantes on a datagrid.  then I went into the code behind and got the position of the scrollbar on link click and then pass it back on my submit click to the javascript.

 Dim yCoord As String = Request.Form("yCoordHolder")

            If yCoord = "" Then
                yCoord = "0"
            End If

yCoordHolder.Value = yCoord

<script language="javascript">
                  
                  function sstchur_SmartScroller_GetCoords()
                  {
                        document.frm.yCoordHolder.value = document.body.scrollTop;
                  }
                      
                  function sstchur_SmartScroller_Scroll()
                  {
                        var x = 0;
                        var y = document.frm.yCoordHolder.value;
                        window.scrollTo(x, y);
                  }
                      
                  window.onload = sstchur_SmartScroller_Scroll;
                  window.onscroll = sstchur_SmartScroller_GetCoords;
                  window.onkeypress = sstchur_SmartScroller_GetCoords;
                  window.onclick = sstchur_SmartScroller_GetCoords;

            </script>



Author

Commented:
If there are no objections I am going to pick mine as the solution and then pass out all the points for help by 1pm today.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial