Maintain scroll position in ASP.Net

G

GrantS

I am trying to convert the VB.Net code example povided by
http://authors.aspalliance.com/JimRoss/Articles/MaintainScrollPos.aspx
into C# (ASP.Net)without success. No errors are thrown in the VB code
provided on the website.
Once I have this example running correctly, I will need to use the
concept in a more complex project. The code I am using involves an
webform and an htc file.

The code for ScrollPos.htc (which is located in a folder within the
project called 'Includes'):

-----------------------------------------------------------------
<PUBLIC:ATTACH EVENT=ondocumentready ONEVENT="elementLoad()" />
<PUBLIC:pROPERTY NAME="scrollPos" />
<PUBLIC:pROPERTY NAME="persistID" />

// DHTML behavior for scrollable DIV
// (or other scrollable element)
//
// allows element to maintain scroll position within
// the DIV across postbacks.

<script language="javascript">
function elementLoad() {
element.scrollTop = scrollPos;
element.attachEvent("onscroll", saveScroll);
}

function saveScroll() {
element.document.all[persistID].value =
event.srcElement.scrollTop;
}
</script>

---------------------------------

The HTML code for the ASP file is:

<%@ Page language="c#" Codebehind="MaintainScrollDemo.aspx.cs"
AutoEventWireup="false" Inherits="TestC.MaintainScrollDemo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>MaintainScrollDemo</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<LINK href="../Styles.css" type="text/css" rel="stylesheet">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<P align="center"><A href="MaintainScrollPos.aspx">&lt;&nbsp; Back
to article</A>
</P>
<P align="left">Here is a demonstration of retaining scroll
position in a
&lt;DIV&gt; element that is being used to add scroll bars to a
DataGrid.&nbsp;
Try scrolling&nbsp;both grids a few rows, then make the form post
back by
clicking any of the Select buttons in either grid.
</P>
<H1 align="center">This Grid Won't Retain Scroll Position</H1>
<blockquote style="TEXT-ALIGN: center">
<DIV style="OVERFLOW: auto; WIDTH: 400px; HEIGHT:
120px"><asp:datagrid id="dgOne" width="100%" BorderWidth="1px"
GridLines="Vertical" CellPadding="4" BackColor="White"
ForeColor="Black" BorderStyle="None" BorderColor="#DEDFDE"
Runat="server">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F7F7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#6B696B"></HeaderStyle>
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="#F7F7DE" Mode="NumericPages"></PagerStyle>
</asp:datagrid></DIV>
</blockquote>
<H1 align="center">This Grid Will Retain Scroll Position</H1>
<blockquote style="TEXT-ALIGN: center">
<% string scrollPosURL = "../Includes/ScrollPos.htc"; %>
<DIV persistID="<%= saveScrollPos.UniqueID %>"
scrollPOS="<%= saveScrollPos.value %>" style ="BEHAVIOR:
url(<%= ResolveURL(scrollPosURL);%>);WIDTH: 400px;HEIGHT: 120px"
persistID="<%= saveScrollPos.UniqueID %>" scrollPOS="<%=
saveScrollPos.value %>">
<INPUT id="saveScrollPos" type="hidden">&nbsp;
<asp:datagrid id="dgTwo" width="100%" BorderWidth="1px"
GridLines="Vertical" CellPadding="4" BackColor="White"
ForeColor="Black" BorderStyle="None" BorderColor="#DEDFDE"
Runat="server">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CE5D5A"></SelectedItemStyle>
<AlternatingItemStyle BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#F7F7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#6B696B"></HeaderStyle>
<FooterStyle BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:ButtonColumn Text="Select" ButtonType="PushButton"
CommandName="Select"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="#F7F7DE" Mode="NumericPages"></PagerStyle>
</asp:datagrid></DIV>
</blockquote>
</form>
</body>
</HTML>


-------------------------------------
The code behind for this page is:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace TestC
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class MaintainScrollDemo : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgOne;
protected System.Web.UI.WebControls.DataGrid dgTwo;

private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.BindLists();
}
}

private void BindLists()
{

//ArrayList[] winelist = new ArrayList[11];
//string [] winelist ;

string [] winelist = new string[12]{"Chateau Lafite Rothschild,
1959",
"Chateau Mouton Rothschild, 1945",
"Maddog 'da Ripper, yesterday",
"Penfolds Grange, 1981",
"Patricia Green Cellars, Estate Pinot Noir, 2000",
"Owen Roe Pinot Gris, 2001",
"d'Arenberg Dead Arm Shiraz, 1978",
"Adelsheim Chardonnay, Elizabeth's Reserve, 1989",
"Screaming Eagle, 1998",
"Far Niente Chardonnay, 2002, 'why pay less?'",
"Vieux Telegraphe CnDP, 1989",
"Whennler Sonnenur Auselese, J.J.Prum, 1978"};

this.dgOne.DataSource = winelist;
this.dgOne.DataBind();
this.dgTwo.DataSource = winelist;
this.dgTwo.DataBind();
}


#region Web Form Designer generated code


override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}
----------------------------------------------

Hopefully someone is able to point a frustrated C# ASP.Net newbie in
the right direction.

Thanks in anticipation

Grant
 
G

Grant Sutcliffe

Hi Jon


Thanks for your reply.
The compiler generated message displayed when I try to run the app is as
below:

----------------------------------------

Compiler Error Message: CS1026: ) expected

Source Error:


Line 37: <blockquote style="TEXT-ALIGN: center">
Line 38: <% string scrollPosURL = "../Includes/ScrollPos.htc"; %>
Line 39: <DIV persistID="<% saveScrollPos.UniqueID; %>"
Line 40: <input id="saveScrollPos" type="hidden"
name="saveScrollPos" runat="server">
Line 41: <asp:datagrid id="dgTwo" width="100%" BorderWidth="1px"
GridLines="Vertical" CellPadding="4" BackColor="White"

--------------------------------

I have played around with the HTML syntax as best as I can but the
'hint' - expected ')' has not helped me so far. Drats!

I have also set Alerts in the htc file (code as per Google posting). So
I know when that file is hit.

Possibly there is something in required in the C# code behind that is
not required in the VB.Net example.

Thanks for your time.

Cheers

Grant
 

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

Top