locked
How to use requiredfield validator with ajax combo box? RRS feed

  • Question

  • User-845805365 posted

    Does any body know how to validate ajax combo box using required field validator?

    Monday, October 12, 2009 12:37 PM

All replies

  • User1291671979 posted

    It's pretty straightforwards:

    1. Add a combobox, a required field validator and a linkbutton.
    2. Set the validator's controltovalidate property to the name of the combobox.
    3. Ensure all the controls' ValidationGroup property is set to the same value.
    4. Start it up, ensure the combobox is empty and click the linkbutton: you'll see the requiredfield display it's message.

    The following code might help you:

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <cc1:ComboBox ID="ComboBox1" runat="server" ValidationGroup="val">
            <asp:ListItem>foobar</asp:ListItem>
        </cc1:ComboBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="ComboBox1" ErrorMessage="RequiredFieldValidator" 
            ValidationGroup="val"></asp:RequiredFieldValidator>
        <asp:LinkButton ID="LinkButton1" runat="server" ValidationGroup="val">LinkButton</asp:LinkButton>


    Wednesday, October 14, 2009 5:37 PM
  • User-845805365 posted

    Its not performing as normal dropdownlist. on the button click, its going inside the code with out validating.

     <ajaxToolkit:ComboBox ID="uii" runat="server" ValidationGroup="val">
            <asp:ListItem Value="0">Select</asp:ListItem>
            <asp:ListItem>New</asp:ListItem>
        </ajaxToolkit:ComboBox>
        
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ControlToValidate="uii" ErrorMessage="RequiredFieldValidator" 
                ValidationGroup="val"></asp:RequiredFieldValidator>
            <br />
            <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" 
                ValidationGroup="val">LinkButton</asp:LinkButton>


    This is my markup. Wile searing on the net i came accross that ajaxcombo validation fails with reuiredfied validator...

    is there any other work around for this?

    Thursday, October 15, 2009 10:08 AM
  • User-876702696 posted

    Hey - have a look at this: http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=22429

    However the test code posted still only validates on the server (in FF anyway) - however the following seems to solve the problem:

    http://ajaxcontroltoolkit.codeplex.com/WorkItem/View.aspx?WorkItemId=24417

    That's not a very workable solution - but I think it also explains why the default required validator does not work - the Ajax Combobox is of course not really a <select> element but textbox (i.e. <input type="text">) with a load of javascript hanging off it. This is then mapped back to a ListControl in .NET.

    Most likely the answer to this problem is either to use the Combobox within an updatepanel (in which case it does not (really) matter that it does not validate on the client) or write your own JavaScript to validate (I guess you could inherit from the ComboBox and have some fun with RegisterStartupScript in the OnPreRender event).


    Monday, November 23, 2009 9:35 AM
  • User-876702696 posted

    Hey - was forced to return to this issue (by our super eager tester!) - and found that this works exactly like a text box.

    So for my drodowns in the solution the text is "Please select" and the InitialValue of my RequiredFirlValidator is "".

    However for the ComboBox (because this is a not a DropDownList - but a textbox nested in an entire table with loads of Javascript hanging off it) - you actually need to set the InitialValue of the RequiredFieldValidator to "Please select" - or whatever the TextField value of your initial Ajax Combobox listitem is.


    Monday, December 14, 2009 11:27 AM
  • User-967933939 posted

    thanks a lot, this worked in my case.

    One more issue I have is, this property

    HighlightCssClass="error-highlight"


     highlights the asp element like textbox when the validation fails, but the same is not working on AJAX elements like calender extender and combobox.

    any way thank you very much for the initial solution.

    Monday, March 3, 2014 8:03 AM