Interaction between the js frontend and backend data-frontend and backend

Source: Internet
Author: User
The website is programmed around the database. It is centered on the data in the database and operates the data through the background, the data is then transmitted to the foreground for display (of course, the background code can be embedded in the foreground ). That is, the following describes the data interaction between the foreground and the background.

The website is programmed around the database. It is centered on the data in the database and operates the data through the background, the data is then transmitted to the foreground for display (of course, the background code can be embedded in the foreground ). That is:


  


The following describes how to interact data between the foreground and the background. The foreground calls the background methods and variables and calls the foreground js Code. This article first introduces the former, which will be introduced in later articles.


Foreground call background methods and variables:
Method 1: implemented through WebService Steps:

Background

? First introduce the namespace (using System. Web. Services ;)

? Define public static methods (public and static methods must be used, and static methods cannot access external non-static variables. At this time, the background and foreground are equivalent to the relationship between the parent class and the subclass ), add [System. web. services. webMethod] to mark method features.

Front-end

? Add the ScriptManager Server Control and set its EnablePageMethods attribute to true.

? Call the public and static methods defined in the background using the PageMethods Method

PageMethods Method Introduction:

PageMethods. FunctionName (Paramter1, Parameter2,..., funRight, funError, userContext );

1) Paramter1, Parameter2,..., indicates the FunctionName parameter. The type is Object or Array;

2) funRight is the callback function after the method call is successful. It processes the return value.

3) funError is the Js method (fault tolerance) executed when the FunctionName method in the background is abnormal ),

4) userContext is any content that can be passed to the SuccessMethod method or FailedMethod method.

Example:

Background code:


  

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. web. services; namespace WebApplication4 {public partial class WebForm10: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} [WebMethod] public static string test1 (string userName) {return "hello" + userName + ", this is the frontend call background method through WebService ";}}}


Front-end code:


  

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm10.aspx.cs" Inherits="WebApplication4.WebForm10" %>
         


Click the button to bring up the following dialog box:


  


VcD4KPHA + PGJyPgo8L3A + cjxwp?vcd4kpggyigfsawdupq = "left"> Method 2: Use <% = methodname () %> and <% # methodname () %> (methodname () method defined for the background)

The background method called by this method may appear in the foreground in three situations:

1) properties of server-side controls or HTML controls

2) Client js Code

3) Position of the Html display content (it is used as a placeholder to display the variable at the position where the symbol appears)

Here is a simple example of the two. The details are described in the following article.

Steps:

Background

? Variables or Methods defining public or protected (cannot be private)

Front-end

? Call the background variables or methods directly with <% = %> and <% # %>, the usage of the two is slightly different (<% # %> can basically implement the <% = %> function)

Example:

Background code:


  

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace WebApplication4 {public partial class WebForm1: System. web. UI. page {public string name = "I Am a background variable"; protected void Page_Load (object sender, EventArgs e) {this. dataBind () ;}// cannot be private protected string strTest () {return "this is the frontend method called by calling the background method through <% # %>";} public void strTest2 () {Response. write ("this is the frontend to call the background method through <% = %> ");}}}


Front-end code:


  

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
         


Running result:


  


<% = Methodname () %> and <% # methodname () %> are described in detail in the following article.


Method 3: Hide the server button

Background code:


  

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace WebApplication4 {public partial class WebForm11: System. web. UI. page {protected void button#click (object sender, EventArgs e) {Response. write ("this is the method for foreground access to the background by hiding controls ");}}}


Front-end code:


  

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm11.aspx. cs" Inherits = "WebApplication4.WebForm11" %>
         
Related Article

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.