Ajax and Json implements a background collection to the foreground and assigns a value to the text box-----ajax\json\jquery

Source: Internet
Author: User

Function: Put two text boxes and a OK button, determine button binding point Click event, click OK to load the data in the background database, display in the text box.

Basic knowledge: JQuery ajax async. Eval (). each () JSON data

1. html file

<DivID= "Text"> <inputtype= "text"ID= "Comment1"name= "T_a21"Numberid= "One"value="" /> <inputtype= "text"ID= "Comment2"name= "T_a22"Numberid= "One"value="" /> <inputtype= "button"class= "Button1"onclick= "Showsenddialog (' Divoutputselcet ')" >Are you sure</Intput> <inputtype= "text"ID= "Hidcomment"value= "321" /></Div>

2. js file

function(obj) {$.ajax ({URL:"Relative Path/xxx.ashx", Global:false, DataType:"JSON", data:{"Functiontype": "Comment", "PrimaryKey": $ ("#hidcomment"). Val ()},//to the background with the parameters success inside can not be used success:function(data) {varresult =eval (data. Data);varNumberid = "One"varInputlist = $ ("#Text"). Find ("input[numberid=" + numberid+ "']");//Theoretically the ID cannot be duplicated if the repetition can be located with $ ("#Text #txtComment")$ (inputlist). each (function(){   for(vari = 0; i < result.length; i++) {    varTempcode = $ ( This). attr ("name"). Substring (2,5); if(Result[i]. Linecode = = Tempcode) {//Result[i]. Linecode Background database Linecode field matches the foreground text box name value 2-5 phase$( This). Val (Result[i]. Itemvalue)//field Itemvalue in database      }}); /Another notation *$ ("#Text"). Find ("input[numberid=" + numberid+ "']"). each (function () {}*/},error:function(str) {alert (str. Message);} });}

3, Backstage Xxx.ashx

 Public voidProcessRequest (HttpContext context) {context. Response.ContentType="Text/plain"; stringFunctiontype = context. request["Functiontype"].        ToString (); Switch(functiontype) { Case "Comment": HttpContext.Current.Response.Write (Getcomment ());  Break; }    }   Public stringgetcomment () {Try        {            stringprimarykey= httpcontext.current.request["PrimaryKey"]; ENTERPRISECOMMENTBLL ECBLL=NewENTERPRISECOMMENTBLL ();//Business Logic LayerEnterprisecommentmodel Ecmodel = Efcbll.getmodel (orderId);//Model Class Library            return NewPagemessageex (true,"successful delivery of information", Efcmodel). ToString ();//The PageMessageEx.cs file placed in App_Code encapsulates a method,. ToString () is a well-encapsulated property converted to JSON data        }        Catch(Exception ex) {return NewPagemessage (false, ex. Message). ToString ();//The PageMessage.cs file placed in App_Code encapsulates a method,. ToString () is a well-encapsulated property converted to JSON data        }    }

4, PageMessage.cs and PassageMessageEx.cs (placed in App_Code)

(1) Pagemessage

usingSystem;usingNewtonsoft.json;/// <summary>///provides a data carrier for the return information of the server-side Ajax call to the page/// </summary> Public classpagemessage{ Publicpagemessage () {}/// <summary>    ///constructs a return information object and specifies the operation result and prompt information/// </summary>    /// <param name= "Result" >Operation Result</param>    /// <param name= "message" >Prompt Information</param>     PublicPagemessage (BOOLResultstringmessage) {         This. _result =result;  This. _message =message; }    /// <summary>    ///Results/// </summary>    Private BOOL_result; /// <summary>    ///Result Information Description/// </summary>    Private string_message; /// <summary>    ///Gets or sets the error result/// </summary>     Public BOOLResult {Get        {            return_result; }        Set        {             This. _result =value; }    }    /// <summary>    ///Gets or sets the result information description/// </summary>     Public stringMessage {Get        {            return_message. Replace ("\ r \ n", String.Empty); }        Set        {             This. _message =value; }    }        /// <summary>    ///returns the JSON representation of the information/// </summary>    /// <returns>JSON representation</returns>     Public Override stringToString () {returnJsonconvert.serializeobject ( This); }}

(2) Pagemessageex (inheriting pagemessage)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingNewtonsoft.json;/// <summary>///Summary description of Pagemessageex/// </summary> Public classpagemessageex:pagemessage{ PublicPagemessageex () {//        //TODO: Add constructor logic here//    }     PublicPagemessageex (BOOLResultstringMessageObjectdata) {         This. Result =result;  This. Message =message;  This. Data =data; }    /// <summary>    ///Data/// </summary>    Private Object_data; /// <summary>    ///Gets or sets the data/// </summary>     Public ObjectData {Get{return_data;} Set{_data =value;} }     Public Override stringToString () {returnJsonconvert.serializeobject ( This); }}

Ajax and Json implements a background collection to the foreground and assigns a value to the text box-----ajax\json\jquery

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.