This section describes a helper class for ASP. NET to register JavaScript scripts with webpage.
In ASP. in the development of net, web layer development needs to interact with users, such as displaying a warning dialog box, displaying a confirmation dialog box before deletion, opening a new window, closing the current window, and so on.
These functions are generally implemented using client scripts, especially JavaScript.Functions in JavascriptCodeIt can be divided into the following types:
1. Confirm;
2. Warning;
3. Close the window;
4. Navigation;
5. Open the mode window, non-mode window, custom dialog box, and common window;
6. Popup;
7. Verification input (that is, the ASP. NET verification control cannot be implemented. For example, to verify that two text boxes must be entered and only one is entered );
8. maskedit (mask input );
...
ASP. NET manages, registers, and adds client scripts through the following methods:
1. registerclientscriptblock
The client code block can be sent to the page (of course, it can be not just a script or other text), and this code will be written to the start part of form.
2. isclientscriptblockregistered
Determine whether the client code sent by registerclientscriptblock has been sent to the client based on a page-level key.
3. registerstartupscript
You can send customers to the page. End code block, which is written to the end part of form. In this way, you can write a piece of automatically Running code after the page is loaded, just like the name.
4. isstartupscriptregistered
Determine whether the client code sent by registerstartupscript has been sent to the client based on a page-level key.
5. registerarraydeclaration (
Register an array with the client through the server.
For example, the server C # code is as follows: registerarraydeclaration ("myarray", "'X', 'y', 'z '");
When it is sent to the client, it becomes:
<Script language = "JavaScript">
<! --
VaR myarray = new array ('x', 'y', 'z ');
// -->
</SCRIPT>
6. registeronsubmitstatement
Send a statement to the client, which is triggered and executed before the form is submitted.
Generally, to display a warning dialog box on the client, we will use the following method:
Response. Write ("<script language = \" javascript \ "> alert ('display an alert dialog. '); </SCRIPT> ");
Or
This. registerclientscriptblock ('alert1', "alert ('display an alert diert .');");
And after using this class ( Source code: Javascripthelper)To implement the same function, you only need to call one sentence:Javascript. Alert ("display an alert dialog .");You can.
In such short JavaScript statements, the convenience of such statements may not be reflected, but imagine we want to implement the function of opening a new window.
Anyone familiar with JavaScript knows that opening a new window is generally implemented through the window. open () method, which has many parameters. Not everyone can remember and write this method accurately and quickly. At this time, this role will be reflected.
In this class, I implemented a method named OpenWindow. The prototype is :
Reload 1:
Public static bool OpenWindow (string URL, string windowname, int height, int width,
Int left, int top, bool iswithtoolbar, bool iswithmenubar, bool resizable, bool iswithlocation,
Bool iswithstatusbar, bool iswithscrollbars, bool isfront, bool isfullscreen, int autocloseinseconds)
Heavy Load 2:
Public static bool OpenWindow (string URL, string windowname, int height, int width)
To enable the function of opening a new window, you only need to call this method. You do not need to combine the script every time and then write or register it to the client..