0

私は3つのドロップダウンを持っています。最初の ddl、2 番目の ddl からアイテムを選択すると、それぞれのアイテムで生成されます。2 番目の ddl を選択すると、3 番目の ddl がそれぞれのデータで生成されます。私の問題は、3 番目の 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"));
    }


}
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 に答える