I. frontend call background
There are two main methods for calling the background at the front end: using Html controls and using Asp controls. 1. Asp controls are very convenient and easy to surprise you! For example, put a LinkButton on the front-end and double-click it to enter the background. Then, you can easily write the code you need!
For example, add a server control Button in the foreground
Double-click to enter the background. You can add your own code. Here is a simple example:
Protected void button#click (object sender, EventArgs e) {Response. Write (Server Control !); }
2. html controls. However, Asp controls are server controls that consume resources, and their flexibility and compatibility are inferior to Html controls. Therefore, a large number of projects in the use of Html controls, while the html control in addition to add runat = server method (see the control display hidden method: http://blog.csdn.net/wlccomeon/article/details/16995481) there are two indirect ways to deal with the background: using Ajax and submitting forms, JavaScript is required here. The following is an example: ① submit a form
First, add an html control in the form or just a tag:
Then, add the Js function to submit the form: // Login, and submit the form function Login () {var form = document. forms [login]; // obtain the id form of the form to be submitted. action = default. aspx; // refresh the target page form. method = post; // form. the default data submission method of method is get, which is applicable to the form Control of the server. submit (); // submit}
After running, click "Log on" a tag to refresh default and run the background code. Note that the background code to be triggered by this method must be written in Page_Load.
② Use Ajax
First, add a tag:
Compile JS Code
// Log out of function loginQuit () {$. ajax ({type: Post, url: Exit. ashx, // exit the General handler onSubmit: function () {}, // determine whether the result is correct success: function (result) {if (result = T) {// hide the document before logon. getElementById (loginAfter ). style. display = none; // hide document. getElementById (loginBefore ). style. display = block; // display /// clear the user input record document. getElementById (UserName ). value =; document. getElementById (txtCode ). value =; Document. getElementById (Password). value =;} else {alert ('logout failed! ')}}});}
Create a general handler Exit. ashx and add the Code:
public void ProcessRequest(HttpContext context) { context.Session[UserName]=null; context.Session[UserID] = null; context.Response.ContentType = text/plain; context.Response.Write(T); }The background code can perform related operations based on whether the Session exists.
2. Call the foreground at the background
There are many methods for calling the front-end in the background. Currently, there are roughly two types of methods used: 1. Using the Response. Write method, this method can only call built-in functions in js:
Response. Write (<script type = 'text/javascript '> alert ('success! '); </Script> );If this JS function is frequently used in the background, it can be abstracted as a class (jumpFrame in this example) and added to the class as follows:
public static void AlertBox(string errMsg) { StringBuilder sb = new StringBuilder(); sb.Append(<script language=javascript> ); sb.Append(alert( + errMsg.Trim() + ); ); //sb.Append(window.location='test.aspx';); sb.Append(</script>); System.Web.HttpContext.Current.Response.Write(sb.ToString()); }Call:
Protected void button#click (object sender, EventArgs e) {jumpFrame. AlertBox (success !); }
2. Use the ClientScript class to dynamically add scripts. You can use JS udfs as follows: add code where you want to call a Javascript script function, make sure that MyFun has been defined in the script file.
ClientScript.RegisterStartupScript(ClientScript.GetType(), myscript, <script>MyFun();</script>);
It takes a lot of effort to explore, practice, solve various problems, and finally apply these methods to your project. In this experience, it is a very good learning process to make simple use of unfamiliar knowledge, however, it is a good review process for videos that have already been viewed, such as the news publishing system and the Qingdao qingting course. Learning without it, over time it will be unfamiliar or even forgotten; and using Middle School is more powerful and more profound than simply watching videos and Typing Code. Finally, if you have a better method or what is wrong with the above application, please kindly advise!