Jquery.ajax () calls the ASP.net background method __.net

Source: Internet
Author: User

Using jquery's $.ajax () makes it easy to invoke the ASP.net background method:

Detailed reference: http://www.cnblogs.com/zxhoo/archive/2011/01/30/1947752.html

1, parameter-free method calls

C # Background code:

Using System.Web.Services;
[WebMethod]
    public static string Sayhi ()
    {return
        ' hi,welcome to china! ';
}

Note: 1. Methods must be static and have [WebMethod] declarations.

HTML code:

<div>
        <asp:button id= "Btnclick" runat= "Server" text= "click Me"/> <br "/> <span id=
        " Msg "></span>
 </div>

jquery Code:

Code

<script type= "Text/javascript" >
        $ (document). Ready (
        function () {

            $ ("#btnClick"). Bind ("click", function () {
                $.ajax ({
                    type: Post),
                    URL: "Ajaxhandler.aspx/sayhi",
                    contentType: "Application/json;" Charset=utf-8 ",
                    dataType:" JSON ",
                    success:function (data) {
                        $ (" #msg "). CSS (" Color "," #0000FF "). HTML ( DATA.D);
                    },
                    error:function (err) {
                        $ ("#msg"). CSS ("Color", "#FF0000"). HTML ("Access Faield:" + err);
                    }
                });
                return false;
            }

); </script>

2. Method calls with parameters

C # Background code:

[WebMethod]
    public static string Sayhi (string address, string name)
    {return
        "Hi," + address + "" + Name;
    }

HTML code:

<div>
    <asp:button id= "Btnclick" runat= "Server" text= "click me"/> address:<asp:textbox
    "id=" Txtaddress "runat=" server ></asp:TextBox>
    family name:<asp:textbox id= "txtname" runat= "Server" > </asp:TextBox> <br/>
    <span id= "msg" ></span>
</div>

jquery Code:

<script type= "Text/javascript" >
        $ (document). Ready (
        function () {

            $ ("#btnClick"). Bind ("click", function () {
                var add = $ ("#txtAddress"). Val ();
                var txtname = $ ("#txtName"). Val ();
                $.ajax ({
                    type: "Post",
                    URL: "Ajaxhandler.aspx/sayhi",
                    data: "{' Address ': '" + add + "', ' name ': '" + txtname + " '} ',
                    contentType: "Application/json; Charset=utf-8 ",
                    dataType:" JSON ",
                    success:function (data) {
                        $ (" #msg "). CSS (" Color "," #0000FF "). HTML ( DATA.D);
                    },
                    error:function (err) {
                        $ ("#msg"). CSS ("Color", "#FF0000"). HTML ("Access Faield:" + err);
                    }
                });
                return false;
            }

); </script>

3. Returns the call to the List collection method

C # Background code:

[WebMethod]
    public static list<string> Sayhi (string address, string name)
    {
        list<string> List = new list< String> ();
        for (int i = 0; i < i++)
        {
            list. Add ("Hi:" + i.tostring ());
        }
        List. Add ("Hi:" + address + "" + name);
        return list;
}

HTML code:

<div>
        <asp:button id= "Btnclick" runat= "Server" text= "click me"/> address:<asp:textbox
        "id=" Txtaddress "runat=" server ></asp:TextBox>
        family name:<asp:textbox id= "txtname" runat= "Server" ></asp:TextBox>
        <br/>
        <ul id= "msg" >
        </ul>
</div>

jquery Code:

<script type= "Text/javascript" > $ (document). Ready (function () {$ ("#btnClick"). Bind ("cl
                Ick ", function () {var add = $ (" #txtAddress "). Val ();
                var txtname = $ ("#txtName"). Val (); $.ajax ({type: "post", url: "Ajaxhandler.aspx/sayhi", data: "{ ' Address ': ' + add + ', ' name ': ' + txtname + '} ', ContentType: "Application/json;
                        Charset=utf-8 ", DataType:" JSON ", success:function (data) {
                        $ ("#msg"). HTML ("");
                        $ (DATA.D). each (function () {$ (' #msg '). Append ("<li>" + This + "</li>");

                        });
                    $ ("#msg"). CSS ("Color", "#0000FF"); Error:function (ERR) {$ ("#msg"). CSS ("Color", "#FF0000"). HTML ("Access Faiel D: "+ ERR);
                }
                });
            return false;

        });

}); </script>

4, return the call to the SortedList method

C # Background code:

[WebMethod]
    public static sortedlist<string, string> Sayhi (string address, string name)
    {
        sortedlist<string, string> SL = new sortedlist<string, string> ();
        for (int i = 0; i < i++)
        {
            sl. ADD (i.tostring () + "_key", i.tostring () + "_value");
        }
        Sl. ADD ("_key", "_value" + address + "" + name);
        return SL;
    }

HTML code:

<div>
        <asp:button id= "Btnclick" runat= "Server" text= "click me"/> address:<asp:textbox
        "id=" Txtaddress "runat=" server ></asp:TextBox>
        family name:<asp:textbox id= "txtname" runat= "Server" ></asp:TextBox>
        <br/>
        <ul id= "msg" >
        </ul>
</div>

jquery Code:

<script type= "Text/javascript" > $ (document). Ready (function () {$ ("#btnClick"). Bind ("cl
                Ick ", function () {var add = $ (" #txtAddress "). Val ();
                var txtname = $ ("#txtName"). Val (); $.ajax ({type: "post", url: "Ajaxhandler.aspx/sayhi", data: "{ ' Address ': ' + add + ', ' name ': ' + txtname + '} ', ContentType: "Application/json;
                        Charset=utf-8 ", DataType:" JSON ", success:function (data) {

                        $ ("#msg"). HTML ("");
                        This only takes a partial key, the value displays $ ("#msg"). Append ("<li>" + data.d["0_key"] + "</li>");
                        $ ("#msg"). Append ("<li>" + data.d["1_key"] + "</li>");
                        $ ("#msg"). Append ("<li>" + data.d["2_key"] + "</li>"); $ ("#msg"). Append ("<li>" + data.d["_key"] + "</li>");
                    $ ("#msg"). CSS ("Color", "#0000FF"); Error:function (ERR) {$ ("#msg"). CSS ("Color", "#FF0000"). HTML ("Access Faiel
                    D: "+ err);
                }
                });
            return false;

        });

}); </script>

5. Manipulating XML

XML File Code:

<?xml version= "1.0" encoding= "Utf-8"?> <books> <book> <name>asp.net 3.5 Advanced Program Design (2nd edition) ;/name> <author> McDonnelburth Studio </author> <wordCount>2034000</wordCount> <p Rice>76</price> </book> <book> <name>asp.net 3.5 Introductory Classic </name> <a Uthor> (Netherlands) Spangas (spaanjaars,i.) </author> <wordCount>1046000</wordCount> <price>78.5</price> &LT;/BOOK&G
    T
        <book> <name>c# Advanced Programming (5th Edition) Upper and lower volumes </name> <author> (US) Nagel (NAGEL.C) etc </author>
        <wordCount>24770000</wordCount> <price>124</price> </book> <book> <name>asp.net Ajax Combat </name> <author> (US) McClure, Gravich (United States) Oher etc </author> <w ordcount>511000</wordcount> <price>44</price> </book> &LT;BOOK&GT <name>asp. NET program Development paradigm (C #) (2nd edition) </name> <author> Zhangyuehua extension, Suyu, Guan Wei Red </author> <wordcount>1419000</wor dcount> <price>71.2</price> </book> </books>

HTML code:

<div>
        <asp:button id= "Btnclick" runat= "Server" text= "click me"/> address:<asp:textbox
        "id=" Txtaddress "runat=" server ></asp:TextBox>
        family name:<asp:textbox id= "txtname" runat= "Server" > </asp:TextBox>
        <br/>
        <ul id= "msg" >
        </ul>
</div>

jquery Code:

<script type= "Text/javascript" > $ (document). Ready (function () {$ ("#btnClick"). Bind ("CLI
                    CK ", function () {$.ajax ({url:" books.xml ", DataType:" xml ",
                        Success:function (XmlData) {$ ("#msg"). HTML ("");
                            $ (xmlData). Find ("Books>book"). each (function () {$ ("#msg"). Append ("====new book====");
                            $ ("#msg"). Append ("<li>name:" + $ (this). Find ("name"). Text () + "</li>");
                            $ ("#msg"). Append ("<li>author:" + $ (this). Find ("Author"). Text () + "</li>");
                            $ ("#msg"). Append ("<li>wordcount:" + $ (this). Find ("WordCount"). Text () + "</li>");
                        $ ("#msg"). Append ("<li>price:" + $ (this). Find ("price"). Text () + "</li>");
                        }); $ ("#msg"). CSS ("Color", "#0000FF"); Error:function (ERR) {$ ("#msg"). CSS ("Color", "#FF0000"). HTML ("Access Faiel
                    D: "+ err);
                }
                });
            return false;

        });

}); </script>

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.