ajax+asp.net2.0 sql2005三级联下拉框

简介: 一sql2005建立数数据库:create database Schoolgouse Schoolgocreate table SchoolInfo(schoolId int IDENTITY(1,1)PRIMARY KEY,schoolName nvarchar(50))gocreate table DepartInfo(departId int IDENTITY(1,1)PRIMARY K
一sql2005建立数数据库:
create database School
go

use School
go

create table SchoolInfo
(
schoolId int IDENTITY(1,1)PRIMARY KEY,
schoolName nvarchar(50)
)
go

create table DepartInfo
(
departId int IDENTITY(1,1)PRIMARY KEY,
schoolId int,
departName nvarchar(50)
)
go

create table personinfo
(
personId int IDENTITY(1,1) PRIMARY KEY,
departId int,
personName nvarchar(50)
)
go

--外键1  //先把表建完之后,再建外键
ALTER TABLE DepartInfo ADD CONSTRAINT SchoolInfo_DepartInfo_rf FOREIGN KEY
(
    schoolId
)
REFERENCES SchoolInfo(schoolId)
GO

ALTER TABLE personinfo ADD CONSTRAINT DepartInfo_personinfo_rf FOREIGN KEY
(
    departId
)
REFERENCES DepartInfo(departId)
GO

insert into SchoolInfo(schoolName)
VALUES('涂家小学')

insert into SchoolInfo(schoolName)
VALUES('江口小学')

insert into SchoolInfo(schoolName)
VALUES('下炮小学')

select * from SchoolInfo

insert into DepartInfo(SchoolId,departName)
values(1,'教务处')
insert into DepartInfo(SchoolId,departName)
values(1,'校办公室')
insert into DepartInfo(SchoolId,departName)
values(1,'学生处')
insert into DepartInfo(SchoolId,departName)
values(2,'党务处')
insert into DepartInfo(SchoolId,departName)
values(2,'校委办公室')
insert into DepartInfo(SchoolId,departName)
values(2,'团支处')
insert into DepartInfo(SchoolId,departName)
values(3,'电教处')
insert into DepartInfo(SchoolId,departName)
values(3,'校办公室')
insert into DepartInfo(SchoolId,departName)
values(3,'外办处')
GO

select * from DepartInfo

insert into personinfo(departId,personName)
values(1,'涂聚文')
insert into personinfo(departId,personName)
values(1,'赵金红')
insert into personinfo(departId,personName)
values(1,'涂斯博')
insert into personinfo(departId,personName)
values(2,'涂思懿')
insert into personinfo(departId,personName)
values(2,'涂年生')
insert into personinfo(departId,personName)
values(2,'赵刚')
insert into personinfo(departId,personName)
values(3,'赵思博')
insert into personinfo(departId,personName)
values(3,'高杰')
insert into personinfo(departId,personName)
values(3,'何处')
insert into personinfo(departId,personName)
values(4,'刘杰')
insert into personinfo(departId,personName)
values(4,'胡新胜')
insert into personinfo(departId,personName)
values(4,'黄瑞斌')
insert into personinfo(departId,personName)
values(5,'黄岗')
insert into personinfo(departId,personName)
values(5,'钟艳红')
insert into personinfo(departId,personName)
values(5,'毛湖南')
insert into personinfo(departId,personName)
values(6,'涂惟')
insert into personinfo(departId,personName)
values(6,'毛婷')
insert into personinfo(departId,personName)
values(6,'曾海波')
insert into personinfo(departId,personName)
values(7,'李鹏')
insert into personinfo(departId,personName)
values(7,'赵三')
insert into personinfo(departId,personName)
values(7,'李四')
insert into personinfo(departId,personName)
values(8,'王五')
insert into personinfo(departId,personName)
values(8,'毛六')
insert into personinfo(departId,personName)
values(8,'陈七')
insert into personinfo(departId,personName)
values(9,'张八')
insert into personinfo(departId,personName)
values(9,'艾九')
insert into personinfo(departId,personName)
values(9,'刘十')


select * from personinfo

select personid,personname from personinfo where departid=4

二CascadingDropDown_DB.aspx代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CascadingDropDown_DB.aspx.cs" Inherits="CascadingDropDown_DB" 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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
   
    </div>
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown1" runat="server"
        TargetControlID="ddlSchool"
        Category="schoolname"
         ServicePath="PersonDBService.asmx"
         ServiceMethod="GetSchool"
          PromptText="请选择学校"
          LoadingText="正在加载学校" >
        </ajaxToolkit:CascadingDropDown>
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown2" runat="server" TargetControlID="ddlDepart"
         Category="departname"
         ServicePath="PersonDBService.asmx"
         ServiceMethod="GetDepartsForSchool"
          PromptText="请选择部门"
          LoadingText="正在加载部门"
         ParentControlID="ddlSchool" >
        </ajaxToolkit:CascadingDropDown>
        <ajaxToolkit:CascadingDropDown ID="CascadingDropDown3" runat="server" TargetControlID="ddlPerson"
         Category="personname"
         ServicePath="PersonDBService.asmx"
         ServiceMethod="GetPersonsForDepart"
          PromptText="请选择人员"
          LoadingText="正在加载人员"
          ParentControlID="ddlDepart">
        </ajaxToolkit:CascadingDropDown>
        &nbsp; &nbsp;
        <table style="width: 572px; height: 168px;">
            <tr>
                <td style="width: 200px">
                </td>
                <td>
                   <strong>选择数据库中的人员</strong> </td>
            </tr>
            <tr>
                <td style="width: 200px">
                    1.选择学校:</td>
                <td>
        <asp:DropDownList ID="ddlSchool" runat="server" Width="234px">
        </asp:DropDownList></td>
            </tr>
            <tr>
                <td style="width: 200px">
                    2.选择部门:</td>
                <td>
        <asp:DropDownList ID="ddlDepart" runat="server" Width="232px">
        </asp:DropDownList></td>
            </tr>
            <tr>
                <td style="width: 200px">
                    3.选择人员:</td>
                <td>
        <asp:DropDownList ID="ddlPerson" runat="server" Width="230px" OnSelectedIndexChanged="ddlPerson_SelectedIndexChanged" AutoPostBack="True">
        </asp:DropDownList></td>
            </tr>
            <tr>
                <td colspan="2">
                  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" Width="341px"></asp:Label>
                        </ContentTemplate>
                     <Triggers>
                          <asp:AsyncPostBackTrigger ControlID="ddlPerson" EventName="SelectedIndexChanged" />
                     </Triggers>
                    </asp:UpdatePanel>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

三CascadingDropDown_DB.aspx.cs 代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;

public partial class CascadingDropDown_DB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ddlPerson_SelectedIndexChanged(object sender, EventArgs e)
    {
        //选择的内容-注意此处不是使用的selectvalue
        string person = ddlSchool.SelectedItem.Text + "学校" + ddlDepart.SelectedItem.Text + "部门的" + ddlPerson.SelectedItem.Text;
        //动态显示
        Label1.Text = "您选择的是-" + person;
    }
}

