Use JavaScript to refresh ASP. NET Server controls

Source: Internet
Author: User

Developers using ASP. NET have a headache in page refresh caused by server control events!

I shared the solution to the problem with you. The idea 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 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 classCode:

1 ///   <Summary>  
2 /// Provides some methods to generate page scripts
3 ///   </Summary>  
4 Public   Class Scripthelper
5 {
6
7 ///   <Summary>  
8 /// Obtain the client-side control script.
9 ///   </Summary>  
10 ///   <Param name = "controlname"> </param>  
11 ///   <Returns> Script code </Returns>  
12 Public   Static   String Getviewcontrolscript ( String Controlname)
13 {
14
15 // Create client function viewobj
16 String Script =   " \ N " ;
17 Script + =   " <Script language = \ " Javascript \ " > \ N " ;
18 Script + =   " Function viewobj (objname) \ n " ;
19 Script + =   " {\ N " ;
20 Script + =   " VaR OBJ = Document. All. Item (objname); \ n " ;
21 Script + =   " If (OBJ! = NULL) \ n " ;
22 Script + =   " {\ N " ;
23 Script + =   " \ Tobj. scrollintoview (); \ n " ;
24 Script + =   " \ Tobj. Focus (); \ n " ;
25 Script + =   " } \ N " ;
26 Script + =   " } \ N " ;
27
28 // Create client function todo
29 Script + =   " Function todo () " ;
30 Script + =   " {\ N " ;
31 Script + =   String . Format ( " SetTimeout (\ " Viewobj ( ' {0} ' )\ " , 1000); \ n " , Controlname );
32 Script + =   " } \ N " ;
33
34 Script + =   " Window. onload = todo; \ n " ;
35 Script + =   " </SCRIPT> \ n " ;
36
37 Return Script;
38 }
39
40 }
41
42

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:

1 Private   Void Btnsave_click ( Object Sender, system. eventargs E)
2 {
3 Project. updateprojectinfo (DS );
4 Lblscript. Text = Scripthelper. getviewcontrolscript ( " Btnsave " );
5 }

After clicking the btnsave button, the page will automatically scroll to the btnsave location, reducing the inconvenience caused by PAGE refresh.

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.