Click here to Skip to main content
16,017,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I had problem in populating country and state dropdownlist.The problem is,

1. I had binded countrytable dropdown with country table
2. To get related state of a country I wrote code like

<code>
public void ddlCountry_SelectionChanged(object sender, EventArgs e)
{

AddressBookDataContext dc = new AddressBookDataContext();
DropDownList ddlFkCountry = (DropDownList)fvAddress.FindControl("ddlFKCountryId");
DropDownList ddlState = (DropDownList)fvAddress.FindControl("ddlFKStateId");
int countryId = Convert.ToInt32(ddlFkCountry.SelectedValue);
List&amp;amp;lt;State&amp;amp;gt; lststate = dc.States.Where(p =&amp;amp;gt; p.FKCountryId == countryId).ToList();
ddlState.DataSource = lststate;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "PKStateId";
ddlState.DataBind();
}
</code>

3. Its working Iam getting the output. Im getting the related state of a country.

4. Here comes the problem as im executing, As soon as the form is opened(when the page is opened) it is not displaying the related state of a country it is showing empty dropdown. When I select any other country in dropdownlist then only it is displaying the relating state.


My requirement is as im executing the code the state also should be shown with the related country.


Regards,
Grace.
Posted
Comments
demouser743 21-Sep-10 4:12am    
Did you write a query regarding that you need. As per my understanding you need to display state related to country right.
eslam soliman 21-Sep-10 8:49am    
i think that it is normal that is why the drop dawn list have to do post back may be you can bind the state ddl with a default value that is related to the first element in country ddl and when you select another country do your post back and bid the state ddl with the new values

you need to populate the statedropdown and call the method(same code as in ddlCountry_SelectionChanged) in page load event..
 
Share this answer
 
protected void Page_Load(object sender, EventArgs e)
{
AddressBookDataContext dc = new AddressBookDataContext();
DropDownList ddlFkCountry = (DropDownList)fvAddress.FindControl("ddlFKCountryId");
DropDownList ddlState = (DropDownList)fvAddress.FindControl("ddlFKStateId");
ddlFkCountry.SelectedIndex = 0; 
int countryId = Convert.ToInt32(ddlFkCountry.SelectedValue);
List<state> lststate = dc.States.Where(p => p.FKCountryId == countryId).ToList();
ddlState.DataSource = lststate;
ddlState.DataTextField = "StateName";
ddlState.DataValueField = "PKStateId";
ddlState.DataBind();
}
 
Share this answer
 
v3
you can also place 'Select Country' and 'Select State' at Row Index -1.

So that on Page Load Both dropdown will contain 'Select' and the moment

user changes dropdown your SelectedIndexChanged Event Will fired


DropDownListState.Items.Insert(-1, new ListItem("Select Country",""));
DropDownListState.Items.Insert(-1, new ListItem("Select State",""));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900