四PersonDBService.asmx 服务代码:

<%@ WebService Language="C#" CodeBehind="~/App_Code/PersonDBService.cs" Class="PersonDBService" %>


五PersonDBService.cs 代码:

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;

using AjaxControlToolkit;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;

/// <summary>
/// PersonDBService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class PersonDBService : System.Web.Services.WebService {

    public PersonDBService () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }
    /// <summary>
    /// 获取学校信息
    /// </summary>
    /// <param name="knownCategoryValues">父级条件</param>
    /// <param name="category">目录</param>
    /// <returns>返回数据</returns>
    [WebMethod]
    public CascadingDropDownNameValue[] GetSchool( string knownCategoryValues, string category)
    {
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        //返回学校表信息
        DataTable schools = getSchoolTable();
        //遍历表中的行
        foreach (DataRow dr in schools.Rows)
        {
            string school = (string)dr["schoolname"];
            int schoolId = (int)dr["schoolid"];
            //表数据添加到级联菜单中
            values.Add(new CascadingDropDownNameValue(
              school, schoolId.ToString()));
        }
        return values.ToArray();
    }
    //获取部门信息
    [WebMethod]
    public CascadingDropDownNameValue[] GetDepartsForSchool(string knownCategoryValues,string category)
    {
        //找到指定条件的信息集合
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        int schoolId;
        //找出指定值的ID。
        if (!kv.ContainsKey("schoolname") ||
            !Int32.TryParse(kv["schoolname"], out schoolId))
        {
            return null;
        }
        //根据学校id返回部门表
        DataTable departs = getDepartTable(schoolId);
        List<CascadingDropDownNameValue> values =  new List<CascadingDropDownNameValue>();
        //遍历部门表
        foreach (DataRow dr in departs.Rows)
        {
            //添加部门信息到级联菜单
            values.Add(new CascadingDropDownNameValue(
              (string)dr["departname"], dr["departid"].ToString()));
        }
        return values.ToArray();
    }
    //获取人员信息
    [WebMethod]
    public CascadingDropDownNameValue[] GetPersonsForDepart( string knownCategoryValues, string category)
    {
        //找到指定条件的集合
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString( knownCategoryValues);
        int departId;
        //获取指定的ID
        if (!kv.ContainsKey("departname") ||
            !Int32.TryParse(kv["departname"], out departId))
        {
            return null;
        }
        //根据部门ID返回人员表
        DataTable persons = getPersonTable(departId);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        //遍历人员表
        foreach (DataRow dr in persons.Rows)
        {
            //添加人员信息到级联菜单
            values.Add(new CascadingDropDownNameValue(
              (string)dr["personname"], dr["personid"].ToString()));
        }
        return values.ToArray();
    }
    /// <summary>
    /// 获取学校表数据
    /// </summary>
    public DataTable getSchoolTable()
    {
        //创建数据库连接
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SchoolConnectionString"].ToString());
        SqlDataAdapter da = new SqlDataAdapter("select schoolid,schoolname  from schoolinfo", conn);
        DataSet ds = new DataSet();
        //填充数据内容
        da.Fill(ds);
        return ds.Tables[0];
    }
    /// <summary>
    /// 获取指定学校ID的部门信息
    /// </summary>
    public DataTable getDepartTable(int schoolid)
    {
        //创建数据库连接
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SchoolConnectionString"].ToString());
        SqlDataAdapter da = new SqlDataAdapter("select departid,departname from departinfo where schoolid=" + schoolid.ToString(), conn);
        DataSet ds = new DataSet();
        //填充数据内容
        da.Fill(ds);
        return ds.Tables[0];
    }
    /// <summary>
    /// 获取指定部门ID的人员信息
    /// </summary>
    public DataTable getPersonTable(int departid)
    {
        //创建数据库连接
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SchoolConnectionString"].ToString());
        SqlDataAdapter da = new SqlDataAdapter("select personid,personname from personinfo where departid="+ departid.ToString(), conn);
        DataSet ds = new DataSet();
        //填充数据内容
        da.Fill(ds);
        return ds.Tables[0];
    }
}

