This is a migrated thread and some comments may be shown as answers.

ASP.NET Validation with Notification

1 Answer 67 Views
Notification
This is a migrated thread and some comments may be shown as answers.
David Rhodes
Top achievements
Rank 1
David Rhodes asked on 12 Aug 2013, 01:12 PM
I needed to get validation working with the Notification control so thought i'd share this code, it's taken from http://demos.telerik.com/aspnet-ajax/notification/examples/waiariasupport/defaultcs.aspx but modified to allow multiple ValidationGroups per page

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

<telerik:RadNotification
    ID="ValidationNotification"
    runat="server"
    AutoCloseDelay="20000"
    EnableRoundedCorners="true"
    EnableShadow="true"
    KeepOnMouseOver="true"
    Position="Center"
    Title="Validation errors"
    TitleIcon="warning"
    ShowCloseButton="true"
    Width="350">
    <NotificationMenu Visible="false" />
    <ContentTemplate>
        <asp:ValidationSummary
            ID="ValidationSummary"
            runat="server"
            DisplayMode="BulletList"
            ShowMessageBox="false"
            ShowSummary="true" />
    </ContentTemplate>
</telerik:RadNotification>

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 () { notification.show(); }, 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 = summaryElem.style.display == 'none';");
        js.Append("                 args.set_cancel(noErrors);");
        js.Append("             }");
 
        ScriptManager.RegisterClientScriptBlock(Page, this.GetType(),  ValidationNotification.ClientID, js.ToString(), true);
 
        base.OnInit(e);
    }
}


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;









1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 13 Aug 2013, 12:19 PM
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: http://www.telerik.com/community/code-library/aspnet-ajax/notification.aspx. This will make your contribution easier to find and more useful to the other community members.


Best regards,
Marin Bratanov
Telerik
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.
Tags
Notification
Asked by
David Rhodes
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or