【问题标题】:DropDownList is not working properly when bound to database and add item manually绑定到数据库并手动添加项目时,DropDownList 无法正常工作
【发布时间】:2015-06-21 10:56:53
【问题描述】:

我在 ASP.net C# 前端和 Oracle 11g 后端创建了一个网页。 网页由 2 个 DropDownList、按钮和 GridView 组成。在这2个DropdownList中,DropDownList1包含通过ListItem Collection Editor添加的静态值“QC Released”,其他DropDownList2绑定到数据库。 但是,在 DropDwonList2 中,我添加了通过 ListItem 集合编辑器添加的“ALL”项。

现在,当我运行网页并从 DropDownList1 中选择“QC Released”并从 DropDownList2 中选择“ALL”以外的任何项目时,我会在 GridView 中得到结果。 但是,当我从 DropDownList1 中选择“QC Released”并从 DropDownList2 中选择“ALL”时,尽管有此特定查询的数据,但在 GridView 中没有获得任何数据。

请参考我的代码如下,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;
using System.Drawing;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
DataSet ds = new DataSet();
OracleConnection con = new OracleConnection("Data Source=10.31.41.103/ORCL;User ID=RL_PET;Password=RL_PET;Unicode=True");

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click1(object sender, EventArgs e)
{
    Label1.Visible = false;
    if (DropDownList1.Text == "QC Released")
    {

        con.Open();
        OracleDataAdapter a = new OracleDataAdapter("SELECT PALLET_NO, DATA_STS, MERGE, PLANT_CD, RELEASE_NO,RELEASE_DATE,QC_STATUS FROM WI_PALLET WHERE MERGE = '" + DropDownList2.Text + "' AND TRANS_TYPE = 'P' AND PLANT_CD IN ('39HV','39HF') AND DATA_STS <>9 AND DATA_STS IS NOT NULL AND PALLET_NO NOT LIKE '7%' ORDER BY PALLET_NO ASC", con);
        a.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            int count = ds.Tables[0].Rows.Count;
            Label1.Text = count.ToString();
            Label1.Visible = true;
            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.Visible = true;
            con.Close();
        }
        else
        {
            Response.Write("<script>alert('No such Record Found')</script>");
            GridView1.Visible = false;
        }

    }
    else if (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL")
    {
        con.Open();
        OracleDataAdapter a = new OracleDataAdapter("SELECT PALLET_NO, DATA_STS, MERGE, MFG_DT, PLANT_CD, RELEASE_NO, RELEASE_DATE, QC_STATUS FROM WI_PALLET WHERE TRANS_TYPE= 'P' AND PLANT_CD IN ('39HV','39HF') AND DATA_STS IS NOT NULL AND PALLET_NO NOT LIKE '7%'  ORDER BY PALLET_NO ASC", con);
        a.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            int count = ds.Tables[0].Rows.Count;
            Label1.Text = count.ToString();
            Label1.Visible = true;
            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.Visible = true;
            con.Close();
        }
        else
        {
            Response.Write("<script>alert('No such Record Found')</script>");
            GridView1.Visible = false;
        }
    }
else
    {
        Response.Write("<script>alert('No such Record Found')</script>");
        GridView1.Visible = false;
    }
} 

【问题讨论】:

    标签: asp.net c#-4.0 drop-down-menu oracle11g


    【解决方案1】:
    else if (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL")
    

    这种情况永远不会奏效。试试看:

    if (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL") {}
    else if (DropDownList1.Text == "QC Released") {}
    

    【讨论】:

    • 需要澄清一些事情......我在 DropDownList1 中有 3 个项目,即“QC 已发布”、“QC 待定”和“生产已预订”......现在这个条件是什么?
    • 您的解决方案有效,但是当我选择“QC Pending”和“ALL”时,如果......这意味着如果 (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL") {} else if (DropDownList1.Text == "QC Released") {} (DropDownList1.Text == "QC Pending" && DropDownList2.Text == "ALL") {}
    【解决方案2】:

    我不确定您如何在查询构建中使用条件。试试这个。

    if (DropDownList2.Text == "ALL")
            {
                switch (DropDownList1.Text)
                {
                    case "QC Released": break;
                    case "QC Pending": break;
                    case "Production Booked": break;
                }
            }
            else
            {
                switch (DropDownList1.Text)
                {
                    case "QC Released": break;
                    case "QC Pending": break;
                    case "Production Booked": break;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      相关资源
      最近更新 更多