我有4个下拉菜单
下拉列表中包含1到4的值
如果在下拉菜单D1中选择了值2,那么对于其余的下拉菜单,可用值1,3,4
如果在下拉菜单D2中选择了值4,那么对于其余的下拉菜单,可用值1,3
如果我在第一个下拉菜单中将值选择为1,然后在第二个下拉菜单中选择2,则其余下拉菜单仍保留3,4个值,但我被卡住了,好像我先将下拉菜单的值从1更改为3,然后在逻辑上1,4应该可以被其他下拉菜单选择,但是没有发生。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ChangingDDvalues
{
public partial class DDchange : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("State"), new DataColumn("Country"), new DataColumn("City") });
dt.Rows.Add(1, "UP", "India", "Agra<br/>Delhi<br/>Shimla");
dt.Rows.Add(2, "US", "America", "New york<br/>New Gercy<br/>Milan");
dt.Rows.Add(3, "Chicago", "Illinois", "Manipolis<br/>Perth<br/>Balino");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
}
protected void SelectionChanged(object sender, EventArgs e)
{
DropDownList current = sender as DropDownList;
GridViewRow row = current.NamingContainer as GridViewRow;
List<DropDownList> others = new List<DropDownList>();
foreach (Control item in row.Controls[4].Controls)
{
if (item.GetType() == typeof(DropDownList))
{
if (((DropDownList)item).ID != current.ID)
{
others.Add(item as DropDownList);
}
}
}
DisableSelectedText(current, others.ToArray());
}
protected void DisableSelectedText(DropDownList ddlCurrent, DropDownList[] others)
{
foreach (DropDownList item in others)
{
item.Items.Remove(ddlCurrent.SelectedItem.Value);
}
}
private DataTable GetFlag()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Text", typeof(string)), new DataColumn("Value", typeof(int)) });
dt.Rows.Add(1, 1);
dt.Rows.Add(2, 2);
dt.Rows.Add(3, 3);
dt.Rows.Add(4, 4);
return dt;
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
DataTable dt = GetFlag();
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl1 = e.Row.FindControl("ddl1") as DropDownList;
DropDownList ddl2 = e.Row.FindControl("ddl2") as DropDownList;
DropDownList ddl3 = e.Row.FindControl("ddl3") as DropDownList;
ddl1.DataSource = dt;
ddl1.DataTextField = "Text";
ddl1.DataValueField = "Value";
ddl1.DataBind();
ddl2.DataSource = dt;
ddl2.DataTextField = "Text";
ddl2.DataValueField = "Value";
ddl2.DataBind();
ddl3.DataSource = dt;
ddl3.DataTextField = "Text";
ddl3.DataValueField = "Value";
ddl3.DataBind();
}
}
}
}
<form id="form1" runat="server">
<asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" Visible="false" />
<asp:BoundField DataField="State" HeaderText="State" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:BoundField DataField="City" HeaderText="City" HtmlEncode="false" />
<asp:TemplateField HeaderText="State Flag">
<ItemTemplate>
<asp:DropDownList runat="server" Width="75px" ID="ddl1" OnSelectedIndexChanged="SelectionChanged"
AutoPostBack="true">
</asp:DropDownList>
<br />
<asp:DropDownList runat="server" Width="75px" ID="ddl2" OnSelectedIndexChanged="SelectionChanged"
AutoPostBack="true">
</asp:DropDownList>
<br />
<asp:DropDownList runat="server" Width="75px" ID="ddl3" OnSelectedIndexChanged="SelectionChanged"
AutoPostBack="true">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
答案 0 :(得分:0)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="div1">
<asp:DropDownList ID="ddl1" runat="server" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
<asp:ListItem Value="3" Text="3"></asp:ListItem>
<asp:ListItem Value="4" Text="4"></asp:ListItem>
</asp:DropDownList>
</div><br /><br />
<div id="div2">
<asp:DropDownList ID="ddl2" runat="server" OnSelectedIndexChanged="ddl2_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
<asp:ListItem Value="3" Text="3"></asp:ListItem>
<asp:ListItem Value="4" Text="4"></asp:ListItem>
</asp:DropDownList>
</div><br /><br />
<div id="div3">
<asp:DropDownList ID="ddl3" runat="server" OnSelectedIndexChanged="ddl3_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
<asp:ListItem Value="3" Text="3"></asp:ListItem>
<asp:ListItem Value="4" Text="4"></asp:ListItem>
</asp:DropDownList>
</div><br /><br />
<div id="div4">
<asp:DropDownList ID="ddl4" runat="server" OnSelectedIndexChanged="ddl4_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Value="0" Text="SELECT"></asp:ListItem>
<asp:ListItem Value="1" Text="1"></asp:ListItem>
<asp:ListItem Value="2" Text="2"></asp:ListItem>
<asp:ListItem Value="3" Text="3"></asp:ListItem>
<asp:ListItem Value="4" Text="4"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</form>
</body>
</html>
------------------------------aspx.cs code-----------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddl1.SelectedValue == ddl1.SelectedValue)
{
ddl2.Items.Remove(ddl1.SelectedValue);
ddl3.Items.Remove(ddl1.SelectedValue);
ddl4.Items.Remove(ddl1.SelectedValue);
}
}
protected void ddl2_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl2.SelectedValue == ddl2.SelectedValue)
{
ddl1.Items.Remove(ddl2.SelectedValue);
ddl3.Items.Remove(ddl2.SelectedValue);
ddl4.Items.Remove(ddl2.SelectedValue);
}
}
protected void ddl3_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl3.SelectedValue == ddl3.SelectedValue)
{
ddl1.Items.Remove(ddl3.SelectedValue);
ddl2.Items.Remove(ddl3.SelectedValue);
ddl4.Items.Remove(ddl3.SelectedValue);
}
}
protected void ddl4_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddl4.SelectedValue == ddl4.SelectedValue)
{
ddl1.Items.Remove(ddl4.SelectedValue);
ddl2.Items.Remove(ddl4.SelectedValue);
ddl3.Items.Remove(ddl4.SelectedValue);
}
}
}