asp.net| Script | solve | refresh | problem | Page refresh with asp.net developer for server-side control events It's a headache!
Not here to share the solution of not doing a project.
Not the idea is to write the 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.