我有3个下拉菜单。当从第一个ddl中选择项目时,使用各个项目生成第二个ddl。当选择第二ddl时,使用各自的数据生成第三ddl。我的问题是,当从第三个ddl中选择项目时,需要在网格视图中显示3个项目(1个ddl的所选项目,2 ddl的所选项目,3 ddl的所选项目)。怎么实现呢? aspx页面:
<body>
<form id="form1" runat="server">
<asp:DropDownList ID="DDLCountry" runat="server" OnSelectedIndexChanged="DDLCountry_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>--Select-- </asp:ListItem>
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>Australia</asp:ListItem>
<asp:ListItem>Pakistan</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DDLState" runat="server" OnSelectedIndexChanged="DDLState_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<asp:DropDownList ID="DDLCity" runat="server" AutoPostBack="true"></asp:DropDownList>
<div>
<asp:GridView ID="Grd1" runat="server">
</asp:GridView>
</div>
</form>
aspx.cs页面
protected void DDLCountry_SelectedIndexChanged(object sender, EventArgs e)
{
DDLCity.Items.Clear();
DDLState.Items.Clear();
if (DDLCountry.SelectedIndex == 1)
{
DDLState.Items.Add(new ListItem("select", "0"));
DDLState.Items.Add(new ListItem("Kerala", "1"));
DDLState.Items.Add(new ListItem("AP", "2"));
DDLState.Items.Add(new ListItem("Karnataka", "3"));
}
else if (DDLCountry.SelectedIndex == 2)
{
DDLState.Items.Add(new ListItem("select", "0"));
DDLState.Items.Add(new ListItem("Victoria", "1"));
DDLState.Items.Add(new ListItem("Western Australia", "2"));
DDLState.Items.Add(new ListItem("New South Wales", "3"));
}
else if (DDLCountry.SelectedIndex == 3)
{
DDLState.Items.Add(new ListItem("select", "0"));
DDLState.Items.Add(new ListItem("Balochstan", "1"));
DDLState.Items.Add(new ListItem("Sindh", "2"));
DDLState.Items.Add(new ListItem("Punjab", "3"));
}
}
protected void DDLState_SelectedIndexChanged(object sender, EventArgs e)
{
DDLCity.Items.Clear();
if ((DDLCountry.SelectedIndex == 1) && (DDLState.SelectedIndex == 1))
{
DDLCity.Items.Add(new ListItem("Calicut", "0"));
DDLCity.Items.Add(new ListItem("Kochi", "1"));
}
else if ((DDLCountry.SelectedIndex == 1) && (DDLState.SelectedIndex == 2))
{
DDLCity.Items.Add(new ListItem("Hyderabad", "0"));
DDLCity.Items.Add(new ListItem("Vijayawada", "1"));
}
else if ((DDLCountry.SelectedIndex == 1) && (DDLState.SelectedIndex == 3))
{
DDLCity.Items.Add(new ListItem("Bengaluru", "0"));
DDLCity.Items.Add(new ListItem("Mysore", "1"));
}
else if ((DDLCountry.SelectedIndex == 2) && (DDLState.SelectedIndex == 1))
{
DDLCity.Items.Add(new ListItem("Melbourne", "0"));
DDLCity.Items.Add(new ListItem("A", "1"));
}
else if ((DDLCountry.SelectedIndex == 2) && (DDLState.SelectedIndex == 2))
{
DDLCity.Items.Add(new ListItem("Perth", "0"));
DDLCity.Items.Add(new ListItem("B", "1"));
}
else if ((DDLCountry.SelectedIndex == 2) && (DDLState.SelectedIndex == 3))
{
DDLCity.Items.Add(new ListItem("Sydney", "0"));
DDLCity.Items.Add(new ListItem("C", "1"));
}
else if ((DDLCountry.SelectedIndex == 3) && (DDLState.SelectedIndex == 1))
{
DDLCity.Items.Add(new ListItem("Lahor", "0"));
DDLCity.Items.Add(new ListItem("D", "1"));
}
else if ((DDLCountry.SelectedIndex == 3) && (DDLState.SelectedIndex == 2))
{
DDLCity.Items.Add(new ListItem("Karachi", "0"));
DDLCity.Items.Add(new ListItem("E", "1"));
}
else if ((DDLCountry.SelectedIndex == 3) && (DDLState.SelectedIndex == 3))
{
DDLCity.Items.Add(new ListItem("Peshawar", "0"));
DDLCity.Items.Add(new ListItem("F", "1"));
}
}
答案 0 :(得分:1)
protected void DDLCity_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dr = dt.NewRow();
dr["Column1"] = DDLState.SelectedItem.Text;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Column1"] = DDLCountry.SelectedItem.Text;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Column1"] = DDLCity.SelectedItem.Text;
dt.Rows.Add(dr);
dbGridView.DataSource = dt;
dbGridView.DataBind();
}
答案 1 :(得分:0)
我为你做了一个样本也许有帮助:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList3_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
和cs代码:
public partial class _Default : System.Web.UI.Page
{
public Random r;
protected void Page_Load(object sender, EventArgs e)
{
r = new Random();
if (!IsPostBack)
{
for (int i = 0; i < 10; i++)
{
int n = r.Next(100, 200);
DropDownList1.Items.Add(new ListItem(n.ToString(), n.ToString()));
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
r = new Random();
for (int i = 0; i < 10; i++)
{
int n = r.Next(300, 400);
DropDownList2.Items.Add(new ListItem(n.ToString(), n.ToString()));
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
r = new Random();
for (int i = 0; i < 10; i++)
{
int n = r.Next(500,600);
DropDownList3.Items.Add(new ListItem(n.ToString(), n.ToString()));
}
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["Column1"] = DropDownList1.SelectedItem.Text;
dr["Column2"] = DropDownList2.SelectedItem.Text;
dr["Column3"] = DropDownList3.SelectedItem.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}