"ASP.net" uses Ajax and jquery to pass parameters to the background and return an instance of the value in the foreground

Source: Internet
Author: User
First, the preface

In the past when the project encountered ASP.net before and after the platform through the AJAX data, at that time done, they have not summed up, although the use of fast, but still want to sum up for later study, thinking. second, the main points of analysis

ASP.net's front desk can enter the background by pressing F7, but add [WebMethod] when you want to use Ajax to capture AJAX-transmitted parameters in the background. However, a lot of the Ajax that can be captured in asp.net is done using "generic handlers." See below for specific actions. iii. Programme of Use

Here is a small series of two schemes to achieve the transfer of data: a direct call to the background

Front desk:

    <%--introduces jquery--%>
    <script src= "js/jquery-2.1.1.min.js" ></script>
    <%--to the background using AJAX to pass parameters, Call background parameter--%>
    <script type= "Text/javascript" >
        $ (function () {
            <%--fires when txtUserName loses focus--% >
            $ (' #txtUserName '). blur (function () {
                var username = $ (this). Val ();
                $.ajax ({
                    type: "Post",
                    ContentType: "Application/json",//value in the way
                    URL: "Index.aspx/getvalueajax",// Webajaxforme.aspx is the target file, Getvalueajax is the method data in the destination file
                    : "{username: '" + Username + "'}",//username To ask for background-passed parameters (where the parameters are optional)
                    success:function (Result) {
                        alert (RESULT.D);//RESULT.D parameters returned in the background
                    }
                })
            })
        })
    </script>


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

Background: Be sure to add [WebMethod]

        [WebMethod] public      
        static string Getvalueajax (string username) must be added at the front of the webmethod]//method This method requires a static method to use the keyword static       
        {
            //Here you can perform any operation on the incoming parameter return           
            username;
        }
Programme II General processing procedures

After you create a generic handler ajaxtest.ashx, complete the following actions:

Front desk:

     <%--introduced jquery--%>
     <script src= "js/jquery-2.1.1.min.js" ></script>

     <%-- Using AJAX to pass parameters to a generic handler, call the function--%>
     <script type= "Text/javascript" >
         $ (function () {
              <%-- Triggers--%>
             $ (' #txtYiBan ') when Txtyiban loses focus. blur (function () {
                 var username = $ (this). Val ();
                 $.ajax ({
                     type: "Get",
                     URL: "ajaxtest.ashx?json=" + username,//ajaxtest.ashx as target file
                     dataType: "Text",
                     success:function (Result) {
                         alert (RESULT.D);//result.d parameters returned by background
                     }
    })}) </script>

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

AJAXTEST.ASHX General processing Program:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Services;

Using System.Web.SessionState; namespace Aspajaxtest {///<summary>///ajaxtest Summary description///</summary> [WebService (namespace = "http://tempuri.org/")] [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1)] public class Ajaxtest:ih
        Ttphandler {HttpContext context; <summary>///Gets the passed value and calls other methods///</summary>///<param name= "context" ></p
            aram> public void ProcessRequest (HttpContext context) {context = context; Context.
            Response.Clear (); Context. Response.ContentType = "text/html;
            Charset=utf-8 ";

            Gets the value that came from string methodname = GetQueryString ("JSON"); Other methods can be invoked------see the following}///<summary>///get the values passed///</summary>///<p Aram Name= ' name ' ></param>///<returns></returns> string getquerystring (string name) {
        Gets the passed value return Context.request.params[name];
            public bool IsReusable {get {return false; }
        }
    }
}

       methods that can be invoked:

private String Getjsonstr (String methodname, String session, String strname, String Userid, String strwhere) {
            String jsonstring = ""; Switch (methodname) {case "getworkscontent"://Paging effect jsonstring = n EW JCZB. Ymgj. Web.Web.WebManager.home.AddHistory ().
                    Getrmcontent (session);
                    jsonstring = "1";
                Break Case "getworkssetcontent"://Paging effect jsonstring = new JCZB. Ymgj. Web.Web.WebManager.home.worksset ().
                    Getrmcontent (Session,strname,userid);
                Break Case "getworkcontent"://Paging effect jsonstring = new JCZB. Ymgj. Web.Web.WebManager.home.works ().
                    Getrmcontent (session, strwhere);
                Break Case "getworkssetbytimecontent"://Paging effect jsonstring = new JCZB. Ymgj. Web.Web.WebManager.home.workssetByTime (). Getrmcontent (Session, StrnamE, Userid);
                Break Case "getworkssetbypositioncontent"://Paging effect jsonstring = new JCZB. Ymgj. Web.Web.WebManager.home.workssetByPosition ().
                    Getrmcontent (Session, strname, Userid);
                Break Case "getworkssetbyartistidcontent"://Paging effect jsonstring = new JCZB. Ymgj. Web.Web.WebManager.home.workssetByArtistid ().
                    Getrmcontent (Session, strname, Userid);
            Break
        return jsonstring; }
Iv. What to learnIn the background of ASPX, you can capture parameters in the following ways:
Workstatevalue = request.querystring["Workstatevalue"];
In a generic handler, you can use:
return Context.request.params[name];
Iv. Summary

These are more commonly used, whether it is any language, must be a pious attitude to learn, so as to improve their proficiency, not forget how to do. Come on. Learn to forestall.

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.