Asp.net+jquery How to realize the two-level linkage function of the provinces and municipalities

Source: Internet
Author: User
This article mainly introduces the method of implementing the two-level linkage function of the city and the jquery+asp.net, involving the operation skills of the ASP.

This paper describes the method of realizing the two-level linkage function of jquery+asp.net. Share to everyone for your reference, as follows:

Page HTML:

<%@ page language= "C #" autoeventwireup= "true" codefile= "DdlAjax.aspx.cs" inherits= "Threeajaxdrop_ddlajax"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

asp:

(1) Default.aspx.cs

public partial class threeajaxdrop_default:system.web.ui.page{  protected void Page_Load (object sender, EventArgs E  {    String sql = "Select * from Province";    String strtemp = "\" text\ ": \" {0}\ ", \" value\ ": \" {1}\ ""; Construct the format string {"text": "Beijing", "value": "00001"}    StringBuilder sb = new StringBuilder ();    OleDbDataReader reader = oledbhelper.executereader (sql);    while (reader. Read ())    {      string str1 = string. Format (strtemp, reader["province"]. ToString (), reader["Provinceid"]. ToString ());      Sb. Append ("{" +str1+ "},");    }    Reader. Close ();    String json = sb. ToString ();    Response.Write ("[" +json. Substring (0,json. LENGTH-1) + "]");}  }

(2) Handlerdropdownajax.ashx

public class Handlerdropdownajax:ihttphandler {public void ProcessRequest (HttpContext context) {if (context. request.querystring["type"]! = NULL && context. request.querystring["FID"] = null) {String type = context. request.querystring["type"]. ToString (); Mainly used to identify whether the query city or the area table string fid = context. request.querystring["FID"].   ToString ();      The parent ID of the city or region String sql = "SELECT * from" + type + "where father= '" + FID + "'"; Construct the type of data [{"Text": "Nanchang", "Value": "0001"},{"text": "Shangrao", "Value": "0002"}]//string strtemp = "{\" text\ ": \" {0}\ ", \" value \ ": \" {1}\ "}";//A mistake has been made here: the direct construction will be wrong, because the curly braces are formatted with curly braces, the parsing will be error string strtemp = "\" text\ ": \" {0}\ ", \" value\ ": \" {1}\ "";      Construct the format string {"text": "Beijing", "value": "00001"} StringBuilder sb = new StringBuilder ();      OleDbDataReader reader = oledbhelper.executereader (SQL); while (reader. Read ()) {string str1 = string. Format (strtemp, reader[2]. ToString (), reader[1].        ToString ()); Sb. Append ("{" + str1 + "},"); The curly braces on both sides are formatted and added} reader.      Close (); String json = sb.      ToString (); Context. Response.Write ("[" + JSON. Substring (0, JSON. LENGTH-1) + "]");    The function of substring is to remove the last ' comma '} public bool IsReusable {get {return false; }  }}
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.