如何从 DropDownList 的选定值加载 CheckBoxList?

How do i load CheckBoxList from a Selected Value from DropDownList?

我正在尝试显示数据库的项目(表、行、fk、...)。

我卡在了前几步。我将数据库的名称加载到 DropDownList 中。但是我试图将所选数据库名称中的表加载到 CheckBoxList 中,但它什么也没显示。

这是我的代码

aspx:

<form id="form1" runat="server" method="get">
        <div>
            <asp:DropDownList ID="drpListDBName" runat="server" OnSelectedIndexChanged="drpListDBName_SelectedIndexChanged">
            </asp:DropDownList>
        </div>
         <div>
             <asp:CheckBoxList ID="chkListTable" runat="server">
            </asp:CheckBoxList>
        </div>

aspx.cs:

    public static String dbname = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        String query = "SELECT * FROM SYS.sysdatabases WHERE name != 'master'" +
                        " and name != 'tempdb'" +
                        " and name != 'model'" +
                        " and name != 'msdb'" +
                        " and name != 'distribution'";
        Program.Connect();
        
        
        if(!Page.IsPostBack)
        {
            drpListDBName.DataSource = Program.ExecSqlDataReader(query);
            drpListDBName.DataTextField = "name";
            drpListDBName.DataBind();
        }    
    }

    protected void drpListDBName_SelectedIndexChanged(object sender, EventArgs e)
    {
        dbname = drpListDBName.SelectedValue.Trim();
        String query = "USE " + dbname +
            " SELECT * FROM SYS.tables" +
            " WHERE is_ms_shipped = 0";

        Program.Connect();

        if (chkListTable.Items.Count > 0)
            chkListTable.Items.Clear();
        chkListTable.DataSource = Program.ExecSqlDataReader(query);
        chkListTable.DataTextField = "name";
        chkListTable.DataBind();
        Response.Write(chkListTable);
    }

我还是 asp.net 的新手。提前致谢。

好的,虽然您可以将第二个 selection(table 的列表)发送到复选框列表? (如果你需要 select 多个 tables - 也许是的)。

但是,让我们做得更好。让我们使用两个组合框。第一个,select 数据库,用 tables.

填充第二个组合框

然后你 select 一个 table,并在网格中显示 table。

因此,首先是(可能)使用的连接字符串。我们可以假设您至少为 sql 服务器上的数据库之一构建或设置了一个有效的连接字符串。

所以,项目->“我的项目名称设置”。

你明白了:

因此,我已经制作了一些,但请注意 [...] 按钮,如果您单击该按钮,VS 将为您启动一个连接生成器(非常简单)。您可以测试连接。

所以,我们只是从使用 TEST4 开始。 (请注意,这确实会为您自动将连接字符串放入 Web 配置中)。

好的,让我们放入一个组合框(到 select 数据库)。

然后是我们的复选框列表。

所以,我们有这个标记:

        <h4>Select Database</h4>
        <asp:DropDownList ID="DropDownList1" runat="server" Width="177px"                
            DataTextField="name"
            DataValueFeild="name"     
            AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>

        <br />
        <asp:CheckBoxList ID="CheckBoxList1" runat="server"                
            DataTextField="TABLE_NAME"
            DataValueField="TABLE_NAME" >
        </asp:CheckBoxList>

现在我们有了这段代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();                
    }

    void LoadData()
    {
        Session["MyCon"] = Properties.Settings.Default.TEST4;

        string query = "SELECT * FROM SYS.sysdatabases WHERE name != 'master'" +
                    " and name != 'tempdb'" +
                    " and name != 'model'" +
                    " and name != 'msdb'" +
                    " and name != 'distribution'  ORDER BY Name";

        DropDownList1.DataSource = MyRst(query);
        DropDownList1.DataBind();
    }

    public DataTable MyRst(string strSQL)
    {
        var rst = new DataTable();
        using (SqlConnection conn = new SqlConnection((Session["MyCon"] as string)))
        {
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                conn.Open();
                rst.Load(cmdSQL.ExecuteReader());
            }
        }
        return rst;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // set connecting string from TEST4 to whatever database selected

        string strCon = Session["MyCon"] as string;

        string strDB = DropDownList1.SelectedItem.Value;

        strCon = strCon.Replace("Initial Catalog = test4;", strDB);
        Session["MyCon"] = strCon;

        // now load check box list with all tables

        string strSQL = @"SELECT TABLE_NAME FROM [" + strDB + "].INFORMATION_SCHEMA.TABLES " +
                        "WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME";

        CheckBoxList1.DataSource = MyRst(strSQL);
        CheckBoxList1.DataBind();

    }

所以,现在我们明白了:

所以,是的,您可以设置一个数据table 到复选框列表。但是,我的一些数据库有很多 tables - 复选框太长(很多)。

那么,让我们将代码更改为两个组合框。

但是,我们需要为组合框添加一个“请 select”。

所以,我们现在的标记是这样的:

        <h4>Select Database</h4>
        <asp:DropDownList ID="DropDownList1" runat="server" Width="180px"                
            DataTextField="name"
            DataValueFeild="name"     
            AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>

        <br />
        <h4>Select Table</h4>
        <asp:DropDownList ID="DropDownList2" runat="server" Width="180"                
            DataTextField="TABLE_NAME"
            DataValueField="TABLE_NAME"
            AutoPostBack="true" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"
            >
        </asp:DropDownList>

        <asp:GridView ID="GridView1" runat="server" CssClass="table">

        </asp:GridView>

我们的代码现在是这样的:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadData();                
    }

    void LoadData()
    {
        Session["MyCon"] = Properties.Settings.Default.TEST4;

        string query = "SELECT * FROM SYS.sysdatabases WHERE name != 'master'" +
                    " and name != 'tempdb'" +
                    " and name != 'model'" +
                    " and name != 'msdb'" +
                    " and name != 'distribution'  ORDER BY Name";

        DropDownList1.DataSource = MyRst(query);
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("Please Select", ""));

    }

    public DataTable MyRst(string strSQL)
    {
        var rst = new DataTable();
        using (SqlConnection conn = new SqlConnection((Session["MyCon"] as string)))
        {
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                conn.Open();
                rst.Load(cmdSQL.ExecuteReader());
            }
        }
        return rst;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // set connecting string from TEST4 to whatever database selected
        string strCon = Session["MyCon"] as string;

        string strDB = DropDownList1.SelectedItem.Value;

        strCon = strCon.Replace("Initial Catalog = test4;", strDB);
        Session["MyCon"] = strCon;

        // now load 2nd cbo  box list with all tables

        string strSQL = @"SELECT TABLE_NAME FROM [" + strDB + "].INFORMATION_SCHEMA.TABLES " +
                        "WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME";

        DropDownList2.DataSource = MyRst(strSQL);
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, new ListItem("Please Select", ""));
    }

    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        // send results to the Grid

        string strSQL = "SELECT * FROM [" + DropDownList2.SelectedItem.Value + "]";

        GridView1.DataSource = MyRst(strSQL);
        GridView1.DataBind();
    }

现在我们明白了:

所以,我对给定的数据库使用了有效的连接字符串。我非常假设相同的信息可以并且将会连接到所有这些信息,所以我将 TEST4 数据库名称换成我们 select.