如何在ASP.Net C#中级联DropDownList? [英] How to Cascade DropDownList in ASP.Net C#?

查看:60
本文介绍了如何在ASP.Net C#中级联DropDownList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends ..我正在做一个基于下拉列表的小项目。我有两张桌子,State&Country。我有两个下拉列表ddlState,ddlCountry。当我在ddlState中选择一个特定的状态时,该状态的国家应该出现在ddlCountry中..



(我可以做其他方式...就像,我可以从ddlCountry中选择一个国家,并且该国家/地区的状态将显示在ddlState中)

请帮助我

Hello Friends.. I am doing a small project based on Drop Down List. I have two tables, State & Country. I have two Dropdowns ddlState, ddlCountry. When i select a particular state in ddlState, the country of that state should appear in ddlCountry..

(I can able to do other way round.. like, I can select a country from ddlCountry & the states of that country are getting displayed in ddlState)
please help me

推荐答案

你没有需要调用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 :)


这篇关于如何在ASP.Net C#中级联DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