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()" />
<PUBLICROPERTY NAME="scrollPos" />
<PUBLICROPERTY 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">< Back
to article</A>
</P>
<P align="left">Here 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: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">
<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
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()" />
<PUBLICROPERTY NAME="scrollPos" />
<PUBLICROPERTY 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">< Back
to article</A>
</P>
<P align="left">Here 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: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">
<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