2013-02-06 118 views
0

尊敬的用户,下拉未正确加载

我的应用程序中有两个下拉列表为ddlCountry和ddlState。 我有数据库

tlbCountry

ID(PK)|国家名称

tlbState

ID(PK)| countryName | Statename的

在页面加载我已经装在ddlCountry下拉列表中的所有项目作为

sqldataadapter da=new sqldataadapter("select countryName from tlbCountry",con); 
dataset ds=new dataset(); 
da.fill(ds); 
for(int i=0;i<ds.tables[0].rows.count;i++) 
ddlCountry.items.add(ds.Tables[0].Rows[i][0].toString()); 

当页面被加载时,高达该公司工作的罚款。 但是, 在Textchange或ddlCountry的选择更改事件上时,它会尝试从tlbStates表中获取相应状态的值,如下所示。它不工作,我做了如下,

sqldataadapter da=new sqldataadapter("select stateName from tlbState where countryName like '"+ddlCountry.selectedItem.toString()+"'",con); 
dataset ds=new dataset(); 
da.fill(ds); 
for(int i=0;i<ds.tables[0].rows.count;i++) 
ddlState.items.add(ds.Tables[0].Rows[i][0].toString()); 

在这种情况下,它不加载ddlState下拉。

当我打开ddlCountry的autopostback时,它会在ddlState中重新加载没有值的页面。 可能是什么问题?

注意: - 我已经使用了AJAX UPDATE PANEL。

+0

尝试在你使用'ddlCountry.selectedItem.Text'上ddlCountry –

+0

的变化提取国家查询在调试时,直接在SQL Server中运行它并分享你的发现。 –

+0

@Behroz调试时,我发现它不会进入ddlcountry组合中的事件函数selectedindexchanged,我尝试在ddlStates中加载状态以查看ddlCountry。我让ddlCountry的autopostback为true,那么它也没有工作 – Freelancer

回答

1

查询改成这样:

"select stateName from tlbState where countryName = '" + ddlCountry.Text + "'" 

在Page_Load中填写您的ddlCountry只有当请求不是PostBack

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      //Load Countires 
      sqldataadapter da = new sqldataadapter("select countryName from tlbCountry", con); 
      dataset ds = new dataset(); 
      da.fill(ds); 
      for (int i = 0; i < ds.tables[0].rows.count; i++) 
       ddlCountry.items.add(ds.Tables[0].Rows[i][0].toString()); 
     } 
    } 
+0

我的查询可能是错误的,但主要问题是,它没有在ddlCountry的onselectedindexchanged事件函数中提供。所以,即使这个查询是写入的,程序流程也没有完成。 – Freelancer

+0

检查你是否在每个'Load'上添加国家。这将重置“SelectedItem”并导致事件不被触发。 – nunespascal

+0

是的,我加入它onload。 – Freelancer

0

希望它会帮助你。

if (!IsPostBack) 
{ 
    //here bind country drop down on page load event. 
} 

,然后ddlCountry绑定状态下拉菜单选择更改事件