Place Javascript in the code
Place Javascript in a separate file (. JS)
Register some page update compatible scripts with the updatepanel Control
Execute functions in C # code in JavaScript Functions
Place Javascript inCodeMedium
Since we can embed some JavaScript inside the HTML element, or even dynamically use JavaScript and web server controls, how can we place all JavaScript Functions in your code?
Before ASP. NET 2.0, you must useRegisterstartupscriptAndRegisterclientscriptblockMethod. Now, these two methods have been eliminated. Two possible methods for registering scripts in ASP. NET 1.x require a set of keywords/script parameters. Because two independent methods are involved, it is very likely that some keyword names conflict.Page. clientscriptThe property itself can complete all script registration, making your code less error.
Place Javascript in a separate. js file and useRegisterstartupscriptAndRegisterclientscriptblockMethod To embed Javascript into the page. UseHtmlgenericcontrolIt is quite easy to deploy the specific JavaScript of the control to the ASP. NET page.
Page. clientscript. registerstartupscript () method
A script element is displayed after the <form> start tag on the page.
Registerstartupscript (type, key, script)
Registerstartupscript (type, key, script, script tag Specification)
In the preceding example, you specify the typeMe. GetType ()Keyword and the script, and then a value isTrueBoolean value (for. Net to automatically use<SCRIPT>Mark the script to be embedded in the ASP. NET page ). It is important to specify a unique keyword for all JavaScript on the page at 01:10 (this can be done byKeyParameters ). If multiple JavaScript have the same keyword name, only the first Javascript is embedded in the page.
Page. clientscript. registerclientscriptblock () method
The START script block is displayed before the </form> end tag on the page.
Back to Top
Place Javascript in a separate file (. JS)
We strongly recommend that you place JavaScript Functions in a separate file (. js file ). Once they are in a separate file and are part of a project, you can use some methods you have introduced to import the file to the page. For example, you can use the following code to include a. js file to an ASP. NET page:
Page. clientscript. registerclientscriptblock (this. GetType (), "myscript", "myjavascriptfile. js ");
Once the. js file is imported to the ASP. NET page, you can call any JavaScript function as before. This is a good method to manage JavaScript Functions and logically separate them from other ASP. NET pages. You can also use this method to easily use the same JavaScript function in multiple ASP. NET pages.
Back to Top
Register some page update compatible scripts with the updatepanel Control
To facilitate the use of the updatepanel control and render the script (to ensure that JavaScript is not invalid in updatepanel), make sure to call the scriptmanager control method.
- RegisterclientscriptblockA script element is displayed after the <form> start tag on the page.
- RegisterstartupscriptThe START script block is displayed before the </form> end tag on the page.
It has one more parameter than the script registration method on the page, that is, the control for which the script is registered. If the control is in the updatepanel of this update, the script takes effect. If not, the script is invalid.
Back to Top
Execute functions in C # code in JavaScript Functions
Method 1: 1. Create a button and write the called or processed content to button_click in the background; 2. Write a JS function on the foreground with the content of document. getelementbyid ("btn1 "). click (); 3. Call JS functions on the front or back end to stimulate the click event, which is equivalent to accessing the C # function in the background;
Method 2: 1. function declaration as public background code (change public to protected) Public String SS () {return ("");} 2. You can use <% = fucntion () %> in HTML to call the foreground script <script language = JavaScript> var A = "<% = SS () %> "; alert (a); </SCRIPT>
method 3: 1.
Method 4: <script language = "JavaScript"> function submitkeyclick () {If (event. keycode = 13) {event. cancelbubble = true; event. returnvalue = false; document. all. funname. value = "Name of the function you want to call"; document. form [0]. submit () ;}</SCRIPT> <input onkeypress = "submitkeyclick () "id =" AAA "type =" text "> <input type =" hidden "name =" funname "> <〈! -- Used to store the function you want to call --> in. CS, there are: public page_onload () {If (! Page. ispost ()){
String strfunname = request. Form ["funname"]! = NULL? Request. form ["funname"]: ""; // you can call the switch (strfunname) {Case "Enter ()": enter () function based on the value returned (); // call this function break; Case "other": // call other functions break; default: // call the default function break ;}} public void enter () {//...... For example, calculating a value}
Reference:
Using JavaScript with ASP. NET 2.0 with http://msdn.microsoft.com/zh-cn/library/aa479390.aspx#XSLTsection139121120120
Scriptmanager http://msdn.microsoft.com/zh-cn/library/system.web.ui.scriptmanager.aspx
How to keep JavaScript scripts from failing http://social.microsoft.com/forums/zh-CN/295/thread/ba3620f1-7418-40ba-b7af-46d98cec2e4a in updatepanel
How to Implement JS call server method http://topic.csdn.net/u/20080907/13/fdfcdd60-946e-4fe5-a97a-d6c5ce198a81.html