2014年7月13日 星期日

[RESOLVED] Bind Country/State DDL


I have two Dropdownlists within a CreateuserWizard. One for Country and the other for State. I also have two sqldatasources. I am trying to load the state dropdownlist based on the selection of the the country dropdownlist. The state dropdownlist is not
populating.


 




                    OnCreatedUser="RegisterUser_CreatedUser" BackColor="#F7F6F3"
BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="0.8em">


Country:
ForeColor="Black" AutoPostBack="True" DataSourceID="SqlDataSource1"
DataTextField="CountryName" DataValueField="Country#"
onselectedindexchanged="ddlCountry_SelectedIndexChanged">

CssClass="failureNotification" ErrorMessage="Country is required." ToolTip="Country is required."
ValidationGroup="RegisterUserValidationGroup">*




State:
ForeColor="Black" DataSourceID="SqlDataSource2"
DataTextField="StateName" DataValueField="State#" AutoPostBack="True">

CssClass="failureNotification" ErrorMessage="State is required." ToolTip="State is required."
ValidationGroup="RegisterUserValidationGroup">*





ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Country]">



ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT State.State#, State.StateName FROM State INNER JOIN StateCountry ON State.State# = StateCountry.State# WHERE (StateCountry.Country# = @column1)">







protected void Page_Load(object sender, EventArgs e)
{
RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];


if (!Page.IsPostBack)
{
DropDownList stateList = (DropDownList)RegisterUserWizardStep.ContentTemplateContainer.FindControl("ddlState");
DropDownList countryList = (DropDownList)RegisterUserWizardStep.ContentTemplateContainer.FindControl("ddlCountry");
Session["countryID"] = countryList.SelectedValue.ToString();
stateList.DataBind();
}


}



 


 




Use control parameter instead. You don't need to write some code behind to populate ddlState items. Don't join the table, simply set criteria.

Error :


Could not find control 'ddlCountry' in ControlParameter 'column1'.


I also tried countryList.



Maybe you can place Country/State DDL, SqlDataSource in same template



hello


try somthing like this


Session["countryID"] = countryList.SelectedValue.ToString();
// first clear statelist then assign value
stateList.ClearSelection();
stateList.DataBind();

hope this works for you 


thanks


manish








try the following code ,



hopfully it will help.





Hi Code_warrior...,


As oned_gk suggested, using ControlParameter would be better here. Also, for the following error you encountered:


"Could not find control 'ddlCountry' in ControlParameter 'column1'."


it is due to the SqlDataSource control cannot locate the dropdownlist control (ddlCountry) inside the CreateUserWizard control (they are not in the same container). As oned_gk suggested, you can try putting the SqlDataSource control into the CreateUserWizard
(make them in the same container) to see if it works.


沒有留言:

張貼留言