ASP.NET RequiredFieldValidator

5 posts, 1 answers
  1. Hadi Teo
    Hadi Teo avatar
    23 posts
    Member since:
    Jan 2008

    Posted 19 Apr 2009 Link to this post


    Here is my simple scenario :  I have one ASP.NET textbox, one ASP.NET requiredfieldvalidator that validate the ASP.NET textbox and two link Telerik RadComboBox and finally one ASP.NET submit button

    My requirement is to make those two RadComboBox link each other by using the SelectedValue of the first combo box, in order to populate the other combo box.

    I can achieve this by using RadAjax and configure AllowAutoPostBack of the first combo box to True, but the problem is , it cannot fire the server side event because the .ASP.NET requiredfieldvalidator perform a check at the client side.

    The questions are :

    1) Can i set the RadComboBox to invoke the server side event without performing full postback ? 
    2) Can anybody recommend what is the correct control to achieve my business scenario ? or if i change to RadTextBox, is there any validation that i can hook to RadTextBox, but after integrating it with the RadComboBox and RadAjax, will it give an issue ?



    After carefully
  2. Tsvetoslav
    Tsvetoslav avatar
    1823 posts

    Posted 23 Apr 2009 Link to this post

    Hello Hadi,

    The easiest way to make the combo do a postback inspite of the validator is to set the former's CausesValidation property to false.

    Alternatively, you can attach an event handler to the combo's OnClientSelectedIndexChanged event and make an ajax request through a RadAjaxManager control:

                 <telerik:RadCodeBlock ID="CodeBlock" runat="server">  
                    <script type="text/javascript">  
                        function ComboClientSelectedIndexChanged(sender, args)  

    I hope this information helps.

    Best Regards,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft R3 2016 release webinar banner
  4. Answer
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 23 Apr 2009 Link to this post

    Hi Hadi,

    what Tsvetoslav wrote works well for simple scenarios.

    I would recommend you to use "ValidationGroup" - this enables you to achieve callbacks / postbacks even in complex situations without the need to write extra code.
    About your first question - you can :)
    And about your second question - of course there are always more possible solutions to one problem.
    And your business need is not declared very precise -- but from what you writ - yes this should work.

    Let me explain what I think (that you want):
    1.) Combobox firing an event to the server
    ---this event binds some data to a second combobox
    2.) a TextBox which should be validated
    3.) a button which should only fire if the TextBox has a value.
    4.) the event (point 1) should be done without a postback

    So here a possible solution - I have added an extra TextBox (next to the combobox) to show you the strength of ValidationGroups.
    You must type something into that TextBox before you are able to fire the first combobox.
    --for your approach - remove the TextBox and it's validator.
    Next the combobox - it fire a callback to there server.
    There I bind the second combobox - depending on the selection of the first one.

    A second TextBox which has to be filled that the button can fire.
    BUT - combo can fire even if this TextBox is empty.
    First serverside:
    protected void cbOne_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e) {  
        txtErg.Text = e.Text;  
        List<string> lS = new List<string>();  
        lS.Add(e.Text + " aa");  
        lS.Add(e.Text + " bb");  
        lS.Add(e.Text + " cc");  
        cbTwo.DataSource = lS;  
    protected void btnSend_Click(object sender, EventArgs e) {  
        txtErg.Text = "button clicked";  
    Next the ASPX controls:
        <telerik:RadComboBox ID="cbOne" runat="server" AutoPostBack="True" onselectedindexchanged="cbOne_SelectedIndexChanged" ValidationGroup="doComboThings">  
        <telerik:RadComboBoxItem Text="One" />  
        <telerik:RadComboBoxItem Text="Two" />  
        <telerik:RadComboBoxItem Text="Three" />  
        Type Text to enable combo postback:<asp:TextBox ID="txtCheckedByCombo" runat="server" /><asp:RequiredFieldValidator runat="server" ControlToValidate="txtCheckedByCombo" ErrorMessage="Type Text to enable combo psotbacks" ValidationGroup="doComboThings" /><br /><br />  
        Binding from server: <telerik:RadComboBox ID="cbTwo" runat="server"  ></telerik:RadComboBox><br />  
        <asp:TextBox ID="txtChecked2" runat="server" /><asp:RequiredFieldValidator runat="server" ControlToValidate="txtChecked2" ErrorMessage="Type Text" /><br />  
        <asp:Button runat="server" ID="btnSend" Text="Send" onclick="btnSend_Click" /><br />  
        <br />  
        Result from Server: <asp:TextBox ID="txtErg" runat="server" /><br />  
    Please notice that cbOne has ValidationGroup set to doComboThings - as well as txtCheckedByCombo's Validator has.
    So a postback does not work as long as there is no text in txtCheckedByCombo.

    Next there is cbTwo which gets filled by code behind.

    After the empty line you see txtChecked2 (no ValidationGroup) and btnSend (also no ValidationGroup).
    Think of "no ValidationGroup" as an own ValidationGroup - so all controls with no VG belong together in validation.

    Now about - Combo selection without full postback - here comes RadAjax in (I use manager - there are panels also):
    <form id="form1" runat="server">  
    <telerik:RadScriptManager runat="server">  
    <telerik:RadAjaxManager runat="server">  
            <telerik:AjaxSetting AjaxControlID="cbOne">  
                    <telerik:AjaxUpdatedControl ControlID="txtErg" />  
                    <telerik:AjaxUpdatedControl ControlID="cbTwo" />  
    <telerik:RadComboBox ID="cbOne" runat="server" AutoPostBack="True" onselectedindexchanged="cbOne_SelectedIndexChanged" ValidationGroup="doComboThings">  
    I double posted the combobox to show where this thing could be in the page (I prefer it on top).
    The interesting thing here - is AjaxSetting - this tells ajax the control which does a call to the server.
    Inside it is the "UpdatedControls Section" this holds a list of the affected controls.
    In this case a selection in cbOne (with Autopostback) alters the content of cbTwo (the second combobox) as well as txtErg where I write some text.

    A last thing to note: the button does a full postback (it's not listed in the ajaxmanager) while cbOne does only a callback (only affected controls rerendered).


  5. Hadi Teo
    Hadi Teo avatar
    23 posts
    Member since:
    Jan 2008

    Posted 27 Apr 2009 Link to this post

    Hi Manfred,

    Thank you very much for your details explanation. At the end, i didn't use any ASP.NET validation and do a manual validation by using javascript.

    For the linked combo box, i have used the AjaxRequest as your recommendation and it's working well.


    hadi teo
  6. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 28 Apr 2009 Link to this post

    Hi hadi teo,

    glad to hear that I could help.
    In most (even complex scenarios like multipage - multiple groups of controls, wizards and so on) cases I prefer the ValidationGroup method. It is simple to maintain (add a control - no problem - move a control also easily done).

    Scripts (from my point of view) are very useful when the things are "living".
    Example - a user has to enter his firstname if he sets the checkbox "advanced user" - in other cases the input is optional.
    These are the things where I take scripts - or if useful callbacks to change validation.

    Anyhow - your solutions work that's what counts. And the best solution is always the one which works for the needs you have.


Back to Top
DevCraft R3 2016 release webinar banner