asp.net两个相关的下拉框如何在第二个下拉框中不显示第一个下拉框选取的值
如下面的代码,两个下拉框的内容是一样的,但是我希望第一个选中a后,第二个下拉框就不显示a,同样的,如果我先选择第二个的a,那么第一个下拉框就不显示a。<asp:dropd...
如下面的代码,两个下拉框的内容是一样的,但是我希望第一个选中a后,第二个下拉框就不显示a,同样的,如果我先选择第二个的a,那么第一个下拉框就不显示a。
<asp:dropdownlist ID="Dropdownlist1" runat="server">
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:dropdownlist>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList> 展开
<asp:dropdownlist ID="Dropdownlist1" runat="server">
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:dropdownlist>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList> 展开
4个回答
展开全部
可以使用ajax处理
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dropdowmlist.aspx.cs" Inherits="Dropdowmlist" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:updatepanel ID="Updatepanel1" runat="server">
<ContentTemplate>
<asp:dropdownlist ID="Dropdownlist1" runat="server" onselectedindexchanged="Dropdownlist1_SelectedIndexChanged"
AutoPostBack="true" style="margin-bottom: 1px" >
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:dropdownlist>
<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Dropdownlist1" />
<asp:AsyncPostBackTrigger ControlID="Dropdownlist2" />
</Triggers>
</asp:updatepanel>
</div>
</form>
</body>
</html>
后台源码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Dropdowmlist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.Dropdownlist1.SelectedItem.Value.ToString() == "a")
this.DropDownList2.Items[0].Attributes.Add("style","display:none");
else if(this.Dropdownlist1.SelectedItem.Value.ToString() == "b")
this.DropDownList2.Items[1].Attributes.Add("style", "display:none");
else if (this.Dropdownlist1.SelectedItem.Value.ToString() == "c")
this.DropDownList2.Items[2].Attributes.Add("style", "display:none");
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownList2.SelectedItem.Value.ToString()=="a")
this.Dropdownlist1.Items[1].Attributes.Add("style", "display:none");
else if (this.DropDownList2.SelectedItem.Value.ToString() == "b")
this.Dropdownlist1.Items[2].Attributes.Add("style", "display:none");
else if (this.DropDownList2.SelectedItem.Value.ToString() == "c")
this.Dropdownlist1.Items[3].Attributes.Add("style", "display:none");
}
}
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dropdowmlist.aspx.cs" Inherits="Dropdowmlist" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:updatepanel ID="Updatepanel1" runat="server">
<ContentTemplate>
<asp:dropdownlist ID="Dropdownlist1" runat="server" onselectedindexchanged="Dropdownlist1_SelectedIndexChanged"
AutoPostBack="true" style="margin-bottom: 1px" >
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:dropdownlist>
<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Dropdownlist1" />
<asp:AsyncPostBackTrigger ControlID="Dropdownlist2" />
</Triggers>
</asp:updatepanel>
</div>
</form>
</body>
</html>
后台源码:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Dropdowmlist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.Dropdownlist1.SelectedItem.Value.ToString() == "a")
this.DropDownList2.Items[0].Attributes.Add("style","display:none");
else if(this.Dropdownlist1.SelectedItem.Value.ToString() == "b")
this.DropDownList2.Items[1].Attributes.Add("style", "display:none");
else if (this.Dropdownlist1.SelectedItem.Value.ToString() == "c")
this.DropDownList2.Items[2].Attributes.Add("style", "display:none");
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.DropDownList2.SelectedItem.Value.ToString()=="a")
this.Dropdownlist1.Items[1].Attributes.Add("style", "display:none");
else if (this.DropDownList2.SelectedItem.Value.ToString() == "b")
this.Dropdownlist1.Items[2].Attributes.Add("style", "display:none");
else if (this.DropDownList2.SelectedItem.Value.ToString() == "c")
this.Dropdownlist1.Items[3].Attributes.Add("style", "display:none");
}
}
展开全部
不知道是不是你要的效果,你先看看吧,希望可以帮到你
前台
<asp:dropdownlist ID="Dropdownlist1" runat="server"
onselectedindexchanged="Dropdownlist1_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
<asp:ListItem Value="c">c</asp:ListItem>
</asp:dropdownlist>
<asp:DropDownList ID="DropDownList2" runat="server" onselectedindexchanged="Dropdownlist2_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
<asp:ListItem Value="c">c</asp:ListItem>
</asp:DropDownList>
后台
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.RemoveAt(Dropdownlist1.SelectedIndex);
}
protected void Dropdownlist2_SelectedIndexChanged(object sender, EventArgs e)
{
Dropdownlist1.Items.Remove(DropDownList2.SelectedValue);
}
前台
<asp:dropdownlist ID="Dropdownlist1" runat="server"
onselectedindexchanged="Dropdownlist1_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
<asp:ListItem Value="c">c</asp:ListItem>
</asp:dropdownlist>
<asp:DropDownList ID="DropDownList2" runat="server" onselectedindexchanged="Dropdownlist2_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem>请选择密保问题</asp:ListItem>
<asp:ListItem Value="a">a</asp:ListItem>
<asp:ListItem Value="b">b</asp:ListItem>
<asp:ListItem Value="c">c</asp:ListItem>
</asp:DropDownList>
后台
protected void Dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList2.Items.RemoveAt(Dropdownlist1.SelectedIndex);
}
protected void Dropdownlist2_SelectedIndexChanged(object sender, EventArgs e)
{
Dropdownlist1.Items.Remove(DropDownList2.SelectedValue);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只要加以判断,既然两个dropdownlist绑定的数据一样的话那么他们在数据库里面的id肯定也是一样的。所以。当选中第一个里面的某项的时候,获取到这一项的id,然后。写一个移除方法把他移除掉。第二个下拉框里面也写上这样的判断。应该就可以了。你试试....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果你的问题是直接写密保问题的话那就不需要这样做,直接用HTML控件
页面上:
<select>
<asp:Literal ID="LiterCom" runat="server"></asp:Literal>
</select>
后台cs:
int pageCount = partTime.GetPageCount(pageSize);
string option = "<option>--请选择公司名称--</option>";
for (int i = 0; i < pageCount; i++)
{
ListItem li = new ListItem((i + 1).ToString(), i.ToString());
option += "<option>"+li.Text+"</option>";
LiterCom.Text = option;
}
我上面只是提供一个后台如果要绑定问题的方法,如果你要像达到你的效果的话你只需要将其做个判断就行了!
页面上:
<select>
<asp:Literal ID="LiterCom" runat="server"></asp:Literal>
</select>
后台cs:
int pageCount = partTime.GetPageCount(pageSize);
string option = "<option>--请选择公司名称--</option>";
for (int i = 0; i < pageCount; i++)
{
ListItem li = new ListItem((i + 1).ToString(), i.ToString());
option += "<option>"+li.Text+"</option>";
LiterCom.Text = option;
}
我上面只是提供一个后台如果要绑定问题的方法,如果你要像达到你的效果的话你只需要将其做个判断就行了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询