绑定到数据库并手动添加项目时,DropDownList 无法正常工作
DropDownList is not working properly when bound to database and add item manually
我在ASP.net C# 前端和Oracle 11g 后端创建了一个网页。
网页由2个DropDownList、按钮和GridView组成。在这 2 个 DropdownList 中,DropDownList1 包含通过 ListItem Collection Editor 添加的静态值 "QC Released",其他 DropDownList2 绑定到数据库。
但是,在 DropDwonList2 中,我添加了 "ALL" 通过 ListItem 集合编辑器添加的项目。
现在,当我从 DropDownList1 运行 网页和 select "QC Released" 以及 DropDownList2 中除 "ALL" 之外的任何项目时,我在 GridView 中得到结果。
但是当我从 DropDownList1 select "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;
}
}
else if (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL")
这个条件永远行不通。试试看:
if (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL") {}
else if (DropDownList1.Text == "QC Released") {}
我不确定您如何在查询构建中使用条件。试试这个。
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;
}
}
我在ASP.net C# 前端和Oracle 11g 后端创建了一个网页。 网页由2个DropDownList、按钮和GridView组成。在这 2 个 DropdownList 中,DropDownList1 包含通过 ListItem Collection Editor 添加的静态值 "QC Released",其他 DropDownList2 绑定到数据库。 但是,在 DropDwonList2 中,我添加了 "ALL" 通过 ListItem 集合编辑器添加的项目。
现在,当我从 DropDownList1 运行 网页和 select "QC Released" 以及 DropDownList2 中除 "ALL" 之外的任何项目时,我在 GridView 中得到结果。 但是当我从 DropDownList1 select "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;
}
}
else if (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL")
这个条件永远行不通。试试看:
if (DropDownList1.Text == "QC Released" && DropDownList2.Text == "ALL") {}
else if (DropDownList1.Text == "QC Released") {}
我不确定您如何在查询构建中使用条件。试试这个。
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;
}
}