how to remember asp:listbox's scrollbar position

D

Daniel

hi,
I had an asp:listbox, and everytime i click item inside, the bar
automatically go to the top, is there any way to keep the scroll position?
I turn on the smartNavigation, it still doesn't work.

Thanks ahead.
 
S

Steven Cheng[MSFT]

Hi Daniel,

From your description, you're using an ASP.NET ListBox in your web page
and set it as AutoPostBack, and since the page is contains large content,
when you select in the ListBox, you need to scroll the page down. However,
you find when the listbox's index changed and post back, the page will
reset to to top rather than keep the origianl scroll position, yes?

I'm not sure whether there're anyother particular elements or script code
in your page, but based on my local test, it seems that the
"SmartNavigation" can do this task, I just turn on the smartnavigation and
when the listbox autopostback, the page will remain the last scroll
poistion. Would you please have a further test?

In addition, there is another means I think can keep the scroll poistion,
since it is the ListBox which cause the post back, we can register a
certain client script which will set the focus back to the ListBox after
the page return back to the client side again after the post back. Here is
a demo page I've made, you can have a view if you have anything unclear:

==============aspx page====================
<HTML>
<HEAD>
<title>ListBox</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">
<script language="javascript">
function setFocus(id)
{
var elm = document.getElementById(id);
elm.focus();
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>ddddddddddddddddddddddddddddddddddddddddddddddddddddd</td>
</tr>
<tr>
<td>
<asp:ListBox id="ListBox1" runat="server" Height="142px"
Width="136px" AutoPostBack="True">
<asp:ListItem Value="aaa">aaa</asp:ListItem>
<asp:ListItem Value="bbb">bbb</asp:ListItem>
<asp:ListItem Value="ccc">ccc</asp:ListItem>
<asp:ListItem Value="ddd">ddd</asp:ListItem>
<asp:ListItem Value="eee">eee</asp:ListItem>
<asp:ListItem Value="fff">fff</asp:ListItem>
<asp:ListItem Value="ggg">ggg</asp:ListItem>
<asp:ListItem Value="hhh">hhh</asp:ListItem>
<asp:ListItem Value="iii">iii</asp:ListItem>
</asp:ListBox></td>
</tr>
</table>
</form>
</body>
</HTML>

===========code behind page class===========
namespace FormAuthApp.controls
{
/// <summary>
/// Summary description for ListBox.
/// </summary>
public class ListBox : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ListBox ListBox1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#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.ListBox1.SelectedIndexChanged += new
System.EventHandler(this.ListBox1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ListBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{
string script = "<script
language='javascript'>setFocus('{0}');</script>";
script = string.Format(script,ListBox1.ClientID);

Page.RegisterStartupScript("script",script);

Response.Write("<br>SelectedIndex: " + ListBox1.SelectedIndex);
}
}
=======================================

Also, if you have any other ideas, please also feel free to post here.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top