Adding an asynchronous callback to validate data (C#) : Validation « Custom Controls « ASP.NET Tutorial

Home
ASP.NET Tutorial
1.ASP.Net Instroduction
2.Language Basics
3.ASP.net Controls
4.HTML Controls
5.Page Lifecycle
6.Response
7.Collections
8.Validation
9.Development
10.File Directory
11.Sessions
12.Cookie
13.Cache
14.Custom Controls
15.Profile
16.Configuration
17.LINQ
18.ADO.net Database
19.Data Binding
20.Ajax
21.Authentication Authorization
22.I18N
23.Mobile
24.WebPart
25.XML
ASP.NET Tutorial » Custom Controls » Validation 
14.18.1.Adding an asynchronous callback to validate data (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebControlLibrary1
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl, ICallbackEventHandler
    {
        private string text;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Themeable(false)]
        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
            }
        }

        protected override void Render(HtmlTextWriter output)
        {
            output.RenderBeginTag(HtmlTextWriterTag.Div);

            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
            output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);

            output.AddAttribute("OnBlur""ClientCallback();");        
            this.AddAttributesToRender(output);

            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();

            output.RenderEndTag();
        }

        protected override void OnPreRender(EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(
                typeof(Page),
                "ControlFocus""document.getElementById('" this.ClientID + "').focus();",
                true);

            Page.ClientScript.RegisterStartupScript(
                typeof(Page)"ClientCallback",
                "function ClientCallback() {" +
                    "args=document.getElementById('" this.ClientID + "').value;" +
                    Page.ClientScript.GetCallbackEventReference(this, "args",
                        "CallbackHandler", null, "ErrorHandler"true"}",
                true);
        }

        #region ICallbackEventHandler Members

        public void RaiseCallbackEvent(string eventArgument)
        {
            int result;
            if (!Int32.TryParse(eventArgument, out result))
                throw new Exception("The method or operation is not implemented.");
        }

        public string GetCallbackResult()
        {
            return "Valid Data";
        }

        #endregion



    }
}
14.18.Validation
14.18.1.Adding an asynchronous callback to validate data (C#)
14.18.2.Adding an asynchronous callback to validate data (VB)
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.