Toars I will smile to the world ^o^

4Sep/094

The CustomValidator and the EventHandler ServerValidateEventArgs / Customized TextBox control

This is more like a continuous article to the previous post.

In this article, I am going to explain a bit how to intergreate the CustomValidator and the ServerValidEventArgs event handler into the TextBox control, and after the integration, you don't need to explicitly have a CustomValidator and the ServerValidEventArgs in the Aspx page, all you need to do is to create a ServerValidEventArgs event for the instance of TextBox control and the CustomValidator will be created accordingly automatically by the TextBox control.

In this example, OTextBox is the customized version of TextBox that integrated with a CustomValidator and its ServerValidEventArgs.

Here is the code for: OTextBox.cs

public class OTextBox : System.Web.UI.WebControls.TextBox
{
    // This is the CustomValidator that will be added to the OTextBox instance if its
    // server validate event handler is created by the user.
    private CustomValidator _customValidator;

    // The server validate event handler.
    public event EventHandler CustomServerValidate;

    /// <summary>
    /// Called by the ASP.NET page framework to notify server controls that
    /// use composition-based implementation to create any child controls
    /// they contain in preparation for posting back or rendering.
    /// </summary>
    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        //CustomValidator
        _customValidator = new CustomValidator();
        _customValidator.ID = "CustomValidator" + this.ID;
        _customValidator.ControlToValidate = this.ID;
        _customValidator.Display = ValidatorDisplay.None;
        _customValidator.ErrorMessage = "Error Message for CustomValidator";
        _customValidator.ServerValidate += new ServerValidateEventHandler(_customValidator_SV);

        // Add the CustomValidator to the page
        Controls.Add(_customValidator);
    }

    /// <summary>
    /// Handles the ServerValidate event of the _customValidator control.
    /// </summary>
    /// <param name="source">The source of the event.</param>
    /// <param name="args">The instance containing the event data.</param>
    private void _customValidator_SV(object source, ServerValidateEventArgs args)
    {
        // If this server validate event handler has been created by the user
        if (CustomServerValidate != null)
            CustomServerValidate(source, args);
    }

    /// <summary>
    /// Outputs server control content to a provided  object and stores
    /// tracing information about the control if tracing is enabled.
    /// </summary>
    /// <param name="writer">The object that receives the control content.</param>
    public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
    {
        base.RenderControl(writer);

        // Render the CustomValidator
        if (this.CustomServerValidate != null)
            _customValidator.RenderControl(writer);
    }
}

How to use this customized control? Very simple, here is another example:

Assume that the OTextBox's namespace is Example.Mycontrols, so add the section below to the Web.Config file in order to access the customized control as <uc:ControlName ID="Balala" runat="server" />:

<pages>
    <controls>
        <add tagPrefix="uc" namespace="Example.MyControls" assembly="Example"/>
    </controls>
</pages>

And in Default.aspx page, add the OTextBox instance:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Example.Default" %>
<html>
<head id="Head" runat="server" />
<title></title>
<body>
    <form id="DefaultForm" runat="server">
        <uc:OTextBox ID="TxtExample" runat="server" />
    </form>
</body>
</html>

In Default.aspx.cs code-behind, create the event handler, and associate it with a method:

namespace Example
{
    public partial class Default : System.Web.UI.Page
    {
        /// <summary>
        /// Raises the event to initialize the page.
        /// </summary>
        /// <param name="e">An EventArgs that contains the event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            TxtExample.CustomServerValidate += new EventHandler(TxtExample_CustomServerValidate);
        }

        /// <summary>
        /// Handles the CustomServerValidate event of the TxtEmailAddress control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The instance containing the event data.</param>
        public void TxtExample_CustomServerValidate(object sender, ServerValidateEventArgs e)
        {
            if (Something is OK)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
    }
}

Then it is done, isn't that simple, and you can add some code to this TxtExample_CustomServerValidate method, and it will validate it when Page.Validate() occurs, is that cool?

Thanks!

Comments (4) Trackbacks (0)
  1. insteresting~ i am pushing you to compose this artical promptly… i am expecting it…

  2. impressive~ it really relieves programmer from tedious work

  3. I will finish this work soon.


Leave a comment


No trackbacks yet.