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;
using System.Data.SqlClient;
public partial class Ex05_01_Ex05_07 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.Items.Add("清华大学");
            DropDownList1.Items.Add("北京大学");
            DropDownList1.Items.Add("浙江大学");
            Label1.Text = "网页第一次加载!";
            Label2.Text = " ";
            try
            {
                DropDownList2_content();
                DropDownList3_content();
            }
            catch
            {
                Response.Write("<script language='javascript'>alert('连接失败!')</script>");
            }
        }
        else
        {
            Label1.Text = "你选择的学校是: " + DropDownList1.SelectedItem.Text;
        }
    }    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList3_content();
        if (ds.Tables["datatable"].Rows.Count != 0)
        {
            Label2.Text = DropDownList2.SelectedItem.Value + "数据库中的数据表" + DropDownList3.SelectedItem.Value;
        }
        else
        {
            Label2.Text = DropDownList2.SelectedItem.Value + "数据库中无用户数据表";
        }
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label2.Text = DropDownList2.SelectedItem.Value + "数据库中的数据表" + DropDownList3.SelectedItem.Value;
    }
}public class Ex05_04_02 : System.Web.UI.Page
{
    public SqlConnection MyConnection;
    public string strSQL;
    public SqlDataAdapter myCommand;
    public DataSet ds = new DataSet();    protected void DropDownList2_content()
    {
        MyConnection = new SqlConnection("server=localhost;usd=sa;pwd=;database=master");
        strSQL = "select name from sysdatabases where dbid>=5 order by dbid desc";
        myCommand = new SqlDataAdapter(strSQL, MyConnection);
        myCommand.Fill(ds, "database");
        DropDownList2.DataSource = ds.Tables["database"].DefaultView;
        DropDownList2.DataValueField = ds.Tables["database"].Columns[0].ColumnName;
        DropDownList2.DataTextField = ds.Tables["database"].Columns[0].ColumnName;
        DropDownList2.DataBind();
    }
    protected void DropDownList3_content()
    {
        myConnection = new SqlConnection("server=localhost;uid=sa;pwd=;database=" + DropDownList2.SelectedItem.Value.ToString() + "");
        strSQL = "select name from sysobjects where xtype='u' order by crdate desc";
        myCommand = new SqlDataAdapter(strSQL, MyConnection);
        myCommand.Fill(ds, "datatable");
        DropDownList3.DataSource = ds.Tables["datatable"].DefaultView;
        DropDownList3.DataValueField = ds.Tables["datatable"].Columns[0].ColumnName;
        DropDownList3.DataTextField = ds.Tables["datatable"].Columns[0].ColumnName;
        DropDownList3.DataBind();
    }
}
错误提示:当前上下文中不存在名称“DropDownList2_content”

解决方案 »

  1.   

    将下面那个DropDownList2_content提出来看看。没有测试
      

  2.   

    else
            {
                Label1.Text = "你选择的学校是: " + DropDownList1.SelectedItem.Text;
            }
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
    ----------
    这里去掉一个"}"
      

  3.   

    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;
    using System.Data.SqlClient;
    public partial class Ex05_01_Ex05_07 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownList1.Items.Add("清华大学");
                DropDownList1.Items.Add("北京大学");
                DropDownList1.Items.Add("浙江大学");
                Label1.Text = "网页第一次加载!";
                Label2.Text = " ";
                try
                {
                    DropDownList2_content();
                    DropDownList3_content();
                }
                catch
                {
                    Response.Write("<script language='javascript'>alert('连接失败!')</script>");
                }
            }
            else
            {
                Label1.Text = "你选择的学校是: " + DropDownList1.SelectedItem.Text;
            }
        }    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList3_content();
            if (ds.Tables["datatable"].Rows.Count != 0)
            {
                Label2.Text = DropDownList2.SelectedItem.Value + "数据库中的数据表" + DropDownList3.SelectedItem.Value;
            }
            else
            {
                Label2.Text = DropDownList2.SelectedItem.Value + "数据库中无用户数据表";
            }
        
        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label2.Text = DropDownList2.SelectedItem.Value + "数据库中的数据表" + DropDownList3.SelectedItem.Value;
        }
    }public class Ex05_04_02 : System.Web.UI.Page
    {
        public SqlConnection MyConnection;
        public string strSQL;
        public SqlDataAdapter myCommand;
        public DataSet ds = new DataSet();    protected void DropDownList2_content()
        {
            MyConnection = new SqlConnection("server=localhost;usd=sa;pwd=;database=master");
            strSQL = "select name from sysdatabases where dbid>=5 order by dbid desc";
            myCommand = new SqlDataAdapter(strSQL, MyConnection);
            myCommand.Fill(ds, "database");
            DropDownList2.DataSource = ds.Tables["database"].DefaultView;
            DropDownList2.DataValueField = ds.Tables["database"].Columns[0].ColumnName;
            DropDownList2.DataTextField = ds.Tables["database"].Columns[0].ColumnName;
            DropDownList2.DataBind();
        }
        protected void DropDownList3_content()
        {
            myConnection = new SqlConnection("server=localhost;uid=sa;pwd=;database=" + DropDownList2.SelectedItem.Value.ToString() + "");
            strSQL = "select name from sysobjects where xtype='u' order by crdate desc";
            myCommand = new SqlDataAdapter(strSQL, MyConnection);
            myCommand.Fill(ds, "datatable");
            DropDownList3.DataSource = ds.Tables["datatable"].DefaultView;
            DropDownList3.DataValueField = ds.Tables["datatable"].Columns[0].ColumnName;
            DropDownList3.DataTextField = ds.Tables["datatable"].Columns[0].ColumnName;
            DropDownList3.DataBind();
        }
    }
    -----------
    这样就可以了
      

  4.   

    TO:OLDMOON(浪者)
    不对,有语法错误。还是不通
      

  5.   

    public partial class Ex05_01_Ex05_07 : System.Web.UI.Page
    这个类中,你调用了另一个类
    public class Ex05_04_02 : System.Web.UI.Page这样恐怕不行吧。你在Ex05_01_Ex05_07 类中是 没有这个方法的
    DropDownList2_content()
      

  6.   

    不知道我的理解对不对,我认为你列出的代码在同一个页面。
    以下认识就是建立在上面的基础之上
    不管错误不错误,我觉得你这样放置代码就是错误的
    public class Ex05_04_02 : System.Web.UI.Page这个看你大部分的方法实际就是为了得到一个数据集然后和dropdownlist绑定,而这些dropdownlist是哪里的了?总体感觉你的代码很混乱