且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.Net C#中级联DropDownList?

更新时间:2023-02-01 13:57:00

你没有需要调用ddlCountry的事件处理程序,这可能会重置State下拉列表中的选择。此外,你似乎还有其他问题。

你试图通过stateid过滤国家。

检查这个 -

You don't need to call the event handler of ddlCountry, that may reset the selection in State dropdown. Further you seem to have other problems too.
You are trying to filter country by stateid.
Check this-
protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
   int M_STATE_SLNO = Convert.ToInt16(ddlState.SelectedValue);
   SqlConnection con = new SqlConnection(strConnection);
   con.Open();
   SqlCommand cmd = new SqlCommand("select * from M_COUNTRY where M_COUNTRY_CODE=(SELECT M_COUNTRY_CODE FROM M_STATE WHERE M_STATE_CODE="+M_STATE_SLNO+")", con); //Assuming coulmns names and table names as such for State table
   SqlDataAdapter da = new SqlDataAdapter(cmd);
   DataSet ds = new DataSet();
   da.Fill(ds);
   con.Close();
   ddlCountry.DataSource = ds;
   ddlCountry.DataTextField = "M_COUNTRY_NAME";
   ddlCountry.DataValueField = "M_COUNTRY_CODE";
   ddlCountry.DataBind();
   //ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
   //ddlCountry_SelectedIndexChanged(null, null);
}





希望,它有帮助:)



Hope, it helps :)