Click here to Skip to main content
15,028,618 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
Hello,

I have 3 dropdown list. All 3 are required field validators.

On Selected index change of country, state dropdown is going to fill and on selected index change of state, city dropdown is going to fill.

Now, the issue is, when i click on register button click, without filling any details in form, all validations are coming properly.

Now,when i select country, the validation message of country disappers. that part is working perfectly.

But when i select any state, the validation message is not going to disapper.

For that, i have removed eventname from asynctrigger of update panel. Now, validation message for state is going to disapper, but city field is not going to bind because we removed trigger event.

My requirement is if someone select country, then country validation message should disapper. State and city validation message should be there. If i select state, then state validation message should disappear, city validation message should be available.

I am sending my code as below,

                       <div class="time_lne">
                                <label>
                                    <span>
                                        <img src="images/red_error.png" alt="error" /></span> Country</label>
                                <asp:UpdatePanel ID="uppnlCountry" runat="server">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlCountry" CssClass="select_ex" Width="304px" AutoPostBack="true"
                                            runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged">
                                        </asp:DropDownList>
                                    </ContentTemplate>
                                </asp:UpdatePanel>
                                <span>
                                    <asp:RequiredFieldValidator ID="reqCountry" runat="server" ErrorMessage="This Field is required"
                                        ForeColor="Red" ControlToValidate="ddlCountry"></asp:RequiredFieldValidator>
                                </span>
                            </div>
                            <div class="time_lne">
                                <label>
                                    <span>
                                        <img src="images/red_error.png" alt="error" /></span> Province</label>
                                <asp:UpdatePanel ID="uppnlState" runat="server" UpdateMode="Conditional">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlState" CssClass="select_ex" Width="304px" AutoPostBack="true"
                                            runat="server" OnSelectedIndexChanged="ddlState_SelectedIndexChanged">
                                        </asp:DropDownList>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
                                    </Triggers>
                                </asp:UpdatePanel>
                                <span>
                                    <asp:RequiredFieldValidator ID="reqState" runat="server" ErrorMessage="This Field is required"
                                        ForeColor="Red" ControlToValidate="ddlState"></asp:RequiredFieldValidator>
                                </span>
                            </div>
                            <div class="time_lne">
                                <asp:UpdatePanel ID="uppnlCity" runat="server" UpdateMode="Conditional">
                                    <ContentTemplate>
                                        <div class="time_lne">
                                            <label>
                                                <span>
                                                    <img src="images/red_error.png" alt="error" /></span> City</label>
                                            <asp:DropDownList ID="ddlCity" CssClass="select_ex" Width="304px" AutoPostBack="true"
                                                runat="server" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged">
                                            </asp:DropDownList>
                                        </div>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlState" EventName="SelectedIndexChanged" />
                                    </Triggers>
                                </asp:UpdatePanel>
                                <span>
                                    <asp:RequiredFieldValidator ID="reqCity" runat="server" ErrorMessage="This Field is required"
                                        ForeColor="Red" ControlToValidate="ddlCity"></asp:RequiredFieldValidator>
                                </span>
                            </div>

In .cs file i have done this code

 protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindState();

    }

    /// <summary>
    /// Used to bind city drop down as per the state drop down selection changes
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCity();

    }

    /// <summary>
    /// Used to bind suburb drop down as per the city drop down selection changes
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSuburb();
    }


=========

 /// <summary>
    /// Used to bind state drop down
    /// </summary>
    private void BindState()
    {
        try
        {
            string strCountryId = ddlCountry.SelectedValue;
            ddlState.Items.Clear();
            if (strCountryId != "")
            {
                StateBL state = new StateBL();
                ddlState.DataSource = state.GetStateByCountry(Convert.ToInt16(strCountryId));
                ddlState.DataTextField = "StateName";
                ddlState.DataValueField = "Id";
                ddlState.DataBind();
            }
            ddlState.Items.Insert(0, new ListItem("Select State", ""));
        }
        catch (Exception ex)
        {
            ValidationScript("EmployerRegister-->BindState :: " + ex.Message);
        }
    }

    /// <summary>
    /// This function will bind city
    /// </summary>
    private void BindCity()
    {
        try
        {
            string strStateId = ddlState.SelectedValue;
            ddlCity.Items.Clear();
            if (strStateId != "")
            {
                CityBL city = new CityBL();
                ddlCity.DataSource = city.GetCityByStateId(Convert.ToInt32(strStateId));
                if (ddlCity.DataSource != null)
                {
                    ddlCity.DataTextField = "CityName";
                    ddlCity.DataValueField = "Id";
                    ddlCity.DataBind();
                }
            } ddlCity.Items.Insert(0, new ListItem("Select City", ""));
        }
        catch (Exception ex)
        {
            ValidationScript("EmployerRegister-->BindCity :: " + ex.Message);
        }
    }

================

When i gone through the viewsource of Page, at that time i found one thing on selected index change event of Country, in viewsource i am getting country list like "India,US" in option button, but state list is not going to come in option. So i thaught that script is not going to fire because there is not any values in option button. So validation remains at that location, when we fill state.

If i put validation message in updatepanel then on selected index change of country, state validationmessage is also going to disappear.

I have tried with jquery, but i am getting the same result.

So, Please suggest me for the same ASAP. If you required more details, please let me know.

Thanks
Posted

ASP .NET Ajax used to take care.. not sure y u r missing
   
Hi,
Thanks for your response. Can you please explain me more.
   

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