Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > General Discussions > RadButton required field validator
RadControls for ASP.NET are no longer supported (see this page for reference). In case you have inquiries about the Telerik ASP.NET AJAX controls, post them in the pertinent ASP.NET AJAX forums.

Not answered RadButton required field validator

Feed from this thread
  • Andreas avatar

    Posted on Jun 23, 2011 (permalink)

    Hi there,

    I have an issue with an RadButton as a RadioBox. 

    I have two RadButtons in a Group (GroupName="Time"). Both are RadioButtons. Initially no one is checked. It is required that one of them will be checked. It looks like I can't use an RequiredFieldValidator.

    How I can validate this buttons?

    Thanks for your help,
    Andreas.

  • Slav Slav admin's avatar

    Posted on Jun 23, 2011 (permalink)

    Hello Andreas,

    You can write your own custom client-side function, which validates the buttons based on their state. I have made scenario, similar to the case you described.
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
     
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <script type="text/javascript">
                function CheckRadios(sender, args) {
     
                    var radionButton1 = $find("<%= RadButton1.ClientID %>");
                    var radionButton2 = $find("<%= RadButton2.ClientID %>");
                    // get reference to clinet-side objects of the radio buttons
     
                    var messageHolder = document.getElementById('ErrorMessage');
                    // get reference to the object containing the message
     
                    if (!radionButton1.get_checked() && !radionButton2.get_checked()) { // see if radionButton1 and radionButton2 are both unchecked
                        messageHolder.innerHTML = "Choose something.";
                        messageHolder.style.color = "Red";
                        // set the text and color of the message
     
                        args.set_cancel(true);
                        // click event canceled, which stops postback
                    }
                }
            </script>
            <telerik:RadButton ID="RadButton1" runat="server" Text="Choise 1" ButtonType="ToggleButton"
                ToggleType="Radio" GroupName="Time" AutoPostBack="false">
            </telerik:RadButton>
            <telerik:RadButton ID="RadButton2" runat="server" Text="Choise 2" ButtonType="ToggleButton"
                ToggleType="Radio" GroupName="Time" AutoPostBack="false">
            </telerik:RadButton>
            <br />
            <span id="ErrorMessage" class="message"></span>
            <br />
            <telerik:RadButton ID="RadButton3" runat="server" Text="Submit" OnClientClicking="CheckRadios">
            </telerik:RadButton>
        </div>
        </form>
    </body>
    </html>

    In addition to the two RadioButtons, a new RadButton, which triggers Postback, is added. In the client-side handler of RadButton's event OnClientClicking, the state of the radio buttons is checked. If both controls are in unchecked state a warning message is displayed and the event is canceled, meaning that Postback doesn't occur.

    You can find more information about RadButton's client-side basis here and description of the used event here.

    Best wishes,
    Slav
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > General Discussions > RadButton required field validator