ASP.NET Validation with Notification

2 posts, 0 answers
  1. David Rhodes
    David Rhodes avatar
    65 posts
    Member since:
    May 2005

    Posted 12 Aug 2013 Link to this post

    I needed to get validation working with the Notification control so thought i'd share this code, it's taken from but modified to allow multiple ValidationGroups per page

    Simply create a UserControl called Validation.ascx and add this markup

        Title="Validation errors"
        <NotificationMenu Visible="false" />
                ShowSummary="true" />

    and this code-behind

    /// <summary>
    /// Provides a ValidationSummary inside a Telerik Notification
    /// </summary>
    /// <example>
    /// Add the controls to the page
    /// <code><uc1:Validation runat="server" id="Validation" ValidationGroup="Activate" /></code>
    /// Attach the validation function to the button which fires the validation in OnInit
    /// <code>ActivateButton.OnClientClicked = Validation.ValidationJsFunction;</code>
    /// Any Server-Side validation function will need to show the notification
    /// <code>if (!valid)   {   Validation.ValidationNotificationControl.Show();    }</code>
    /// </example>
    public partial class Validation : System.Web.UI.UserControl
        public string ValidationGroup { get; set; }
        public RadNotification ValidationNotificationControl { get { return this.ValidationNotification; } }
        public string ValidationJsFunction { get { return string.Format("Show_{0}", ValidationNotification.ClientID); } }
        protected void Page_Load(object sender, EventArgs e) { }
        protected override void OnInit(EventArgs e)
            ValidationSummary.ValidationGroup = ValidationGroup;
            ValidationNotification.OnClientShowing = string.Format("Check_{0}", ValidationSummary.ClientID);
            StringBuilder js = new StringBuilder();
            // the Show function needs to be added to each button which causes validation
            js.AppendFormat("       function Show_{0}()", ValidationNotification.ClientID);
            js.Append("             {");
            js.AppendFormat("           var notification = $find('{0}');", ValidationNotification.ClientID);
            js.Append("                 setTimeout(function () {; }, 0);");
            js.Append("             }");
            js.AppendFormat("       function Check_{0}(sender, args)", ValidationSummary.ClientID);
            js.Append("             {");
            js.AppendFormat("           var summaryElem = document.getElementById('{0}');", ValidationSummary.ClientID);
            js.Append("                 var noErrors = == 'none';");
            js.Append("                 args.set_cancel(noErrors);");
            js.Append("             }");
            ScriptManager.RegisterClientScriptBlock(Page, this.GetType(),  ValidationNotification.ClientID, js.ToString(), true);

    Now add the control to your page in place of a ValidationSummary

    <uc1:Validation runat="server" ID="Validation" ValidationGroup="Subscription" />

    You then need to attach the onclick event of the button firing validation to Validation.ValidationJsFunction eg

    GetQuoteButton.OnClientClicked = Validation.ValidationJsFunction;

  2. Marin Bratanov
    Marin Bratanov avatar
    4441 posts

    Posted 13 Aug 2013 Link to this post

    Hi David,

    I am glad to hear the demos and the control helped you. If you create a runnable project and post it in the code library section of the RadNotification with a detailed description I will gladly award your efforts with some Telerik points: This will make your contribution easier to find and more useful to the other community members.

    Best regards,
    Marin Bratanov
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Back to Top