标签上打印的数据库下拉列表值

时间:2018-06-27 22:26:24

标签: c# sql asp.net database dropdown

我正在使用ASP.net进行下拉菜单。下拉菜单链接到数据库,所有方法均有效。如果在下拉菜单中选择了一个值,则必须填写与数据库中的下拉菜单值匹配的标签。 希望我很清楚,这就是我到目前为止的情况:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.Open();
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Server.MapPath(@"\App_Data") + @"\JipEnJanneke.mdb";


        OleDbCommand cmd1 = new OleDbCommand("Select (Prijs, Jaartal, ISBN) from JipEnJanneke where Titel = @Titel", conn);
        cmd1.Parameters.AddWithValue("@Titel", DropDownList1.SelectedValue.ToString());
        OleDbDataReader rd = cmd1.ExecuteReader();
        while (rd.Read())
        {
            lbl_Prijs.Text = rd["Prijs"].ToString();
            lbl_Jaar.Text = rd["Jaartal"].ToString();
            lbl_Isbn.Text = rd["ISBN"].ToString();
        }
        conn.Close();

不幸的是,这使标签为空。如果我将函数添加到page_load中,则标签会被填充,但由于某些原因,只能通过下拉列表中的第一个值来填充。在这里有一个类似的帖子建议将它替换为selectindex更改,但这对我来说是空的。有人知道吗?

这是我的page_load事件

        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Server.MapPath(@"\App_Data") + @"\JipEnJanneke.mdb";
        //conn.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0; Data Source=" + Server.MapPath(@"\App_Data") + @"\JipEnJanneke.mdb";

        lblConnectionFeedback.Text = "";
        try
        {
            conn.Open();
            lblConnectionFeedback.Text += "Connection is: " + conn.State.ToString();

            // HIER QUERY
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT * FROM Boeken";

            OleDbDataReader rd = cmd.ExecuteReader();

            DropDownList1.DataSource = rd;
            DropDownList1.DataTextField = "Titel";
            DropDownList1.DataValueField = "Titel";
            DropDownList1.DataBind();
            rd.Close();

            conn.Close();

        }
        catch (Exception exc)
        {
            lblConnectionFeedback.Text = exc.Message;
        }
        finally
        {
            conn.Close();
            lblConnectionFeedback.Text += "<br />Connection is: " + conn.State.ToString();
        }

2 个答案:

答案 0 :(得分:1)

在加载事件中,并且dropdown_selectedindex中的代码已更改,请检查页面的IsPostBack。例如:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
if (IsPostBack){
        OleDbConnection conn = new OleDbConnection();
        conn.Open();
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Server.MapPath(@"\App_Data") + @"\JipEnJanneke.mdb";


        OleDbCommand cmd1 = new OleDbCommand("Select (Prijs, Jaartal, ISBN) from JipEnJanneke where Titel = @Titel", conn);
        cmd1.Parameters.AddWithValue("@Titel", DropDownList1.SelectedValue.ToString());
        OleDbDataReader rd = cmd1.ExecuteReader();
        while (rd.Read())
        {
            lbl_Prijs.Text = rd["Prijs"].ToString();
            lbl_Jaar.Text = rd["Jaartal"].ToString();
            lbl_Isbn.Text = rd["ISBN"].ToString();
        }
        conn.Close();
}
}

负载:

if (!IsPostBack){
   OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + Server.MapPath(@"\App_Data") + @"\JipEnJanneke.mdb";
        //conn.ConnectionString = "Provider=Microsoft.JET.OLEDB.4.0; Data Source=" + Server.MapPath(@"\App_Data") + @"\JipEnJanneke.mdb";

        lblConnectionFeedback.Text = "";
        try
        {
            conn.Open();
            lblConnectionFeedback.Text += "Connection is: " + conn.State.ToString();

            // HIER QUERY
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "SELECT * FROM Boeken";

            OleDbDataReader rd = cmd.ExecuteReader();

            DropDownList1.DataSource = rd;
            DropDownList1.DataTextField = "Titel";
            DropDownList1.DataValueField = "Titel";
            DropDownList1.DataBind();
            rd.Close();

            conn.Close();

        }
        catch (Exception exc)
        {
            lblConnectionFeedback.Text = exc.Message;
        }
        finally
        {
            conn.Close();
            lblConnectionFeedback.Text += "<br />Connection is: " + conn.State.ToString();
        }
}

答案 1 :(得分:0)

请检查DropDownList1的AutoPostBack属性是否设置为True。