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; } }}