Asp.net c # support scroll bar memory in updatepanel

Source: Internet
Author: User

On a non-ajax page, you only need to set MaintainScrollPositionOnPostback = "true" on the page to remember the position of the scroll bar.

However, in ajax, this function does not work correctly. How can we make the div automatically maintain the position of the scroll bar?

 


First, add the asp: panel control on the page, and then add the following method in the aspx. cs Background:

 


[C #]
Protected void DoMaintainScrollPositionForAjax (UpdatePanel updatePanel, Panel panel)
{
StringBuilder sb = new StringBuilder ();
Sb. AppendLine (@ "function Panel_SaveScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID + '_ scrollposx'). value = document. getElementById (PanelID). scrollLeft;
Document. getElementById (PanelID + '_ ScrollPosY'). value = document. getElementById (PanelID). scrollTop ;}}
Function Panel_RestoreScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID). scrollLeft = document. getElementById (PanelID + '_ scrollposx'). value;
Document. getElementById (PanelID). scrollTop = document. getElementById (PanelID + '_ ScrollPosY'). value ;}}");
ScriptManager. RegisterClientScriptBlock (updatePanel, panel. GetType (), "PanelScrollFunction", sb. ToString (), true );
 
HiddenField x = new HiddenField ();
X. ID = panel. ClientID + "_ ScrollPosX ";
X. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (x );
 
HiddenField y = new HiddenField ();
Y. ID = panel. ClientID + "_ ScrollPosY ";
Y. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (y );
 
String sScript = "Panel_SaveScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterOnSubmitStatement (updatePanel, panel. GetType (), panel. ID + "_ SavePanelScroll", sScript );
 
If (Page. IsPostBack)
{
X. Value = Page. Request. Form [x. ClientID];
Y. Value = Page. Request. Form [y. ClientID];
SScript = "Panel_RestoreScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterStartupScript (updatePanel, panel. GetType (), panel. ID + "_ SetPanelScroll", sScript, true );
}
}
Protected void DoMaintainScrollPositionForAjax (UpdatePanel updatePanel, Panel panel)
{
StringBuilder sb = new StringBuilder ();
Sb. AppendLine (@ "function Panel_SaveScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID + '_ scrollposx'). value = document. getElementById (PanelID). scrollLeft;
Document. getElementById (PanelID + '_ ScrollPosY'). value = document. getElementById (PanelID). scrollTop ;}}
Function Panel_RestoreScrollPosition (PanelID ){
If (document. getElementById (PanelID )! = Null ){
Document. getElementById (PanelID). scrollLeft = document. getElementById (PanelID + '_ scrollposx'). value;
Document. getElementById (PanelID). scrollTop = document. getElementById (PanelID + '_ ScrollPosY'). value ;}}");
ScriptManager. RegisterClientScriptBlock (updatePanel, panel. GetType (), "PanelScrollFunction", sb. ToString (), true );

HiddenField x = new HiddenField ();
X. ID = panel. ClientID + "_ ScrollPosX ";
X. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (x );

HiddenField y = new HiddenField ();
Y. ID = panel. ClientID + "_ ScrollPosY ";
Y. ClientIDMode = panel. ClientIDMode;
Panel. Controls. Add (y );

String sScript = "Panel_SaveScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterOnSubmitStatement (updatePanel, panel. GetType (), panel. ID + "_ SavePanelScroll", sScript );

If (Page. IsPostBack)
{
X. Value = Page. Request. Form [x. ClientID];
Y. Value = Page. Request. Form [y. ClientID];
SScript = "Panel_RestoreScrollPosition ('" + panel. ClientID + "');";
ScriptManager. RegisterStartupScript (updatePanel, panel. GetType (), panel. ID + "_ SetPanelScroll", sScript, true );
}
}

 

 

Call aspx. cs in the following method:


[C #]
Protected void Page_Load (object sender, EventArgs e)
{
DoMaintainScrollPositionForAjax (this. UpdatePanel1, Panel1 );
}
Protected void Page_Load (object sender, EventArgs e)
{
DoMaintainScrollPositionForAjax (this. UpdatePanel1, Panel1 );
}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.