Web development--asynchronous calls to and from the front and back tables

Source: Internet
Author: User

  Do Web development, the most headache, the core of the part may be the front and back of the interaction, has not been understood, each time do not know how to do. Recently, due to development needs, coupled with some friends to ask this question, have to re-explore the front and back of the method of interaction. The kung fu is a success.

Question Description: For example, how to display the contents of the background read database in the blue box in the foreground according to the dropdown box selected in the Red box???

  

  Workaround:

(1) Front desk:

The page has a Select selection box and text text box. Note that select has a onchange event, which is to respond to the user's actions in the foreground.

    <select id= "Select1" name= "D1" runat= "Server" onchange= "return DataChange ()"></Select >

<input id= "Text1" type= "text"/>

The response event specifically implements the following code, in the response event DataChange () , first gets the value of the drop-down box selected by the user, and passes the value to the background for appropriate processing.

  <%=sCallBackFunctionInvocation%> is the portal to the asynchronous invocation.

The showmsg (ARG) function, arg , is used to receive data that is returned to the foreground after being spooled, only to display the data returned in the background in a text box. 

+ View Code?
1 2 3 4 5 6 7 8 9 10 <script language="javascript" type="text/javascript">         function datachange(){             var message = document.getElementById("Select1").value;             <%=sCallBackFunctionInvocation%>         }         function showMsg(arg)         {             document.getElementById("Text1").value = arg;         }     </script>

OK, the front desk is easy to take care of.

  (2) Backstage

First Paste Code ~ ~  

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public partial class _Default : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler {     public string sCallBackFunctionInvocation;     public string strResult = "";     protected void Page_Load(object sender, EventArgs e)     {         Select1.Items.Add("aa");         Select1.Items.Add("bb");         Select1.Items.Add("cc");        sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "showMsg", "");     }     public void RaiseCallbackEvent(string arg)     {         if (arg == "aa")         {             //执行相应的函数或语句             strResult = arg + "——来自后台哦11";         }         else if (arg == "bb")         {             //执行相应的函数或语句             strResult = arg + "——来自后台哦22";         }         else if (arg == "cc")         {             //执行相应的函数或语句             strResult = arg + "——来自后台哦33";         }     }     public string GetCallbackResult()     {         return strResult;     } }

from the above code, you first need to inherit the System.Web.UI.ICallbackEventHandler, otherwise you will report the following error at run time:  

Again, you need to register the client function in the Page_Load () function. Page.ClientScriptManager.GetCallbackEventReference (Control, string (argument), String (Clientcallback), String ( Context) to obtain a reference to a client function that, when called, initiates a client callback to the server-side event. The client function for this overloaded method contains the specified controls, parameters, client script, and context.

Thirdly, in the raisecallbackevent (string arg) function, arg is the value passed from the foreground, which is the value selected in the Select drop-down box. in this function, the developer can do the corresponding operation according to the value transmitted by the foreground, and the processing result is stored by strresult . (originally intended to read the contents of the database, but this blog post is mainly to explore the front and back of the interaction, the use of the simplest statement to replace it).

Thestring GetCallbackResult () function is primarily intended to handle the result of a postback to the client, the strresult. Come to a few results diagram ~

at this point, a simple but powerful function of the Web before and after the data call to realize, as long as the understanding of the processing mechanism, it feels very simple, is not?! O (∩_∩) o

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.