如何为标签编写数据库驱动的下拉选择?

时间:2015-08-12 15:55:11

标签: c# asp.net

另一个ASP.NET C#noobie问题......

以下代码隐藏从数据库填充下拉列表。表中有三列(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>";
        }

    }
}

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

任何帮助表示感谢。

感谢。

1 个答案:

答案 0 :(得分:0)

编辑*** 好的,然后在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....

这样的东西,但也许写得更好。