jquery + ashx DropDownList 二级

 

前台:

Code
   
     
<% @ Page Language = " C# " AutoEventWireup = " true " CodeFile = " Default.aspx.cs " Inherits = " _Default " %>

<! 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 " >
< head runat = " server " >
< title ></ title >

< script src = " javascript/jquery-1.3.2.min.js " type = " text/javascript " ></ script >

< script type = " text/javascript " >
<!--
$(function() {
$(
" #ddlMenuOne " ).append(
" <option value='-1' selected='selected'>请选择...</option> " );
ddlMenuTwoInit();

$.getJSON(
" DDLMenu.ashx " ,
{ menuOneId: $(
" #ddlMenuOne " ).val() },
function(json) {
$.each(json, function(i) {
$(
" #ddlMenuOne " ).append(
" <option value=' " + json[i].Id + " '> "
+ json[i].MenuOneName + " </option> "
);
});
});

$(
" #ddlMenuOne " ).change(function( event ) {
if ($( " #ddlMenuOne " ).val() !== " -1 " ) {
$.getJSON(
" DDLMenu.ashx " ,
{ menuOneId: $(
" #ddlMenuOne " ).val() },
function(json) {
ddlMenuTwoInit();
$.each(json, function(i) {
$(
" #ddlMenuTwo " ).append(
" <option value=' " + json[i].Id + " '> "
+ json[i].MenuTwoName + " </option> "
);
});
});
}
else {
ddlMenuTwoInit();
}
});
});

function ddlMenuTwoInit() {
$(
" #ddlMenuTwo " ).html( "" ).append(
" <option value='-1' selected='selected'>请选择...</option> " );
}
// -->
</ script >

</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:DropDownList ID = " ddlMenuOne " runat = " server " >
</ asp:DropDownList >
< br />
< asp:DropDownList ID = " ddlMenuTwo " runat = " server " >
</ asp:DropDownList >
</ div >
< ul id = " list " >
</ ul >
</ form >
</ body >
</ html >

 

 

后台 ashx 

 

Code
   
     
<% @ WebHandler Language = " C# " Class = " DDLMenu " %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.ServiceModel.Dispatcher;

public class DDLMenu : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{

context.Response.ContentType
= " text/plain " ;

int menuOneId = Convert.ToInt32(context.Request.QueryString[ " menuOneId " ]);

JsonQueryStringConverter jq
= new JsonQueryStringConverter();

// JavaScriptSerializer serializer = new JavaScriptSerializer();
// string data = serializer.Serialize(list);

string data = "" ;

if (menuOneId == - 1 )
{
List
< MenuOne > list = new List < MenuOne > ();

list.Add(
new MenuOne { Id = 1 , MenuOneName = " 城市 " });

list.Add(
new MenuOne { Id = 2 , MenuOneName = " 年份 " });

list.Add(
new MenuOne { Id = 3 , MenuOneName = " 蔬菜 " });

data
= jq.ConvertValueToString(list, typeof (List < MenuOne > ));
}
else
{
List
< MenuTwo > list = new List < MenuTwo > ();

list.Add(
new MenuTwo { Id = 1 , MenuOneId = 1 , MenuTwoName = " 淮安 " });

list.Add(
new MenuTwo { Id = 2 , MenuOneId = 1 , MenuTwoName = " 南京 " });

list.Add(
new MenuTwo { Id = 3 , MenuOneId = 1 , MenuTwoName = " 盐城 " });

list.Add(
new MenuTwo { Id = 4 , MenuOneId = 1 , MenuTwoName = " 苏州 " });

list.Add(
new MenuTwo { Id = 5 , MenuOneId = 1 , MenuTwoName = " 无锡 " });

list.Add(
new MenuTwo { Id = 6 , MenuOneId = 1 , MenuTwoName = " 镇江 " });

list.Add(
new MenuTwo { Id = 7 , MenuOneId = 1 , MenuTwoName = " 泰州 " });

list.Add(
new MenuTwo { Id = 8 , MenuOneId = 2 , MenuTwoName = " 2000 " });

list.Add(
new MenuTwo { Id = 9 , MenuOneId = 2 , MenuTwoName = " 2001 " });

list.Add(
new MenuTwo { Id = 10 , MenuOneId = 2 , MenuTwoName = " 2002 " });

list.Add(
new MenuTwo { Id = 11 , MenuOneId = 2 , MenuTwoName = " 2003 " });

list.Add(
new MenuTwo { Id = 12 , MenuOneId = 2 , MenuTwoName = " 2004 " });

list.Add(
new MenuTwo { Id = 13 , MenuOneId = 2 , MenuTwoName = " 2005 " });

list.Add(
new MenuTwo { Id = 14 , MenuOneId = 2 , MenuTwoName = " 2006 " });

list.Add(
new MenuTwo { Id = 15 , MenuOneId = 3 , MenuTwoName = " 黄瓜 " });

list.Add(
new MenuTwo { Id = 16 , MenuOneId = 3 , MenuTwoName = " 萝卜 " });

list.Add(
new MenuTwo { Id = 17 , MenuOneId = 3 , MenuTwoName = " 白菜 " });

list.Add(
new MenuTwo { Id = 18 , MenuOneId = 3 , MenuTwoName = " 猪肉 " });

list.Add(
new MenuTwo { Id = 19 , MenuOneId = 3 , MenuTwoName = " 鸡腿 " });

list.Add(
new MenuTwo { Id = 20 , MenuOneId = 3 , MenuTwoName = " 闫腾 " });

list.Add(
new MenuTwo { Id = 21 , MenuOneId = 3 , MenuTwoName = " 黄鱼 " });

List
< MenuTwo > newList = new List < MenuTwo > ();
int n = list.Count;
for ( int i = 0 ; i < n; i ++ )
{
if (list[i].MenuOneId == menuOneId)
newList.Add(list[i]);
}

data
= jq.ConvertValueToString(newList, typeof (List < MenuTwo > ));

}

context.Response.Write(data);
}

public bool IsReusable
{
get
{
return false ;
}
}

}

 

 

哈哈!~

你可能感兴趣的:(jquery)