根据第一个选定的下拉列表项

时间:2016-10-26 17:37:25

标签: c# asp.net

                   <div class="form-group col-lg-12">
                        <label for="ddl1">1:</label>
                        <asp:DropDownList ID="ddl1" name="ddl1" CssClass="form-control" runat="server"  />
                    </div>

                    <div class="form-group col-lg-12">
                        <label for="ddl2">1:</label>
                        <asp:DropDownList ID="ddl2" name="ddl2" CssClass="form-control" runat="server" required />
                    </div>

我试图根据我选择的ddl1的值来获取ddl2的下拉列表项。因此&#34; if&#34;功能与虚空Get1&amp; Get2。 注意:每个ddl的数据都来自我的数据库中的表。

    protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddl1Get();
        Get1();
        Get2();
    }

    #region "Function"
    if (ddl1.SelectedItem.Value == "1")
    {
        Get1();
    }
    else if (1.SelectedItem.Value == "2")
    {
        Get2();
    }
    #endregion
}

    #region "Function"
    if (ddl1.SelectedItem.Value == "1")
    {
        Get1();
    }
    else if (ddl1.SelectedItem.Value == "2")
    {
        Get2();
    }
 #endregion

 void Get1()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT ID, Category FROM Table1";
    SqlDataReader dr = cmd.ExecuteReader();
    ddl2.DataSource = dr;
    ddl2.DataTextField = "Category";
    ddl2.DataValueField = "Category";
    ddl2.DataBind();
    con.Close();

    ddl2.Items.Insert(0, new ListItem(" ", ""));
}

void Get2()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT ID, Category FROM Table2";
    SqlDataReader dr = cmd.ExecuteReader();
    ddl2.DataSource = dr;
    ddl2.DataTextField = "Category";
    ddl2.DataValueField = "Category";
    ddl2.DataBind();
    con.Close();

    ddl2.Items.Insert(0, new ListItem(" ", ""));
} 
void ddlGet()
{
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandText = "SELECT ID, Category FROM Table0";
    SqlDataReader dr = cmd.ExecuteReader();
    ddl1.DataSource = dr;
    ddl1.DataTextField = "Category";
    ddl1.DataValueField = "Category";
    ddl1.DataBind();
    con.Close();

    ddl1.Items.Insert(0, new ListItem(" ", ""));
}

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ddl1.SelectedItem.Value == "1")
    {
        Get1();
    }
    else if (ddl1.SelectedItem.Value == "2")
    {
        Get2();
    }
}

2 个答案:

答案 0 :(得分:2)

下拉列表中有selectedindexchanged ..只需双击下拉列表并将代码复制到其中。那它应该工作正常。

public void ddl1_selectedIndexChanged(sender,e){ //your code here }

您还在代码中两次提到过Get2()。这是一个复制错误还是你没注意到你的代码?这可能就是为什么即使选择的值被更改,也会为相同的值绑定的问题

答案 1 :(得分:0)

使用OnSelectedIndexChanged

asp:DropDownList事件
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
  if (ddl1.SelectedItem.Value == "1")
  {
    Get1();
  }
  else if (ddl1.SelectedItem.Value == "2")
  {
    Get2();
  }
}

另外:

  ddl1.DataTextField = "Category";
  ddl1.DataValueField = "ID";