温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

asp.net三级联动(jquery+json)

发布时间:2020-06-24 11:58:17 来源:网络 阅读:1184 作者:mkyang0627 栏目:编程语言

liandong.aspx:
------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="liandong.aspx.cs" Inherits="WebApplication1.liandong" %>

<!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="script/jquery-1.6.2.js" type="text/javascript"></script>
        <script language="javascript" type="text/javascript">
            $(function() {
                $("#DropDownList1").change(function() {
                    var defaultValue = "<option value='-1'>--请选择--</option>";
                    $("#DropDownList2").html(defaultValue);
                    $("#DropDownList3").html(defaultValue);
                    showMsg("#DropDownList1", "#DropDownList2");
                })

                $("#DropDownList2").change(function() {
                    var defaultValue = "<option value='-1'>--请选择--</option>";
                    $("#DropDownList3").html(defaultValue);
                    showMsg("#DropDownList2", "#DropDownList3");
                })

            })

            function showMsg(sender, receiver) {
                var d1 = $(sender).val();
                $.ajax({
                    type: 'get',
                    dataType: "json",
                    url: 'liandong.ashx',
                    data: 'pid=' + d1,
                    success: function(data) {
                        $.each(data, function(i, o) {
                            var opts = "<option value='" + o.id + "'>" + o.name + "</option>";
                            $(receiver).append(opts);
                        })
                    }
                })
            }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Value="-1">--请选择--</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server">
            <asp:ListItem Value="-1">--请选择--</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList3" runat="server">
            <asp:ListItem Value="-1">--请选择--</asp:ListItem>
        </asp:DropDownList>
    
    </div>
    </form>
</body>
</html>

liandong.aspx.cs
-------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication1
{
    public partial class liandong : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             if(!IsPostBack)
            {
                string sql = "select * from t_ld where pid=0";
                DataTable dt  = DBhelper.query(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    ListItem li = new ListItem(dr["name"].ToString(), dr["id"].ToString());
                    this.DropDownList1.Items.Add(li);
                }
            }
        }
    }
}

liandong.ashx.cs

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebApplication1
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class liandong1 : IHttpHandler
    {

        public void Proce***equest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
            if(!string.IsNullOrEmpty(context.Request.QueryString["pid"]))
            {
                int pid = Convert.ToInt32(context.Request.QueryString["pid"]);
                string sql = "select * from t_ld where pid=" + pid;
                DataTable dt = DBhelper.query(sql);
                string json_str = "";
                json_str += "[";
                foreach (DataRow dr in dt.Rows)
                {
                    json_str += "{\"id\"";
                    json_str += ":";
                    json_str += "\"" + dr["id"].ToString() + "\"";
                    json_str += ",";
                    json_str += "\"name\"";
                    json_str += ":";
                    json_str += "\"" + dr["name"].ToString() + "\"";
                    json_str += "}";
                    json_str += ",";
                }
                json_str = json_str.Substring(0, json_str.Length - 1);
                json_str += "]";
                context.Response.Write(json_str);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI