views:

439

answers:

2

Hi guys I use 2 requiredfiledvalidator for 2 selects, one is working but the second one (the one I need) isnt

    <asp:dropdownlist id="ddlMod" runat="server" Width="235px" AutoPostBack="True" Font-Names="Arial" Font-Size="XX-Small">
</asp:dropdownlist>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" Font-Names="Arial" Font-Size="XX-Small"
ErrorMessage="Select Mod" InitialValue="00" Display="Dynamic" ControlToValidate="ddlMod">
</asp:requiredfieldvalidator>

<asp:dropdownlist id="ddlInd" runat="server" Width="235px" AutoPostBack="True" Font-Size="XX-Small">
</asp:dropdownlist>
<asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" Font-Names="Arial" Font-Size="XX-Small"
ErrorMessage="Select Ind" InitialValue="0" Display="Dynamic" ControlToValidate="ddlInd">
</asp:requiredfieldvalidator>

If i select the 2nd one I can see the error message for the 1st one, but I can't see any message if I dont select any or if I select the 1st one. Thanks!

A: 

I believe this is a known asp.net limitation of using the AutoPostBack="true" property on the dropdown lists. The AutoPostBack property negates the validation process and posts back.

As a simple workaround, you can cause the validation to occur during the postback by modifying the page load event:

protected void Page_Load(object sender, EventArgs e)
{
    if(Page.IsPostBack)
        Page.Validate();
}

This will cause the validation to still occur after an AutoPostBack situation. If the drop down list values are incorrect, the page will refresh with the error messages shown as expected. The downside is that the screen will flicker, etc. but afaik the only other option is to add custom client-side validation scripting to each DropDownList control which I personally don't think is worth it.

With the above code, remember that when the page automatically posts back, it could actually be invalid - yet other methods you may have called might be expecting a valid page. Use the Page.IsValid property to protected against this. (Use of Page.IsValid is actually best practice anyway for validated forms.)

Example:

protected void Page_Load(object sender, EventArgs e)
{
    if(Page.IsPostBack)
        Page.Validate();

    ddlMod.SelectedIndexChanged += new EventHandler(ddlMod_SelectedIndexChanged);

    if(Page.IsValid)
    {
        //load some data; do some things...
    }
}

protected void ddlMod_SelectedIndexChanged(object sender, EventArgs e)
{
    if(Page.IsValid)
    {
        //do some stuff if the page validated
    }
}

One final note - you will want to make sure Page.Validate() is called early in the page lifecycle (Page_Load is a good point) as if you reference Page.IsValid and Validate() has not yet been called, Page.IsValid with throw a big nasty exception...

If you want to get rid of the flicker from the auto postback, you could also use an ajax UpdatePanel to make things seamless to the user.

Hope this helps!

KP
Thanks for the fast answer, but is still not working. I still need to select ddlMod first in order to work. I dont know why is this happening because under IE it works just fine.What else could i try? Thanks in advice
Giskard
A: 

I would like to know one thing here. Are your drop downlists inside a panel? Panels dont quite work properly in firefox.

Abhishek Rao