0

我有 3 个下拉菜单。当从第一个 ddl 中选择一个项目时,第二个 ddl 由相应的项目生成。选择第二个 ddl 时,第三个 ddl 生成各自的数据。我的问题是,当从第三个 ddl 中选择一个项目时,需要在网格视图中显示 3 个项目(1 个 ddl 的选定项目、2 个 ddl 的选定项目、3 个 ddl 的选定项目)。如何做到这一点?页面:

  <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"));
    }


}
4

2 回答 2

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();
}
于 2013-07-26T07:10:41.607 回答
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();

    }

}
于 2013-07-26T07:13:07.583 回答