Using Ajaxpro to implement two-level linkage code under ASP.net

Source: Internet
Author: User
Tags add implement net return string
Copy CodeThe code is as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Test.aspx.cs" inherits= "Test"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd ">

<title>ajaxpro realizes two level linkage </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<table width= "border=" 0 "align=" center "cellpadding=" 3 "cellspacing=" 1 "bordercolor=" #FFFFFF "style=" Border-collapse:collapse ">
&LT;TR align= "center" >
&LT;TD height= "colspan=" 2 ">
<strong>ajaxpro realizes two level linkage </strong> </td>
</tr>
<tr class= "tdbg" >
&LT;TD width= "30%" >
Provinces </td>
&LT;TD width= "70%" align= "left" >
<asp:dropdownlist id= "ddlstatelist" runat= "Server" datatextfield= "StateName" datavaluefield= "StateId" >
</asp:DropDownList></td>
</tr>
<tr class= "tdbg" >
<td><strong> City </strong></td>
&LT;TD align= "Left" >
<asp:dropdownlist id= "ddlcitylist" runat= "Server" >
</asp:DropDownList></td>
</tr>
</table>

</div>
<script language= "javascript" type= "Text/javascript" defer= "defer" >
function showcity (ID)
{
var res=test.getcitylist (parseint (ID)). value;
var Ddl=document.getelementbyid ("<%=ddlcitylist.uniqueid%>");
ddl.length=0;
if (res)
{
Res is a list<city> collection returned by the server
for (Var i=0;i<res.length;i++)
{
Ddl.options.add (New Option (res[i). Cityname,res[i]. Cityid));
From the above you can see that you can directly call elements in the List<city> collection and their properties
}
}
}
</script>
</form>
</body>
<div Class=cnblogs_highlighter><pre class=brush:csharp>using System;
Using System.Data;
Using System.Configuration;
Using System.Collections;
Using System.Collections.Generic;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;


/**
* Writing Instructions: This article shows how to use the Ajaxpro to interact with the server, and also shows that in JS can directly call the server returned the collection and directly call the class on the server properties
* Author: Zhou Gong
* Date: 2008-1-1
* Starting Address: http://blog.csdn.net/zhoufoxcn/
**/
public partial class Test:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
list<state> statelist = new list<state> (10);
Statelist.add (new state (0, "select City"));//default option
Statelist.add (New state (1, "Beijing"));
Statelist.add (New state (2, "Tianjin"));
Statelist.add (New state (3, "Shanghai"));
Statelist.add (New state (4, "Hubei"));
Statelist.add (New state (5, "Hunan"));
Statelist.add (New state (6, "Shanxi"));
Ddlstatelist.datasource = StateList;
Ddlstatelist.databind ();
ddlstatelist.attributes["onchange"] = "showcity (this.options[selectedindex].value)";
}
AjaxPro.Utility.RegisterTypeForAjax (Test);//Registration (typeof)
}
[Ajaxpro.ajaxmethod]
Public list<city> getcitylist (int stateid)
{
Oh, I am familiar with the city or district
list<city> citylist = new list<city> (12);
Citylist.add (New City (11, "Haidian", 1));
Citylist.add (New City (12, "Chaoyang District", 1));
Citylist.add (New City (13, "Grand Port District", 2));
Citylist.add (New City (14, "Nankai District", 2));
Citylist.add (New City (15, "Putuo", 3));
Citylist.add (New City (16, "Huangpu", 3));
Citylist.add (New City (17, "Huanggang", 4));
Citylist.add (New City (18, "Jinzhou", 4));
Citylist.add (New City (19, "Changsha", 5));
Citylist.add (New City (20, "Yueyang", 5));
Citylist.add (New City (21, "Taiyuan", 6));
Citylist.add (New City (22, "Datong", 6));
list<city> templist = new list<city> ();
for (int i = 0; i < Citylist.count; i++)
{
if (citylist[i). Stateid = = Stateid)
{
Templist.add (Citylist[i]);
}
}
return templist;
}
}
<summary>
Province information
</summary>
public class State
{
private int Stateid;
private string StateName;
<summary>
Province name
</summary>
public string StateName
{
get {return statename;}
set {statename = value;}
}

<summary>
Province number
</summary>
public int Stateid
{
get {return stateid;}
set {Stateid = value;}
}
public State (int Stateid, string statename)
{
This.stateid = Stateid;
This.statename = StateName;
}
}
<summary>
City Information
</summary>
public class city
{
private int Cityid;
private int Stateid;
private string CityName;
<summary>
City Name
</summary>
public string CityName
{
get {return cityname;}
set {CityName = value;}
}

<summary>
Number of provinces in which the city is located
</summary>
public int Stateid
{
get {return stateid;}
set {Stateid = value;}
}

<summary>
City number
</summary>
public int Cityid
{
get {return Cityid;}
set {Cityid = value;}
}

Public City (int Cityid, string cityname, int stateid)
{
This.cityid = Cityid;
This.cityname = CityName;
This.stateid = Stateid;
}

}


</PRE>
</DIV>


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.