2 个包含数据库中值的下拉列表,需要隐藏第二个下拉列表中第一个的选定值
我有一个包含 2 个下拉列表的页面,两者都具有从 sql 绑定的相同值 当我在框 1 中选择 1 个值时,该值不应在框 2
APSX
</head>
</div>
<div id="div2">
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
</div>
</form>
CS 文件
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillQualCheckBoxList();
}
}
private void FillQualCheckBoxList()
{
string conn_str = ConfigurationManager.ConnectionStrings["CashGameConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(conn_str);
string query = "SELECT * FROM tblStoPokerGames Where ACTIVE = '1'";
SqlCommand comm = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
adp.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Games";
DropDownList1.DataValueField = "Selected";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Please select", ""));
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "Games";
DropDownList2.DataValueField = "Selected";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("Please select", ""));
}
中处于“活动”状态} 我
已经尝试了很多东西,但现在我已经删除了所有不起作用的东西。 我已经在下拉列表中尝试过自动回发 几个脚本
我认为我需要对第二个下拉列表进行数据绑定的
,但我不知道如何干杯 Ace
这是我现在正在尝试的代码:
<body>
<form id="form1" runat="server">
<div style="padding:45px">
<h2>Please select two city destinations for your trip</h2>
<div style="float:left" >
<h4>First City choice</h4>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true"
DataValueField="Selected"
DataTextField="Games"
Height="24px" Width="149px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
<div style="float:left;margin-left:25px">
<h4>Second City choice</h4>
<asp:DropDownList ID="DropDownList2" runat="server"
DataValueField="Selected"
DataTextField="Games"
Height="24px" Width="149px">
</asp:DropDownList>
</div>
</div>
</form>
代码后面
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillQualCheckBoxList();
}
}
private void FillQualCheckBoxList()
{
string query = "SELECT * FROM tblStoPokerGames Where ACTIVE = '1'";
DataTable dt = new DataTable();
dt = MyRst(query);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Please select", ""));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text != "Please select")
{
// clear and re-load 2nd cbo ALWAYS, but skip current selection
DropDownList2.Items.Clear();
DropDownList2.Items.Insert(0, new ListItem("Please select", ""));
foreach (ListItem OneRow in DropDownList1.Items)
{
if (OneRow.Value != DropDownList1.SelectedValue)
{
DropDownList2.Items.Add(OneRow);
}
}
}
}
public DataTable MyRst(string strSQL)
{
string conn_str = ConfigurationManager.ConnectionStrings["CashGameConnectionString"].ConnectionString;
var rst = new DataTable();
using (SqlConnection conn = new SqlConnection(conn_str))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
cmdSQL.Connection.Open();
// fill items table
rst.Load(cmdSQL.ExecuteReader());
}
}
return rst;
}
}
}
I have a page with 2 dropdownlists, both have same values bind from sql
When i select 1 value in box 1 that should not be "active" in box2
APSX
</head>
</div>
<div id="div2">
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
</div>
</form>
CS file
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillQualCheckBoxList();
}
}
private void FillQualCheckBoxList()
{
string conn_str = ConfigurationManager.ConnectionStrings["CashGameConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(conn_str);
string query = "SELECT * FROM tblStoPokerGames Where ACTIVE = '1'";
SqlCommand comm = new SqlCommand(query, conn);
conn.Open();
SqlDataAdapter adp = new SqlDataAdapter(comm);
DataTable dt = new DataTable();
adp.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Games";
DropDownList1.DataValueField = "Selected";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Please select", ""));
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "Games";
DropDownList2.DataValueField = "Selected";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("Please select", ""));
}
}
}
I have tried a lot of stuff but now i have removed all that wasn't working.
I have tried autopostback on the dropdownlist
Several scripts
I think i need to to databinding for second dropdownlist but i dont know how
Cheers
Ace
This is the code i am trying now:
<body>
<form id="form1" runat="server">
<div style="padding:45px">
<h2>Please select two city destinations for your trip</h2>
<div style="float:left" >
<h4>First City choice</h4>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true"
DataValueField="Selected"
DataTextField="Games"
Height="24px" Width="149px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
<div style="float:left;margin-left:25px">
<h4>Second City choice</h4>
<asp:DropDownList ID="DropDownList2" runat="server"
DataValueField="Selected"
DataTextField="Games"
Height="24px" Width="149px">
</asp:DropDownList>
</div>
</div>
</form>
Code behind
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillQualCheckBoxList();
}
}
private void FillQualCheckBoxList()
{
string query = "SELECT * FROM tblStoPokerGames Where ACTIVE = '1'";
DataTable dt = new DataTable();
dt = MyRst(query);
DropDownList1.DataSource = dt;
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("Please select", ""));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text != "Please select")
{
// clear and re-load 2nd cbo ALWAYS, but skip current selection
DropDownList2.Items.Clear();
DropDownList2.Items.Insert(0, new ListItem("Please select", ""));
foreach (ListItem OneRow in DropDownList1.Items)
{
if (OneRow.Value != DropDownList1.SelectedValue)
{
DropDownList2.Items.Add(OneRow);
}
}
}
}
public DataTable MyRst(string strSQL)
{
string conn_str = ConfigurationManager.ConnectionStrings["CashGameConnectionString"].ConnectionString;
var rst = new DataTable();
using (SqlConnection conn = new SqlConnection(conn_str))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
cmdSQL.Connection.Open();
// fill items table
rst.Load(cmdSQL.ExecuteReader());
}
}
return rst;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您可以采用一些技巧来节省大量代码!
第一个问题?如果用户首先选择第二个组合会发生什么?
或者更糟糕的是,他们选择第一个,然后选择第二个,然后返回第一个 CBO 并选择与第二个相同的城市?您可以尝试围绕此“跳舞”,但有一个非常简单的方法吗?
在您选择第一个 CBO 的每种情况下,您都会清除并重新加载第二个组合框的每一行,除了第一行中选定的内容。
这解决了大量的代码问题,同时也解决了大量的 UI 问题。因此,如果您重新选择第一个组合,那么第二个组合将返回到请选择 - 确实是一个小问题 - 甚至可能比尝试与用户打交道更好,然后返回到第一个组合,并选择与第二个组合相同的城市已经在第二个城市选好了一个。 (事实上,我怀疑用户会“做很多”,但是,他们可以,并且上面的简单想法解决了这个问题。
所以,好吧,这是我们的标记:
我们的代码是这样的:
现在怎么有这个:
请注意,我们甚至还没有给或让用户选择第二个 CBO。
如果他们选择一个值,请像这样说:
然后自动回发,我们的简单代码运行:
现在我们得到这个:
可以看到我们的第一个条目不在列表中。
如果我们重新选择第一个 cbo,则第二个被清除,我们就没有任何内容了花哨的代码来处理用户改变主意。
编辑
因此,您的标记将有两个下拉菜单。
因此,您的代码将类似于:
因此请注意我们如何不填充第二个 cbo 框,因为我们强制用户必须选择第一个组合框 (DropDownlist1)。
我为此所做的标记是这样的:
注意我如何将数据文本和数据值设置放入标记中 - 它清理了后面的代码。
请注意,第二个组合框没有自动回发功能,请非常小心 - 它不需要自动回发。
另外,您假设您的 sql 表有两列,“选定”和“游戏” - 对吗?
Ok, you can adopt a few tricks to save a BOATLOAD of code!
The first problem? What happens if the user selects the 2nd combo first?
or worse, they select first, then 2nd, and THEN go back to the first cbo and select the same city as the 2nd? You can try and "dance" around this, but a really simple approach?
IN EVERY case that you select the first cbo, you clear and re-load 2nd combo box with every row except the selected on in the first.
This solves a boatload of code issues, but ALSO solves a boatload of UI issues. So, if you re-select the first one, then the 2nd combo go back to please select - really minor issue - and probably even better then trying to deal with the user then goes back to first combo, and selects the same city as the already selected one in the 2nd city. (in fact, I doubt users would do that "much" but, they could, and the above simple idea fixes this.
So, ok, here is our markup :
And our code is this:
And now how have this:
Note how we not even give or let the user selected the 2nd cbo as of yet.
If they select a value, say like this:
Then with auto-post back, our simple code of this runs:
And now we get this:
You can see our first entry is not in the list.
And if we re-select the first cbo, then the 2nd one is cleared, and we don't have any fancy code to deal with the user changing there mind.
Edit
So, your markup will have the two drop downs.
and your code thus will look similar to this:
So note how we don't fill the 2nd cbo box, since we force and start out with the user MUST select the first combo box (DropDownlist1).
The markup I had for this is this:
Note how I put the data text and data value settings in the markup - it cleans up the code behind.
NOTE VERY careful how the 2nd combo box DOES NOT have a auto post back - it does not need one.
Also, you have it is a assume your sql table has two columns, Selected, and Games - correct?