We help IT Professionals succeed at work.

Asp.net:  Retain scroll position after datagrid update

jay-are
jay-are asked
on
1,052 Views
Last Modified: 2012-08-14
Hello Experts!

I'm trying to retain my scroll position after I update, or cancel an edit line in a datagrid.  It works fine on edit but once I update it goes back to the top of the page.  This same code has worked fine before but I can't seem to pick out the problem here!  Here is the code I use:

Sub DataGrid1_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        'keep scroll position
        If (bookMark) Then
            bookmarkIndex = e.Item.ItemIndex
            Me.InsertScriptBlock(e.Item.ClientID)
        End If
    End Sub

Sub DataGrid1_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        'keep scroll position
        If (bookMark) Then
            Dim anchor As New LiteralControl
            anchor.Text = "<a name=""#" + e.Item.UniqueID + """>"
            e.Item.Cells(6).Controls.Add(anchor)
        End If
    End Sub

    Sub InsertScriptBlock(ByVal strItem As String)
        'script that holds scroll position after post back
        Dim jScript As New System.Text.StringBuilder
        jScript.Append("<script language=""JavaScript"">")
        jScript.Append("location.href=""#")
        jScript.Append("strItem")
        jScript.Append(""";")
        jScript.Append("<" & "/script>")
        Me.RegisterStartupScript("bookMark", jScript.ToString())
    End Sub

Any reason this won't work after update/cancel?
Help!
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Or you can find coordinates of the place where you want to scrool and use java script

window.scroll(iX, iY)
Parameters

iX Required. Integer that specifies the horizontal scroll offset, in pixels.
iY Required. Integer that specifies the vertical scroll offset, in pixels.  

 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sExpression As String = "window.open('http://www.google.com')"
        Me.RegisterStartupScript("TestJS", _
  "<script language='javascript'>window.scroll(0,100);</script>")
    End Sub
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
farsight:  I can't answer your question about the & symbol in javascript.  This code was provided for me from another EE user who helped me about 2 months ago.
I visited your examples and will test it out.

I was really hoping to keep the code I'm using as its worked fine before.

iboutchkine:  I've never had to use a <a> anchor before.  I was thinking that I just made some blatant error somewhere.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
nishikanth:  My datagrid is inside a framed page already.  Not interested in having it scroll, just doesn't look right inside a framed page.

I'm trying to make my existing code work, not change to another method.  I'm thinking its something with the java but I'm not sure.  Need to find my old code where I first used this...

Any ideas?
David H.H.LeeDigital Marketing (Hospitality)
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I finally found some older code where I used this scrollback method and found the problem.

jScript.Append("strItem")
should be
jScript.Append(strItem)

also this goof:
 Sub DataGrid1_PageCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs)
        'show a new row of data
        ' If Not isEditing Then
        myComponent.FillDataSet(DataSet11)
        DataGrid1.EditItemIndex = -1                             <--------added this line
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        DataGrid1.DataBind()
        'End If
    End Sub

So those two changes and it works perfectly.  Thanks for the suggestions everyone.  
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.