下拉未正确加载

时间:2013-02-06 05:27:42

标签: asp.net .net

尊敬的用户

我的应用程序中有两个下拉列表,如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());

当页面加载时,它的工作正常。 但, 在文本更改或选择更改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。

2 个答案:

答案 0 :(得分:1)

将您的查询更改为:

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

仅当请求不是ddlCountry

时,才会在Page_Load上填充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());
        }
    }

答案 1 :(得分:0)

希望它会对你有所帮助。

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

然后选择ddlCountry绑定状态下拉列表的选择更改事件

相关问题