开发者

DropDown List resets to the first item and doesn't return a selectedvalue

开发者 https://www.devze.com 2023-02-13 23:57 出处:网络
So I have a country dropdownlist and a state dropdownlist which is being populated dynamically based off of the country chosen. When I click the country the state dropdown gets populated just fine but

So I have a country dropdownlist and a state dropdownlist which is being populated dynamically based off of the country chosen. When I click the country the state dropdown gets populated just fine but the problem arises when I click a value (state) from the other dropdown, the list instead of retaining the selected item will go back to the first item of the list and no selectedvalue are displayed.

<td><asp:DropDownList ID="ddlState" runat="server"
    DataSourceId="dsStateList"
    DataTextField="state_nm"
    DataValueField="state_cd"
    OnSelectedIndexChanged="ddlState_SelectedIndexChanged"
    AutoPostBack="true"
    AppendDataBoundItems="true" 
    Width="160px" OnDataBound="ddlState_OnDataBound">
    </asp:DropDownList>
</td>

    <asp:DropDownList ID="ddlCountry" runat="server"
    DataSourceId="dsCountryList"
    DataTextField="COUNTRY_NAME"
    DataValueField="COUNTRY_CIA_ID"
    OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
    OnDataBound="ddlCountry_OnDataBound"
    AutoPostBack="true"
    AppendDataBoundItems="true" 
    Width="160px">
    </asp:DropDownList>


protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
{
    string comboStateCODE = ddlState.SelectedValue;
    dsCompanyListParam.Text = comboStateCODE;
    ddlCountry.DataBind();
    ddlState.DataBind();
}

protected void ddlState_OnDataBound(object sender, EventArgs e)
{
    ddlState.Items.Insert(0, "Please Select a State");
}
protected v开发者_如何学Pythonoid ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    ddlState.Items.Clear();
    dsStateList.SelectParameters["iCountryID"].DefaultValue = ddlCountry.SelectedValue;

    dsCompanyListParam.Text = ddlCountry.SelectedValue;
    Trace.Warn("ddlCountry_SelectedIndexChanged");

    ddlCountry.DataBind();
    ddlState.DataBind();

}
protected void ddlCountry_OnDataBound(object sender, EventArgs e)
{
    ddlCountry.Items.Insert(0, "Please Select a Country");
}


I presume that somewhere in your Page_Load() you are making a call to a method that populates the dropdown... you need to encapsulate this into an IF !PostBack block:

// somewhere in PageLoad()...
If(!IsPostBack)
{
    PopulateDropdown();
}

Using the convention above, the dropdown will only be populated on the first ever page load. What I suspect is happening is that when you make a selection from the other dropdown, the AutoPostBack is executing the Page_Load() method (as it should) and repopulating the dropdowns again.

Using the convention above should help avoid this.


Your state drop down is set to Autopostback - is it possible that your code to populate the country drop down is executing again on postback, thus rendering the selected state invalid because the country dropdown was repopulated


I would remove the ddlCountry.DataBind(); from the ddlState_SelectedIndexChanged event. I don't see why you need to do another DataBind there.


Solved it! Ok, just so anyone who's stuck with a similar problem and can't find any other areas to look at here's how I fixed the stupid problem. First of all, I was using a stored procedure and the stored procedure is concatenating the values from two fields. I set the parameters the Integer(4) which I didn't noticed there're a couple of countries with codes of more than 4. So basically, it's returning a NULL selectedvalue which in turn will not generate any value since my onselectedindexchanged method is based off of the selectedvalue and for some reason a NULL is not being processed. So yeah, check your stored procs and parameter data! :D Thanks for your time!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号