ArticleDirectory
Yesterday, a classmate asked MVC in the Group how to make the backend call of the front-end js method. I told him that he still couldn't understand it for a long time. I was dizzy.
Call Javascript Many people may ask a question, whether in the forum or QQ group, that is, his background. Program How to call the frontend js method. It is not surprising to ask this question. The strange thing is how B/S works? Can we still "call" the way we work? B/S: browser/server, which is an application in Browser/Server mode. Browser is the browser. Where is the webpage after the browser opens the webpage? Client! Cache! Javascritpt runs on the client, while the background program runs on the server. How can the server call the client? Code What about it? What is the server's responsibility? Is responsible for page generation! Since the page can be generated, I can generate code that calls a js method. For example: < Script >
Function Alertme (){
Alert ( " Hello " );
}
</ Script >
I want to call this alertme. If the HTML code I generated contains <SCRIPT> alertme (); </SCRIPT>, is that all? How can this problem be solved? Output! ASP. NET also provides several different output methods, Registerclientscriptblock, registerstartupscript, registerclientscriptinclude, registerclientscriptresource Normally, we can use reponse. Write. So Which one should I use with so many registerclientscripts?
First, we need to know what these registerclientscripts do, and naturally we will know how to use them. Open msdn (we want to get used to viewing msdn) to see details of each. Protected Void Page_load ( Object Sender, eventargs E)
{
Page. clientscript. registerstartupscript (GetType (), " Alert " , " <SCRIPT type = text/JavaScript> document. Write ('registerstartupscript'); </SCRIPT> " );
Page. clientscript. registerclientscriptblock (GetType (), " Alert " , " <SCRIPT type = text/JavaScript> document. Write ('registerclientscriptblock'); </SCRIPT> " );
Page. clientscript. registerclientscriptinclude (GetType (), " Alert " , " Scripts/jquery-1.4.1.js " );
Page. clientscript. registerclientscriptresource (GetType (), " Jquery " );
}
Look at the generated HTML:
Code
<! -- This is Block -->
< Script Type = Text/JavaScript > Document. Write ( ' Registerclientscriptblock ' ); </ Script >
<! --This is include-->
<ScriptSRC= "Scripts/jquery-1.4.1.js"Type= "Text/JavaScript"> </Script>
<! -- This is source -->
< Script SRC = "/Webresource. axd? D = eSqmgi-l6CZy070cbDdcTJ63DvWE5qxNSXwJOSqYZW81 & amp; T = 634196210013506462" Type = "Text/JavaScript" > </ Script >
< Div >
</ Div >
<! -- This is startup -->
< Script Type = Text/JavaScript > Document. Write ( ' Registerstartupscript ' ); </ Script > </ Form >
We can see that startup is at the bottom of form, while block is at the beginning of form. Include is the same as block, while source is strange, Which is webresource.
From the function name, we can understand their meaning:
Startup starts and starts. Generally, some calling statements or DOM processing code are put, so it is most secure to output the code at least after the control is output.
Block Code blocks are generally placed in the function code for calling elsewhere. Of course, you can put any code on the premise that you need to know the JS running rules.
Include is actually connecting external JS files, such as my scripts directory has a jquery-1.4.1.js.
A resouce resource is generally used for custom control development. For example, when developing a control, we need to use some JS files, but our control will eventually be a DLL, where can I put JS? Anyone who has worked on winform knows what resource is. It is the resource file required by DLL, including icons, language packs, and static files. All these resources have a name, so registerresource is used for custom control development.
Again, when you click the button control, the page is submitted! The post page is opened, but the HTML output this time is much more than the JS Code you output before submission! Do you know how to make the page not Refresh after clicking submit? To make the output JS call effective, follow the JS running rules.
What are the Javascript rules? The javascript language is the language in which 10 thousand objects are all objects. Everything in your code is an object, a variable, and a method. Methods can be declared before or after the call, while variables must be declared before (otherwise, undefined ). Execute the code in the writing order (including the nesting order ).
If you call a method, a method declaration or reference is required on the page. If you are processing Dom, you need to output it in the DOM node or window. onload or document. Ready (jquery). Otherwise, document. getelementbyid () is usually undefinded.
Reponse. end is a special method that will stop IIS output. Therefore, if the submitted page uses response. End (), you must determine whether the resources used by your js method have output.
Now everyone understands how to "call" and the sequence of JS output. OK, I'm done! Another problem! Do you know how to use js to call the background. Cs method? This article is for your consideration. The following sections will introduce this question.