<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Toars &#187; CustomValidator</title>
	<atom:link href="http://www.toars.com/tag/customvalidator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.toars.com</link>
	<description>I will smile to the world ^o^</description>
	<lastBuildDate>Fri, 06 May 2011 08:50:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>The CustomValidator and the EventHandler ServerValidateEventArgs / Customized TextBox control</title>
		<link>http://www.toars.com/2009/09/the-customvalidator-and-the-eventhandler-servervalidateeventargs/</link>
		<comments>http://www.toars.com/2009/09/the-customvalidator-and-the-eventhandler-servervalidateeventargs/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 08:53:38 +0000</pubDate>
		<dc:creator>Xin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CustomValidator]]></category>
		<category><![CDATA[EventHandler]]></category>
		<category><![CDATA[ServerValidateEventArgs]]></category>
		<category><![CDATA[Validator]]></category>

		<guid isPermaLink="false">http://www.toars.com/?p=225</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>This is more like a continuous article to the previous post.</p>
<p>In this article, I am going to explain a bit how to intergreate the <strong>CustomValidator </strong>and the <strong>ServerValidEventArgs </strong>event handler into the <strong>TextBox </strong>control, and after the integration, you don't need to explicitly have a <strong>CustomValidator </strong>and the <strong>ServerValidEventArgs </strong>in the Aspx page, all you need to do is to create a <strong>ServerValidEventArgs </strong>event for the instance of <strong>TextBox </strong>control and the <strong>CustomValidator </strong>will be created accordingly automatically by the <strong>TextBox </strong>control.</p>
<p>In this example, <strong>OTextBox</strong> is the customized version of <strong>TextBox </strong>that integrated with a <strong>CustomValidator </strong>and its <strong>ServerValidEventArgs</strong>.</p>
<p>Here is the code for: <strong>OTextBox.cs</strong></p>
<pre>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;

    /// &lt;summary&gt;
    /// 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.
    /// &lt;/summary&gt;
    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);
    }

    /// &lt;summary&gt;
    /// Handles the ServerValidate event of the _customValidator control.
    /// &lt;/summary&gt;
    /// &lt;param name="source"&gt;The source of the event.&lt;/param&gt;
    /// &lt;param name="args"&gt;The instance containing the event data.&lt;/param&gt;
    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);
    }

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

        // Render the CustomValidator
        if (this.CustomServerValidate != null)
            _customValidator.RenderControl(writer);
    }
}</pre>
<p><strong>How to use this customized control? </strong>Very simple, here is another example:</p>
<p>Assume that the <strong>OTextBox</strong>'s namespace is <strong>Example.Mycontrols</strong>, so add the section below to the <strong>Web.Config</strong> file in order to access the customized control as &lt;uc:ControlName ID="Balala" runat="server" /&gt;:</p>
<pre>&lt;pages&gt;
    &lt;controls&gt;
        &lt;add tagPrefix="uc" namespace="Example.MyControls" assembly="Example"/&gt;
    &lt;/controls&gt;
&lt;/pages&gt;</pre>
<p>And in <strong>Default.aspx</strong> page, add the OTextBox instance:</p>
<pre>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Example.Default" %&gt;
&lt;html&gt;
&lt;head id="Head" runat="server" /&gt;
&lt;title&gt;&lt;/title&gt;
&lt;body&gt;
    &lt;form id="DefaultForm" runat="server"&gt;
        &lt;uc:OTextBox ID="TxtExample" runat="server" /&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>In <strong>Default.aspx.cs</strong> code-behind, create the event handler, and associate it with a method:</p>
<pre>namespace Example
{
    public partial class Default : System.Web.UI.Page
    {
        /// &lt;summary&gt;
        /// Raises the event to initialize the page.
        /// &lt;/summary&gt;
        /// &lt;param name="e"&gt;An EventArgs that contains the event data.&lt;/param&gt;
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            TxtExample.CustomServerValidate += new EventHandler(TxtExample_CustomServerValidate);
        }

        /// &lt;summary&gt;
        /// Handles the CustomServerValidate event of the TxtEmailAddress control.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The source of the event.&lt;/param&gt;
        /// &lt;param name="e"&gt;The instance containing the event data.&lt;/param&gt;
        public void TxtExample_CustomServerValidate(object sender, ServerValidateEventArgs e)
        {
            if (Something is OK)
                e.IsValid = true;
            else
                e.IsValid = false;
        }
    }
}</pre>
<p>Then it is done, isn't that simple, and you can add some code to this <strong>TxtExample_CustomServerValidate</strong> method, and it will validate it when <strong>Page.Validate()</strong> occurs, is that cool?</p>
<p>Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.toars.com/2009/09/the-customvalidator-and-the-eventhandler-servervalidateeventargs/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

