Developers using ASP. NET have a headache in page refresh caused by server control events! Not here, we will share the solution of not in a project. The idea of not is to write a segment script after the event is executed, so that the page will automatically scroll to the control place before the page is refreshed, reducing the inconvenience caused by PAGE refreshing. For example, if you press a button, the script automatically scrolls the page to the button position. A scripthelper class is not used to write a segment script. This class has a getviewcontrolscript (string controlname) method, which returns a client script, the input parameter is the ID of the control. Scripthelper class code: /// <Summary> /// Provides some methods to generate page scripts /// </Summary> Public class scripthelper { /// <Summary> /// Obtain the script for the client to view the control /// </Summary> /// <Param name = "controlname"> </param> /// <Returns> script code </returns> Public static string getviewcontrolscript (string controlname) { // Create the 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 the 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; } } Example: To facilitate script input, I put a label: lblscript on the page, and set the enableviewstate attribute and visible attribute of lblscript to false. Then add the script input code after the operation code of the click event of lblscrpt, as shown below: Private void btnsave_click (Object sender, system. eventargs E) { Project. updateprojectinfo (DS ); Lblscript. Text = scripthelper. getviewcontrolscript ("btnsave "); } After clicking the btnsave button, the page will automatically scroll to the btnsave location, reducing the inconvenience caused by PAGE refresh. |