七:Web.config 数据库连接代码:
    <connectionStrings>
        <add name="SchoolConnectionString" connectionString="Data Source=GEOVI-5E9530747/SQLEXPRESS;Initial Catalog=School;Integrated Security=True"
   providerName="System.Data.SqlClient" />
    </connectionStrings>

 
目录
相关文章
|
2月前
|
SQL 开发框架 .NET
ASP.NET连接SQL数据库:详细步骤与最佳实践指南ali01n.xinmi1009fan.com
随着Web开发技术的不断进步,ASP.NET已成为一种非常流行的Web应用程序开发框架。在ASP.NET项目中,我们经常需要与数据库进行交互,特别是SQL数据库。本文将详细介绍如何在ASP.NET项目中连接SQL数据库,并提供最佳实践指南以确保开发过程的稳定性和效率。一、准备工作在开始之前,请确保您
282 3
|
2月前
|
SQL 开发框架 .NET
ASP连接SQL数据库:从基础到实践
随着互联网技术的快速发展,数据库与应用程序之间的连接成为了软件开发中的一项关键技术。ASP(ActiveServerPages)是一种在服务器端执行的脚本环境,它能够生成动态的网页内容。而SQL数据库则是一种关系型数据库管理系统,广泛应用于各类网站和应用程序的数据存储和管理。本文将详细介绍如何使用A
93 3
|
2月前
|
SQL 开发框架 .NET
ASP.NET连接SQL数据库:实现过程与关键细节解析an3.021-6232.com
随着互联网技术的快速发展,ASP.NET作为一种广泛使用的服务器端开发技术,其与数据库的交互操作成为了应用开发中的重要环节。本文将详细介绍在ASP.NET中如何连接SQL数据库,包括连接的基本概念、实现步骤、关键代码示例以及常见问题的解决方案。由于篇幅限制,本文不能保证达到完整的2000字,但会确保
|
7月前
|
JSON 前端开发 JavaScript
jQuery ajax读取本地json文件 三级联动下拉框
jQuery ajax读取本地json文件 三级联动下拉框
|
7月前
|
SQL 开发框架 .NET
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
89 0
|
开发框架 前端开发 .NET
用ajax和asp.net实现智能搜索功能
用ajax和asp.net实现智能搜索功能
82 0
|
开发框架 JavaScript .NET
ASP.NET Core+Element+SQL Server开发校园图书管理系统(三)(下)
ASP.NET Core+Element+SQL Server开发校园图书管理系统(三)(下)
83 0
ASP.NET Core+Element+SQL Server开发校园图书管理系统(三)(下)
|
SQL 开发框架 前端开发
ASP.NET Core+Element+SQL Server开发校园图书管理系统(完)
ASP.NET Core+Element+SQL Server开发校园图书管理系统(完)
155 0
|
开发框架 JavaScript .NET
ASP.NET Core+Element+SQL Server开发校园图书管理系统(四)(下)
ASP.NET Core+Element+SQL Server开发校园图书管理系统(四)(下)
116 0
|
开发框架 前端开发 JavaScript
ASP.NET Core+Element+SQL Server开发校园图书管理系统(四)(上)
ASP.NET Core+Element+SQL Server开发校园图书管理系统(四)(上)
122 0