I am trying to convert the VB.Net code example povided by
http://authors.aspalliance.com/JimRo...ScrollPos.aspx
into C# (ASP.Net)withou t 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:ATTAC H EVENT=ondocumen tready ONEVENT="elemen tLoad()" />
<PUBLIC:PROPERT Y NAME="scrollPos " />
<PUBLIC:PROPERT Y NAME="persistID " />
// DHTML behavior for scrollable DIV
// (or other scrollable element)
//
// allows element to maintain scroll position within
// the DIV across postbacks.
<script language="javas cript">
function elementLoad() {
element.scrollT op = scrollPos;
element.attachE vent("onscroll" , saveScroll);
}
function saveScroll() {
element.documen t.all[persistID].value =
event.srcElemen t.scrollTop;
}
</script>
---------------------------------
The HTML code for the ASP file is:
<%@ Page language="c#" Codebehind="Mai ntainScrollDemo .aspx.cs"
AutoEventWireup ="false" Inherits="TestC .MaintainScroll Demo" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Maintain ScrollDemo</title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="C#" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
<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="MaintainS crollPos.aspx"> < Back
to article</A>
</P>
<P align="left">He re is a demonstration of retaining scroll
position in a
<DIV> element that is being used to add scroll bars to a
DataGrid.
Try scrolling 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:dat agrid id="dgOne" width="100%" BorderWidth="1p x"
GridLines="Vert ical" CellPadding="4" BackColor="Whit e"
ForeColor="Blac k" BorderStyle="No ne" BorderColor="#D EDFDE"
Runat="server">
<SelectedItemSt yle Font-Bold="True" ForeColor="Whit e"
BackColor="#CE5 D5A"></SelectedItemSty le>
<AlternatingIte mStyle BackColor="Whit e"></AlternatingItem Style>
<ItemStyle BackColor="#F7F 7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="Whit e"
BackColor="#6B6 96B"></HeaderStyle>
<FooterStyle BackColor="#CCC C99"></FooterStyle>
<Columns>
<asp:ButtonColu mn Text="Select" ButtonType="Pus hButton"
CommandName="Se lect"></asp:ButtonColum n>
</Columns>
<PagerStyle HorizontalAlign ="Right" ForeColor="Blac k"
BackColor="#F7F 7DE" Mode="NumericPa ges"></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.U niqueID %>"
scrollPOS="<%= saveScrollPos.v alue %>" style ="BEHAVIOR:
url(<%= ResolveURL(scro llPosURL);%>);W IDTH: 400px;HEIGHT: 120px"
persistID="<%= saveScrollPos.U niqueID %>" scrollPOS="<%=
saveScrollPos.v alue %>">
<INPUT id="saveScrollP os" type="hidden">& nbsp;
<asp:datagrid id="dgTwo" width="100%" BorderWidth="1p x"
GridLines="Vert ical" CellPadding="4" BackColor="Whit e"
ForeColor="Blac k" BorderStyle="No ne" BorderColor="#D EDFDE"
Runat="server">
<SelectedItemSt yle Font-Bold="True" ForeColor="Whit e"
BackColor="#CE5 D5A"></SelectedItemSty le>
<AlternatingIte mStyle BackColor="Whit e"></AlternatingItem Style>
<ItemStyle BackColor="#F7F 7DE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="Whit e"
BackColor="#6B6 96B"></HeaderStyle>
<FooterStyle BackColor="#CCC C99"></FooterStyle>
<Columns>
<asp:ButtonColu mn Text="Select" ButtonType="Pus hButton"
CommandName="Se lect"></asp:ButtonColum n>
</Columns>
<PagerStyle HorizontalAlign ="Right" ForeColor="Blac k"
BackColor="#F7F 7DE" Mode="NumericPa ges"></PagerStyle>
</asp:datagrid></DIV>
</blockquote>
</form>
</body>
</HTML>
-------------------------------------
The code behind for this page is:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
namespace TestC
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class MaintainScrollD emo : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data Grid dgOne;
protected System.Web.UI.W ebControls.Data Grid dgTwo;
private void Page_Load(objec t sender, System.EventArg s 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.Data Source = winelist;
this.dgOne.Data Bind();
this.dgTwo.Data Source = winelist;
this.dgTwo.Data Bind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}
----------------------------------------------
Hopefully someone is able to point a frustrated C# ASP.Net newbie in
the right direction.
Thanks in anticipation
Grant