Using JavaScript to troubleshoot the refresh problem caused by ASP.net server controls

Source: Internet
Author: User
Asp.net| Server | resolution | control | refresh | Problems with ASP.net developers the page refresh caused by server-side control events It's a headache!

I put the solution to the problem to share with you, the idea is to write a script after the event execution, so that the page automatically scroll to refresh the page before the control, reduce the inconvenience caused by page refreshes.
For example, when a button is pressed, the script automatically scrolls the page to the position of the button.

Writing a paragraph script is not done with a Scripthelper class, which has a getviewcontrolscript (string controlname) method that returns a client-side script, and the incoming argument is the ID of the control.

Scripthelper Class Code:

<summary>
Provides some ways to generate page scripts
</summary>
public class Scripthelper
{

<summary>
Get the script for the Client view control
</summary>
<param name= "ControlName" ></param>
<returns> Scripting code </returns>
public static string Getviewcontrolscript (String controlname)
{

Create a client function viewobj
string script = "\ n";
Script = = "<script language=\" javascript\ ">\n";
Script = = "function viewobj (objname) \ n";
Script + = "{\ n";
Script + = "var obj = Document.All.Item (objname); \ n";
Script = = "if (obj!= null) \ n";
Script + = "{\ n";
Script + = "\tobj.scrollintoview (); \ n";
Script + = "\tobj.focus (); \ n";
Script = = "}\n";
Script = = "}\n";

Create a client function todo
Script + = "function ToDo ()";
Script + = "{\ n";
Script = = string. Format ("settimeout (\" Viewobj (' {0} ') \ ", 1000); \ n", controlname);
Script = = "}\n";

Script + = "window.onload = todo;\n";
Script = = "</script>\n";

return script;
}

}

Use examples:

To facilitate the input script, I put a label:lblscript on the page and set the Lblscript EnableViewState property and Visible property to False.
Then add the code for the input script after the action code for the Lblscrpt click event, as follows:

private void Btnsave_click (object sender, System.EventArgs e)
{
Project. Updateprojectinfo (DS);
Lblscript.text = Scripthelper.getviewcontrolscript ("Btnsave");
}

After clicking on the Btnsave button, the page will automatically scroll to the btnsave position, reducing the inconvenience caused by the page refresh.
Powered by Dvnews.net


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.