Dropdownlist onselectedindexchanged事件无效 [英] Dropdownlist onselectedindexchanged event is not working

查看:303
本文介绍了Dropdownlist onselectedindexchanged事件无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在页面加载时,国家/地区下拉列表已绑定且工作正常但在我选择国家/地区后,状态下拉列表中没有状态绑定。



我的尝试:



 ------------ ----------前端----------------------------- 
< asp:DropDownList ID = ddlcountry runat = server OnSelectedIndexChanged = ddlcountry_SelectedIndexChanged AutoPostBack = true CssClass = < span class =code-string> form-control ddl > < / asp:DropDownList >

< asp:DropDownList ID = < span class =code-string> ddlstate runat = server AutoPostBack = true CssClass = form-control ddl > < / asp:DropDownList >

--------------------------------背后的代码---------- --------------------

protected void Page_Load( object sender,EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (!Page.IsPostBack)
{
getcountry();
}
}
受保护 void getcountry()
{
尝试
{
da = new SqlDataAdapter(< span class =code-string> select * from get_country,con);
ds = new DataSet();
da.Fill(ds);
ddlcountry.DataSource = ds;
ddlcountry.DataTextField = conname;
ddlcountry.DataValueField = conid;
ddlcountry.DataBind();
ddlcountry.Items.Insert( 0 new ListItem( - 选择 - 0));
ddlstate.Items.Insert( 0 new ListItem( - 选择 - 0));
}
catch (例外情况)
{
Response.Write( 发生错误: + ex.Message.ToString());
}
最后
{
con.Close();
}
}
受保护 void ddlcountry_SelectedIndexChanged( object sender,EventArgs e)
{
try
{
< span class =code-keyword> int
conid = Convert.ToInt32(ddlcountry.SelectedItem.Value.ToString());
da = new SqlDataAdapter( select stateid ,来自get_state的statename,其中conid = + conid,con);
ds = new DataSet();
da.Fill(ds);
ddlstate.DataSource = ds;
ddlstate.DataTextField = statename;
ddlstate.DataValueField = stateid;
ddlstate.DataBind();
ddlstate.Items.Insert( 0 new ListItem( ---选择状态--- 0\" ));
if (ddlstate.SelectedValue == 0
{
ddlcity.Items.Clear();
ddlcity.Items.Insert( 0 new ListItem( ---选择City --- 0\" ));
}
}
catch (例外情况)
{
Response.Write( 错误: + ex.Message.ToString());
}
最后
{
con.Close();
}
}

解决方案

基于评论代码



 ddlstate.Items.Insert( 0  new  ListItem(  ---选择状态---  0));  //  第1行 
if (ddlstate.SelectedValue == 0 / / 第2行
{
ddlcity.Items.Clear(); // 第3行
ddlcity.Items.Insert( 0 new ListItem( ---选择城市--- 0));
}




第1行中的
您在第0个索引处插入一个值,默认情况下会选择这个值
第2行中
您正在检查所选值是否等于0,当然它将仅为0,它将在第3行中传递条件

它将清除ddlcity中的所有项目

最终你的ddlstate将是空的,



解决方案:

 < span class =code-keyword> if ( ddlCountry  .SelectedValue ==   0 //  将州改为国家 


on page load the country dropdownlist is bind and working fine but after when i select the country than no state bind in state dropdownlist.

What I have tried:

 ----------------------front end-----------------------------
<asp:DropDownList ID="ddlcountry" runat="server" OnSelectedIndexChanged="ddlcountry_SelectedIndexChanged" AutoPostBack="true" CssClass="form-control ddl"></asp:DropDownList>
 
<asp:DropDownList ID="ddlstate" runat="server" AutoPostBack="true" CssClass="form-control ddl"></asp:DropDownList> 
 
--------------------------------code behind------------------------------ 
 
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (!Page.IsPostBack)
{
getcountry();
}
}
protected void getcountry()
{
try
{
da = new SqlDataAdapter("select * from get_country", con);
ds = new DataSet();
da.Fill(ds);
ddlcountry.DataSource = ds;
ddlcountry.DataTextField = "conname";
ddlcountry.DataValueField = "conid";
ddlcountry.DataBind();
ddlcountry.Items.Insert(0, new ListItem("--Select--", "0"));
ddlstate.Items.Insert(0, new ListItem("--Select--", "0"));
}
catch (Exception ex)
{
Response.Write("error occurd :" + ex.Message.ToString());
}
finally
{
con.Close();
}
}
protected void ddlcountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int conid = Convert.ToInt32(ddlcountry.SelectedItem.Value.ToString());
da = new SqlDataAdapter("select stateid,statename from get_state where conid=" + conid, con);
ds = new DataSet();
da.Fill(ds);
ddlstate.DataSource = ds;
ddlstate.DataTextField = "statename";
ddlstate.DataValueField = "stateid";
ddlstate.DataBind();
ddlstate.Items.Insert(0, new ListItem("---Select State---", "0"));
if (ddlstate.SelectedValue == "0")
{
ddlcity.Items.Clear();
ddlcity.Items.Insert(0,new ListItem("---Select City---","0"));
}
}
catch(Exception ex)
{
Response.Write("error occurd :" + ex.Message.ToString());
}
finally
{
con.Close();
}
}

解决方案

based on the code from Comments

ddlstate.Items.Insert(0, new ListItem("---Select State---", "0")); // line 1
if (ddlstate.SelectedValue == "0")  // line 2
{
ddlcity.Items.Clear();  // line 3
ddlcity.Items.Insert(0,new ListItem("---Select City---","0"));
}



in line 1 you are inserting a value at 0th index, which will be selected by default
in line 2 you are checking the selected value is equals 0, of course it will be 0 only, it will pass inside the condition
in line 3 it will clear all the items in ddlcity
ultimately your ddlstate will be empty,

Solution:

if (ddlCountry.SelectedValue == "0") // change state to country


这篇关于Dropdownlist onselectedindexchanged事件无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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