如何停止在DropDownLists中重复数据?我找不到任何有来自sql的非重复值的DropDownlists的好例子?我试着让select语句说明了区别。
这是我正在使用的代码:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = (string)Session["name"];
if (!IsPostBack)
{
DropDownList1.AppendDataBoundItems = true;
DropDownList2.AppendDataBoundItems = true;
DropDownList3.AppendDataBoundItems = true;
DropDownList4.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
String strQuery = "select distinct * from dbo.Vehiclemain WHERE ISENABLED = 'YES'";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "Year";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
con.Close();
con.Open();
DropDownList2.DataSource = cmd.ExecuteReader();
DropDownList2.DataTextField = "Make";
DropDownList2.DataValueField = "ID";
DropDownList2.DataBind();
con.Close();
con.Open();
DropDownList3.DataSource = cmd.ExecuteReader();
DropDownList3.DataTextField = "Model";
DropDownList3.DataValueField = "ID";
DropDownList3.DataBind();
con.Close();
con.Open();
DropDownList4.DataSource = cmd.ExecuteReader();
DropDownList4.DataTextField = "Submodel";
DropDownList4.DataValueField = "ID";
DropDownList4.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
答案 0 :(得分:0)
您正在使用单个查询来填充所有下拉列表。尝试创建4个选择的状态,并将不同的列应用于相应的列。对于ex Make下拉将在查询中具有不同的Make。
答案 1 :(得分:0)
看起来您想要根据第一个下拉菜单更改下拉菜单。 如果是这种情况,那么您也希望将下拉列表恢复为自动提交。 每当有人更改年份时,将根据年份填写制作。 每当有人更改make时,都会根据make填充模型。 每当有人更改模型时,将根据模型填充子模型。 在我们构建信息时,sQL查询将被搭建......
以下代码尚未经过测试。
<asp:DropDownList ID="ddlYearObj" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlObj_SelectedIndexChanged" ></asp:DropDownList>
<asp:DropDownList ID="ddlMakeObj" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlMakeObj_SelectedIndexChanged" Visible="false"></asp:DropDownList>
<asp:DropDownList ID="ddlModelObj" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlModelObj_SelectedIndexChanged" Visible="false"></asp:DropDownList>
<asp:DropDownList ID="ddlSubmodelObj" runat="server" Visible="false"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = (string)Session["name"];
if (!IsPostBack)
{
String strConnString = ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
String strQuery = "select distinct Year from dbo.Vehiclemain WHERE ISENABLED = 'YES'";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlYearObj.DataSource = cmd.ExecuteReader();
ddlYearObj.DataTextField = "Year";
ddlYearObj.DataValueField = "Year";
ddlYearObj.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
//Visible!? -- All invisible until something is chosen
ddlMakeObj.Visible = false;
ddlModelObj.Visible = false;
ddlSubmodelObj.Visible = false;
}
protected void ddlYearObj_SelectedIndexChanged(object sender, EventArgs e)
{
String strConnString = ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
String strQuery = "select distinct Make from dbo.Vehiclemain WHERE ISENABLED = 'YES' AND YEAR = "+ ddlYearObj.Value;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlMakeObj.DataSource = cmd.ExecuteReader();
ddlMakeObj.DataTextField = "Make";
ddlMakeObj.DataValueField = "Make";
ddlMakeObj.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
//Visible!
ddlMakeObj.Visible = true;
ddlModelObj.Visible = false;
ddlSubmodelObj.Visible = false;
}
protected void ddlMakeObj_SelectedIndexChanged(object sender, EventArgs e)
{
String strConnString = ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
String strQuery = "select distinct modelfrom dbo.Vehiclemain WHERE ISENABLED = 'YES' AND YEAR = "+ ddlYearObj.Value;
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlModelObj.DataSource = cmd.ExecuteReader();
ddlModelObj.DataTextField = "Model";
ddlModelObj.DataValueField = "Model";
ddlModelObj.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
//Visible!
ddlMakeObj.Visible = true;
ddlModelObj.Visible = true;
ddlSubmodelObj.Visible = false;
}
protected void ddlModelObj_SelectedIndexChanged(object sender, EventArgs e)
{
String strConnString = ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
String strQuery = "select distinct Submodelfrom dbo.Vehiclemain WHERE ISENABLED = 'YES' AND YEAR = "+ ddlYearObj.Value + " AND Model = '" + ddlModelObj.Value + "'";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
ddlSubmodelObj.DataSource = cmd.ExecuteReader();
ddlSubmodelObj.DataTextField = "Submodel";
ddlSubmodelObj.DataValueField = "ID";
ddlSubmodelObj.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
//Visible!
ddlMakeObj.Visible = true;
ddlModelObj.Visible = false;
ddlSubmodelObj.Visible = true;
}