Previous Code series-Ajax and JSON drop-down list box

Source: Internet
Author: User
ArticleDirectory
    • Front-end page
    • Background page -- manually splice JSON strings

CodeTest use

Front-end page
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "getgradeclassinfo. aspx. cs" inherits = "Eyes. Web. getgradeclassinfo" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">  VaR  Xhr; window. onload = Function () {Xhr = New  XMLHttpRequest (); getdatabyajax (); showmsg ();}  Function  Showmsg () {document. getelementbyid ( "Msgdiv"). style. Display = "Block" ; Document. getelementbyid ( "Msgimg"). src = "images/load.gif" ;}  Function  Getdatabyajax () {xhr. Open ( "Get", "getgradeclassinfo. aspx? Isajax = 1 ", True ); Xhr. setRequestHeader ( "If-modified-since", "0 "); //  No Cache Xhr. onreadystatechange = Watching; xhr. Send (  Null  );}  //  Storage server returned grade/class information          VaR  Jsonresult;  Function  Watching (){  If (Xhr. readystate = 4){  If (Xhr. Status = 200 ){  VaR Resstr = Xhr. responsetext; jsonresult = Eval ("(" + resstr + ")" ); Addgradestoselectcontrol (jsonresult. Grades); addclassestoselectcontrol (jsonresult. Grades [ 0 ]. ID); document. getelementbyid ( "Msgdiv"). style. Display = "NONE" ;}  Else {Alert ( "Server error" + Xhr. Status );}}}  //  Select an array containing the grade information ---> Generate the grade drop-down list option          Function  Addgradestoselectcontrol (gradearr ){  VaR Selgrades = Document. getelementbyid ("grades" );  For ( VaR I = 0; I <gradearr. length; I ++ ){  VaR Option = New  Option (gradearr [I]. Name, gradearr [I]. ID); selgrades. Options. Add (option );}}  //  Select an array containing the class information ---> Generate the class drop-down list option          Function  Addclassestoselectcontrol (gradeid ){  VaR Selclasses = Document. getelementbyid ("classes" ); Clearsel (selclasses );  For ( VaR I = 0; I <jsonresult. classes. length; I ++){  If (Gradeid = Jsonresult. classes [I]. cgid ){  VaR Option = New  Option (jsonresult. classes [I]. Name, jsonresult. classes [I]. ID); selclasses. Options. Add (option );}}}  //  Clear drop-down list box          Function  Clearsel (selobj ){  For ( VaR I = selobj. Options. Length-1; I>-1; I -- ) {Selobj. Options [I] = Null  ;}} </SCRIPT> Background page -- manually splice JSON strings
 Using  System;  Using System. Collections. Generic;  Using  System. LINQ;  Using  System. Web;  Using  System. Web. UI;  Using  System. Web. UI. webcontrols;  Using  System. text;  Namespace  Eyes. Web {  Public   Partial   Class Getgradeclassinfo: system. Web. UI. Page {BLL. grademanager = New  Bll. grademanager (); BLL. classmanager = New  Bll. classmanager ();  //  Age Information List <model. Grades> listgrades = Null  ; List <Model. Classes> listclasses = Null  ;  Protected  Void Page_load ( Object  Sender, eventargs e ){  If (! String . Isnullorwhitespace (request. querystring [ "  Isajax  "  ]) {System. Threading. thread. Sleep (  5000  ); Loaddata ();  String Gradearrstr =Getgradedatastring ();  String Classarrstr = Getclassdatastring (); response. Write (  "  {Grades:  " + Gradearrstr + "  , Classes:  " + Classarrstr + "  }  "  ); Response. End ();}}  ///  <Summary>          ///  Obtain the grade data string  ///   </Summary>          ///   <Returns>  [{ID: 1, name: "eyes" },{}]  </Returns>          Public   String  Getgradedatastring () {stringbuilder sbstr = New Stringbuilder ( " [  "  );  Foreach (Model. Grades Model In  Listgrades) {sbstr. append (  "  {ID:  "  ); Sbstr. append (model. GID); sbstr. append (  "  , Name :\"  "  ); Sbstr. append (model. gname); sbstr. append ( "  \"},  "  );}  Return Sbstr. tostring (). substring ( 0 , Sbstr. Length- 1 ) + "  ]  "  ;}  ///   <Summary>          ///  Obtain the class data string ///   </Summary>          ///   <Returns> </returns>          Public   String  Getclassdatastring () {stringbuilder sbstr = New Stringbuilder ( "  [  "  );  Foreach (Model. Classes Model In Listclasses) {sbstr. append (  "  {ID:  "  ); Sbstr. append (model. CID); sbstr. append (  "  , Name :\"  "  ); Sbstr. append (model. cname); sbstr. append (  "  \ ", Cgid:  "  ); Sbstr. append (model. cgid); sbstr. append ( "  },  "  );}  Return Sbstr. tostring (). Trim ( '  ,  ' ) + "  ]  "  ;}  Public   Void  Loaddata () {listgrades =Grademanager. getallgrades (); listclasses = Classmanager. getallclasses ();}}} 
Related Article

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.