asp.net二级联动

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值