DropDownList SelectedIndexChanged不起作用

时间:2016-08-17 09:52:06

标签: c#

从DropDownList中选择值,但它不会调用SelectedIndexChanged事件。

为了选择值,必须调用我的代码所在的SelectedIndexchanged事件,但当我尝试从DropDownList中选择一些东西时,我确实选择了SelectIndexChanged事件

John Doe| fishing | reading| swimming| jogging



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

protected void BindContrydropdown()
{
    // Connection path for database

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from TblCountry", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlCountry.DataSource = ds;
    ddlCountry.DataTextField = "Name";
    ddlCountry.DataValueField = "CountryID";
    ddlCountry.DataBind();
    ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
    ddlState.Items.Insert(0, new ListItem("--Select--", "0"));
}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    int CountryID = Convert.ToInt32(ddlCountry.SelectedValue);

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from TblState where CountryID=" + CountryID, con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    ddlState.DataSource = ds;
    ddlState.DataTextField = "StateName";
    ddlState.DataValueField = "StateID";
    ddlState.DataBind();
}




0 个答案:

没有答案
相关问题