[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.

 

<code>

<register.aspx>

<asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="True"
                    OnCreatedUser="RegisterUser_CreatedUser" BackColor="#F7F6F3"
                    BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px"
                    Font-Names="Verdana" Font-Size="0.8em">

 <p>
                                <asp:Label ID="CountryLabel" runat="server" AssociatedControlID="ddlCountry">Country:</asp:Label>
                                <asp:DropDownList ID="ddlCountry" runat="server" BackColor="White"  EnableViewState="true"
                                    ForeColor="Black" AutoPostBack="True" DataSourceID="SqlDataSource1"
                                    DataTextField="CountryName" DataValueField="Country#"
                                    onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList>
                                <asp:RequiredFieldValidator ID="CountryRequired" runat="server" ControlToValidate="ddlCountry"
                                     CssClass="failureNotification" ErrorMessage="Country is required." ToolTip="Country is required."
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>

                             <p>
                                <asp:Label ID="StateLabel" runat="server" AssociatedControlID="ddlState">State:</asp:Label>
                                <asp:DropDownList ID="ddlState" runat="server" BackColor="White" EnableViewState="true"
                                     ForeColor="Black" DataSourceID="SqlDataSource2"
                                     DataTextField="StateName" DataValueField="State#" AutoPostBack="True"></asp:DropDownList>
                                <asp:RequiredFieldValidator ID="StateRequired" runat="server" ControlToValidate="ddlState"
                                     CssClass="failureNotification" ErrorMessage="State is required." ToolTip="State is required."
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>

</asp:CreateUserWizard >

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
                    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                    SelectCommand="SELECT * FROM [Country]"></asp:SqlDataSource>
                <br />
                <asp:SqlDataSource ID="SqlDataSource2" runat="server"
                    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
                    SelectCommand="SELECT State.State#, State.StateName FROM State INNER JOIN StateCountry ON State.State# = StateCountry.State# WHERE (StateCountry.Country# = @column1)">
                    <SelectParameters>
                        <asp:SessionParameter Name="column1" SessionField="countryID" />
                    </SelectParameters>
                </asp:SqlDataSource>

</register.aspx>

<register.aspx.cs>

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();
        }
        
        
    }

</register.aspx.cs>

 

 

</code>

Use control parameter instead. <asp:controlparameter name= "column1" controlid= "ddlCountry" propertyname= "SelectedValue" /> 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 ,

<asp:controlparameter name= "column1" controlid= "RegisterUser$ddlCountry" propertyname= "SelectedValue" />

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.

Leave a Reply