如何将数据库驱动的下拉选择写入标签

本文关键字:选择 标签 数据库 | 更新日期: 2023-09-27 18:07:37

另一个ASP。. NET c# noobie question…

下面的代码从数据库填充一个下拉列表。表中有三列(ID, ItemType &;BinType)。我需要能够为用户选择的行返回正确的BinType:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
  // Global variable for SqlConnection
  OleDbConnection con = new OleDbConnection();
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!this.IsPostBack)
    {
      // specifying sqlconnection string
      con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString_GRPAS_dev"].ConnectionString;
      {
        // Select rows from database where the ItemType field isn't empty. Sort them alphabetically by ItemType
        using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM NF_WhatWasteWhere WHERE ItemType <>'' Order By ItemType"))
        {
          //Open the connection and populate the dropdown list with ID and Itemtype
          cmd.CommandType = CommandType.Text;
          cmd.Connection = con;
          con.Open();
          ItemType1.DataSource = cmd.ExecuteReader();
          ItemType1.DataTextField = "ItemType";
          ItemType1.DataValueField = "ID";
          ItemType1.DataBind();
          con.Close();
        }
      }
      // Add a non selectable "Select Item" row at the top of the dropdown list
      ItemType1.Items.Insert(0, new ListItem("--Select Item--", "0"));

    }
  }
    protected void ItemType1_SelectedIndexChanged(object sender, EventArgs e)
    {
      //
      // *** Stuff needs to go here in order to continue with the following conditional statement ***
      //
        if (ItemType1.SelectedValue == "Green")
        {
          BinResultTest.Text = "<div class='greenBin results'><div class='arrow'></div><p>" + ItemType1.SelectedItem + " should be disposed of in a <strong>green bin</strong>.</p></div>";
        }
        else if (ItemType1.SelectedValue == "Black")
        {
          BinResultTest.Text = "<div class='blackBin results'><div class='arrow'></div><p>" + ItemType1.SelectedItem + " should be disposed of in a <strong>black bin</strong>.</p></div>";
        }
        else
        {
          BinResultTest.Text = "<div class='noBin results'><div class='arrow'></div><p>" + ItemType1.SelectedItem + " should <strong>NOT</strong> be disposed of in a green or black bin.</p></div>";
        }
    }
}

我需要做什么才能使条件语句工作?我假定我需要运行另一个数据库查询—类似于以下内容:

SELECT BinType FROM NF_WhatWasteWhere WHERE ID=" + ItemType1.DataValueField

但是,我不确定如何编写此代码以建立连接并返回结果。

感谢您的帮助。

谢谢。

如何将数据库驱动的下拉选择写入标签

编辑***在protected void ItemType1_SelectedIndexChanged(object sender, EventArgs e)

你可以做类似的事情,你做了你的第一个查询,但使用OdbcDataReader

    string binValue;
    int idHolder = ItemType1.SelectedValue;
        con.ConnectionString =System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString_GRPAS_dev"].ConnectionString;
              {
               using (OleDbCommand cmd = new OleDbCommand("SELECT BinType FROM NF_WhatWasteWhere WHERE ID = @Id;"))
                {
                  cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("@Id",idHolder);
                  cmd.Connection = con;
                  con.Open();
        OleDbDataReader reader = cmd.ExecuteReader();
        if(reader.Read())
        {
          if(!DBNull.Value.Equals(reader["BinType"]))
            {
             binValue = Convert.ToString(reader["BinType"]);
             }
       }
     con.Close();
//Then all your conditionals based off of binValue....

类似这样,但可能写得更好一点。