. NET AJAX interacts with the background general handler (ASHX)

Source: Internet
Author: User

    This article mainly realizes the non-dynamic refresh query background data function, mainly uses the ajax+ashx+sqlserver to carry on the interaction.

You first need to reference jquery:

<language= "javascript"  type= "Text/javascript"  src =".. /js/jquery.js ">

HTML script:

<ID= "Tb_corpname"  runat= "Server"  MaxLength= " "  Width=" 369px "></asp:textbox>

The foreground uses an event to invoke ASHX:

<script type= "Text/javascript" >    $(function () {        $("#tb_corpName"). Blur (function() {//Mouse loses focus event            varCorpname = $ ("#tb_corpName"). Val (); $.ajax ({type:"Post",//How to submitURL: "/ashx/fzgpyshowdata.ashx",//path to the generic handlerData: {Corpname:corpname},//values passed in to the backgroundDataType: "JSON",//return value FormatSuccessfunction(data) {//return the thing that will be done after success, here is the return of a table$ ("#tb_CreditCode"). Val (data[0].                        Creditcode); $("#tb_corpCode"). Val (data[0].                    Corpcode); $("#tb_linkTel"). Val (data[0].                    Linkphone); $("#corpProvince option[text= '" + data[0]. StateName + "']"). attr ("Selected",true);                    Showcity (); $("#corpCity option[text= '" + data[0]. Adminareaname + "']"). attr ("Selected",true); $("#tb_address"). Val (data[0].                    Address); $("#tb_linkMan"). Val (data[0].                    Linkman); $("#tb_Mobile"). Val (data[0].                Linkmobile);        }            }); })    })</script>

Backstage to receive the value sent by the foreground, to manipulate it:

        Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; stringCorpname = context. request["Corpname"]. Trim ().      ToString (); //Receive the parameters sent by the front desk. if(Corpname! ="")            {                stringsql =@"Select top 1 corpname,creditcode,corpcode,linkphone,statename,adminareaname,linkman,linkmobile,address from                                        Tbproductorder as a inner join tbstatedic as B on a.statenum = B.statenum                                        INNER JOIN tbadminareaclass on a.citynum = Adminareaclassid where Corpname [email protected] ORDER by Corpname"; sqlparameter[] par=Newsqlparameter[1]; par[0] =NewSqlParameter ("@CorpName", Corpname); DataSet DS=dbherpler.load (SQL, par); stringJSON = Serializerhelper.tojsonstring (ds. tables[0]);   //returns JSON-type data context.                                      Response.Write (JSON); Context.             Response.End (); }         }

If more than one parameter is passed into the background, the data is separated by commas to write multiple parameters:

Data: {corpname:corpname, corpname2:corpname2}

. NET AJAX interaction with the background general handler (ASHX)

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.