<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="下拉框关联.WebForm1" EnableEventValidation="false" %>
<!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 type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(function () {
GameListChange();
});
function GameListChange() {
$("#gameList").change(function () {
InitializeGameService();
});
}
function InitializeGameService() {
$.ajax({
async: false,
type: 'GET',
url: 'ajax/GameService.ashx',
data: 'gameId=' + $("#gameList").val(),
dataType: 'json',
success: function (data) {
removeOption();
if (data.status == false) return;
$.each(data.serviceList, function (index, item) {
appendOption(item.id, item.name);
});
}
});
}
function removeOption() {
$("#gameServiceList option").not(":first").remove();
}
function appendOption(value, text) {
$("#gameServiceList").append("<option value='" + value + "'>" + text + "</option>");
}
function ReloadService(serviceId) {
InitializeGameService();
$("#gameServiceList option[value='" + serviceId + "']").attr("selected", true);
}
function CreateServiceValue() {
$("#hfServiceId").val($("#gameServiceList").val());
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
游戏:
<asp:DropDownList ID="gameList" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="gameServiceList" runat="server">
<asp:ListItem Value="">请选择区服</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btns" runat="server" Text="Button" OnClientClick="javascript:CreateServiceValue();" onclick="Unnamed1_Click"></asp:Button>
<asp:HiddenField ID="hfServiceId" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 下拉框关联
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitializeGameList();
}
}
protected void InitializeGameList()
{
gameList.DataSource = GetList();
gameList.DataTextField = "Name";
gameList.DataValueField = "Id";
gameList.DataBind();
gameList.Items.Insert(0, new ListItem("请选择游戏",""));
}
protected IEnumerable<Game> GetList()
{
for (int i = 1; i < 11; i++)
{
yield return new Game
{
Id = i,
Name = string.Format("游戏{0}", i)
};
}
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "ReloadService(" + hfServiceId.Value + ");", true);
}
}
public struct Game
{
public string Name{get;set;}
public int Id{get;set;}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace 下拉框关联.ajax
{
/// <summary>
/// GameService 的摘要说明
/// </summary>
public class GameService : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
int gameId = 0;
int.TryParse(context.Request["gameId"], out gameId);
var jsonBuilder = new System.Text.StringBuilder();
using (var jsonWriter = new Newtonsoft.Json.JsonTextWriter(new System.IO.StringWriter(jsonBuilder)))
{
DataRow[] serverRows = getAllGameService().Select("serviceId=" + gameId);
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("status");
if (serverRows == null || serverRows.Length == 0)
{
jsonWriter.WriteValue(false);
jsonWriter.WritePropertyName("msg");
jsonWriter.WriteValue("没有相应的游戏区服");
}
else
{
jsonWriter.WriteValue(true);
jsonWriter.WritePropertyName("serviceList");
jsonWriter.WriteStartArray();
foreach(DataRow row in serverRows)
{
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("id");
jsonWriter.WriteValue(row["Id"]);
jsonWriter.WritePropertyName("name");
jsonWriter.WriteValue(row["Name"]);
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
}
jsonWriter.WriteEndObject();
jsonWriter.Flush();
}
context.Response.Write(jsonBuilder);
}
private DataTable getAllGameService()
{
System.Data.DataTable table = new System.Data.DataTable("GameServerce");
table.Columns.Add("Id", typeof(Int32));
table.Columns.Add("ServiceId", typeof(Int32));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(new object[] {1,1,"游戏1区服1" });
table.Rows.Add(new object[] {2,1,"游戏1区服2" });
table.Rows.Add(new object[] {3,2,"游戏2区服1" });
table.Rows.Add(new object[] {4,2,"游戏2区服2" });
table.Rows.Add(new object[] {5,2,"游戏2区服3" });
table.Rows.Add(new object[] {6,3,"游戏3区服1" });
table.Rows.Add(new object[] {7,3,"游戏3区服2" });
table.Rows.Add(new object[] {8,4,"游戏4区服1" });
table.Rows.Add(new object[] {9,5,"游戏5区服1" });
table.Rows.Add(new object[] {10,6,"游戏6区服1" });
table.Rows.Add(new object[] {11,7,"游戏7区服1" });
return table;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}