Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:49:57+00:00 2026-05-30T21:49:57+00:00

I am creating a custom server control to generate button elements with specific markup

  • 0

I am creating a custom server control to generate button elements with specific markup and JavaScript handlers for my Web Forms application. They are, of course, able to cause postbacks, so I would like them to function with any of ASP’s validation controls for form validation, especially the client-side framework.

This button server control supports an OnClientClick property to emit an onclick attribute in the button tag with the code provided (primarily used for a simple confirmation reprompt when a user clicks a delete button for a list view or similar), so using the asp:Button control’s method of emitting the validation script as an onclick attribute will be pretty ineffectual. As a matter of fact, specifying both OnClientClick and ValidationGroup attributes on a standard asp:Button turns out pretty badly. Here’s a painfully obvious example of why that’s not working out of the box:

Page Markup

<asp:Button ID="btnSaveAsp" ValidationGroup="vgMyValidationGroup" OnClientClick="return true;" runat="server" />

Rendered Markup

<input type="submit" name="ctl00$cphBodyContent$lvMyList$ctrl0$btnSaveAsp" value="Save"  id="cphBodyContent_lvUsers_btnSaveAsp_0"
    onclick='return true; WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$cphBodyContent$lvMyList$ctrl0$btnSaveAsp", "", true, "vgMyValidationGroup", "", false, false))'>

Here is the existing non-working code for wiring up the control with validation. I was unable to find much documentation on how best to accomplish this with a method aside from emitting a similar onclick attribute. I thought my call to Page.ClientSCript.RegisterForEventValidation in the overridden AddAttributesToRender method would wire up the client-side validation, but that does not appear to be functioning as I assumed. If necessary, jQuery is available for use in binding additional handling to the button’s click event:

Custom Server Button Control

<ToolboxData("<{0}:Button runat=server></{0}:Button>")> _
<ParseChildren(False)> _
<PersistChildren(True)> _
Public Class Button
    Inherits System.Web.UI.WebControls.WebControl
    Implements IPostBackDataHandler

    Public Sub New()
        MyBase.New(HtmlTextWriterTag.Button)
    End Sub

    <Category("Behavior")> _
    <DefaultValue("")> _
    Public Overridable Property PostBackUrl As String
        Get
            Return If(ViewState("PostBackUrl"), String.Empty)
        End Get
        Set(value As String)
            ViewState("PostBackUrl") = value
        End Set
    End Property

    <Category("Validation")> _
    <DefaultValue(True)> _
    Public Overridable Property CausesValidation As Boolean
        Get
            Return If(ViewState("CausesValidation"), True)
        End Get
        Set(value As Boolean)
            ViewState("CausesValidation") = value
        End Set
    End Property

    <Category("Validation")> _
    <DefaultValue("")> _
    Public Overridable Property ValidationGroup As String
        Get
            Return If(ViewState("ValidationGroup"), String.Empty)
        End Get
        Set(value As String)
            ViewState("ValidationGroup") = value
        End Set
    End Property

    <Category("Behavior")> _
    <DefaultValue("")> _
    <Description("Client-side script to be run when the button is clicked.")> _
    Public Property OnClientClick As String
        Get
            Return If(ViewState("OnClientClick"), String.Empty)
        End Get
        Set(value As String)
            ViewState("OnClientClick") = value
        End Set
    End Property

    Protected Overrides Sub AddAttributesToRender(writer As HtmlTextWriter)
        MyBase.AddAttributesToRender(writer)

        If Not String.IsNullOrEmpty(OnClientClick) Then
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, OnClientClick)
        End If

        Dim postBackOptions = GetPostBackOptions()

        If postBackOptions.TargetControl Is Me Then
            writer.AddAttribute(HtmlTextWriterAttribute.Name, ClientID)
        End If

        If Page IsNot Nothing Then
            Page.ClientScript.RegisterForEventValidation(postBackOptions)
        End If
    End Sub

    Protected Overridable Function GetPostBackOptions() As PostBackOptions
        Dim options As New PostBackOptions(Me) With {
            .ClientSubmit = False
        }

        If Page IsNot Nothing Then
            If CausesValidation AndAlso (Page.GetValidators(ValidationGroup).Count > 0) Then
                options.PerformValidation = True
                options.ValidationGroup = ValidationGroup
            End If

            If Not String.IsNullOrEmpty(PostBackUrl) Then
                options.ActionUrl = HttpUtility.UrlPathEncode(ResolveClientUrl(PostBackUrl))
            End If
        End If

        Return options
    End Function
