CODE
using System;
using System.Data;
using System.Configuration;
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;
using System.Text;
using System.Data.SqlClient;
using System.Security;
using System.Security.Principal;
using System.Security.Cryptography;public interface IType
{
/// <summary>
/// 获取大类
/// </summary>
/// <returns><returns>
    SqlDataReader GetDtype();    /// <summary>
    /// 获取小类
    /// </summary>
    /// <returns><returns>
    SqlDataReader GetDty();
 }public class Type : IType
{
    private static readonly string GETDTYPE = "SELECT * FROM Dtype";
    private static readonly string GETDTY = "SELECT * FROM Dty,Dtype WHERE Dty.TID=Dtype.TypeId";    public SqlDataReader GetDtype()
    {
        ///创建链接
        SqlConnection myConnection = new SqlConnection(
            ConfigurationManager.ConnectionStrings["AConn"].ConnectionString);
        ///创建Command
        SqlCommand myCommand = new SqlCommand(GETDTYPE, myConnection);        ///定义DataReader
        SqlDataReader dr = null;
        try
        {
            ///打开链接
            myConnection.Open();
            ///读取数据
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (SqlException ex)
        {
            ///抛出异常
            throw new Exception(ex.Message, ex);
        }
        ///返回DataReader
        return dr;
    }
    public SqlDataReader GetDty()
    {
        ///创建链接
        SqlConnection myConnection = new SqlConnection(
            ConfigurationManager.ConnectionStrings["AConn"].ConnectionString);
        ///创建Command
        SqlCommand myCommand = new SqlCommand(GETDTY, myConnection);        ///定义DataReader
        SqlDataReader dr = null;
        try
        {
            ///打开链接
            myConnection.Open();
            ///读取数据
            dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (SqlException ex)
        {
            ///抛出异常
            throw new Exception(ex.Message, ex);
        }
        ///返回DataReader
        return dr;
    }}
//////////////////////////////////////////////////////////////////////////////////////////////////
前台:
<%@ 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>
 </head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server"><asp:ListItem Text="请选择..." Value=""></asp:ListItem>
</asp:DropDownList>
    </div>
    </form>
</body>
</html>
/////////////////////////////////////////////////////////////////////////////////////////////
后台:
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindDtypeData();
        
        }
    }
    private void BindDtypeData()
    {
        ///获取大类
        IType type = new Type();
        SqlDataReader dr = type.GetDtype();        ///设置DropDownList的数据源,并绑定数据
        DropDownList1.DataSource = dr;
        DropDownList1.DataTextField = "TypeName";
        DropDownList1.DataValueField = "TypeID";
        DropDownList1.SelectedIndex = this.DropDownList1.SelectedValue; 
        DropDownList1.DataBind();        ///关闭数据读取器
        dr.Close();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ///获取小类
        IType type = new Type();
        SqlDataReader ds = type.GetDty();        ///设置DropDownList的数据源,并绑定数据
        DropDownList2.DataSource = ds;
        DropDownList2.DataTextField = "TyName";
        DropDownList2.DataValueField = "TyID";
        DropDownList2.DataBind();        ///关闭数据读取器
        ds.Close();
    }
}

解决方案 »

  1.   

    这种方法就可以实现 可我想用上面的方法
    using System;
    using System.Data;
    using System.Configuration;
    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;
    using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page 
    {
       SqlConnection conn = new SqlConnection("server=localhost;database=Archives;uid=sa;pwd=ak4740;");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                P();
            }
        }public void P()
        {
            conn.Open();
            string sql1 = "Select * From DType";
            SqlDataAdapter sda = new SqlDataAdapter(sql1, conn);
            DataSet ds1 = new DataSet();
            sda.Fill(ds1, "P");
            DataTable dt = ds1.Tables["P"];
            DropDownList1.Items.Add(new ListItem("请选择...", ""));
            foreach (DataRow dr in dt.Rows)
            {
                DropDownList1.Items.Add(new ListItem(dr["TypeName"].ToString(), dr["TypeID"].ToString()));
            }
            conn.Close();
        }
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            conn.Open();
            string sql2 = "Select * From Dty Where TID=" + DropDownList1.SelectedValue + "";
            SqlCommand comm = new SqlCommand(sql2, conn);
            SqlDataReader sdr = comm.ExecuteReader();
            DropDownList2.Items.Clear();
            while (sdr.Read())
            {
                DropDownList2.Items.Add(new ListItem(sdr["TyName"].ToString(), sdr["TyID"].ToString()));
            }
            conn.Close();
        }
    }