End Class

Presently, this code does not function with an asp:CompareValidator in the same ValidationGroup to determine if two password reset fields are equal before posting back to the server, nor does validation occur once the request gets to the server side.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:49:58+00:00Added an answer on May 30, 2026 at 9:49 pm

    Making OnClientClick work with client-side form validation

    Since the <asp:Button> control concatenates the value of OnClientClick with the form validation script, the easiest way to make them work together is to return false when you want to block the form submission, and do nothing if you want the button to validate and submit the form:

    OnClientClick="if (!confirm('Are you sure?')) return false;"
    

    However, if you absolutely want to write return confirm('Are you sure?'), then you can move the form validation code to an event listener (as you suggest), or you can wrap the OnClientClick code like this:

    writer.AddAttribute(
        HtmlTextWriterAttribute.Onclick,
        "if (!(function() { " + this.OnClientClick + "; return true; })()) return false;" +
        this.Page.ClientScript.GetPostBackEventReference(options, false));
    

    Server-side form validation

    You need to implement the IPostBackEventHandler interface and call the Page.Validate method. The ClientScriptManager.RegisterForEventValidation method is used for event validation (preventing unauthorized or malicious postbacks), not for form validation.

    Sample code (C#)

    Here is the code for a bare-bones custom button control that supports OnClientClick and ValidationGroup:

    [ParseChildren(false)]
    [PersistChildren(true)]
    public class Button : WebControl, IPostBackEventHandler
    {
        private static readonly object EventClick = new object();
    
        public Button()
            : base(HtmlTextWriterTag.Button)
        {
        }
    
        public bool CausesValidation
        {
            get { return ((bool?)this.ViewState["CausesValidation"]) ?? true; }
            set { this.ViewState["CausesValidation"] = value; }
        }
    
        public string ValidationGroup
        {
            get { return (string)this.ViewState["ValidationGroup"] ?? ""; }
            set { this.ViewState["ValidationGroup"] = value; }
        }
    
        public string OnClientClick
        {
            get { return (string)this.ViewState["OnClientClick"] ?? ""; }
            set { this.ViewState["OnClientClick"] = value; }
        }
    
        public event EventHandler Click
        {
            add { this.Events.AddHandler(EventClick, value); }
            remove { this.Events.RemoveHandler(EventClick, value); }
        }
    
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
    
            if (this.Page != null && this.Enabled)
            {
                PostBackOptions options = this.GetPostBackOptions();
                writer.AddAttribute(
                    HtmlTextWriterAttribute.Onclick,
                    this.OnClientClick + this.Page.ClientScript.GetPostBackEventReference(options, false));
            }
        }
    
        protected virtual PostBackOptions GetPostBackOptions()
        {
            PostBackOptions options = new PostBackOptions(this) { ClientSubmit = false };
    
            if (this.Page != null)
            {
                if (this.CausesValidation && this.Page.GetValidators(this.ValidationGroup).Count > 0)
                {
                    options.PerformValidation = true;
                    options.ValidationGroup = this.ValidationGroup;
                }
            }
    
            return options;
        }
    
        protected virtual void OnClick(EventArgs e)
        {
            EventHandler handler = (EventHandler)this.Events[EventClick];
    
            if (handler != null)
            {
                handler(this, e);
            }
        }
    
        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
        {
            if (this.CausesValidation)
            {
                this.Page.Validate(this.ValidationGroup);
            }
    
            this.OnClick(EventArgs.Empty);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a custom ASP.NET AJAX server control in which multiple instances of
I am creating tree with some custom control prepared with JavaScript/jquery. For creating the
I am creating a custom server control with a property that accepts a String
We are looking at creating a custom ASP.NET application for a client, however they
I am creating a custom .NET AJAX Server Control, and need to get access
I'm creating a custom server control in ASP.NET WebForms and want to have a
I'm creating a server/client solution with custom user logins (not WindowsIdentity). I need a
I am creating custom membership provider for my asp.net application. I have also created
I am creating an ASP.NET custom control. I want to set the body onload
I have a question about creating and managing events inside an ascx custom web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.