ASP.NET Tutorial/Custom Controls/Validation

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Adding an asynchronous callback to validate data (C#)

using System;
using System.Collections.Generic;
using System.ruponentModel;
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

    }
}


Adding an asynchronous callback to validate data (VB)

Imports System.ruponentModel
Imports System.Web.UI
<DefaultProperty("Text"), _
 ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")> _
Public Class WebCustomControl1
    Inherits System.Web.UI.WebControls.WebControl
    Implements System.Web.UI.ICallbackEventHandler
    Dim _text As String
    <Bindable(True), Category("Appearance"), Themeable(False), DefaultValue("")> _
    Property [Text]() As String
        Get
            Return _text
        End Get
        Set(ByVal Value As String)
            _text = Value
        End Set
    End Property

    Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
        output.RenderBeginTag(HtmlTextWriterTag.Div)
        output.AddAttribute(HtmlTextWriterAttribute.Type, "text")
        output.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
        output.AddAttribute(HtmlTextWriterAttribute.Name, Me.ClientID)
        output.AddAttribute(HtmlTextWriterAttribute.Value, Me.Text)
        output.AddAttribute("OnBlur", "ClientCallback();")
        Me.AddAttributesToRender(output)
        output.RenderBeginTag(HtmlTextWriterTag.Input)
        output.RenderEndTag()
        output.RenderEndTag()
    End Sub
    Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
        Page.ClientScript.RegisterStartupScript(GetType(Page), _
           "ControlFocus", "document.getElementById("" & Me.ClientID & "").focus();", _
           True)
        Page.ClientScript.RegisterStartupScript( _
            GetType(Page), "ClientCallback", _
            "function ClientCallback() {" & _
                "args=document.getElementById("" & Me.ClientID & "").value;" & _
                Page.ClientScript.GetCallbackEventReference(Me, "args", _
                    "CallbackHandler", Nothing, "ErrorHandler", True) + "}", _
            True)
    End Sub

    Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
        Dim result As Int32
        If (Not Int32.TryParse(eventArgument, result)) Then
            Throw New Exception("The method or operation is not implemented.")
        End If
    End Sub
    Public Function GetCallbackResult() As String Implements ICallbackEventHandler.GetCallbackResult
        Return "Valid Data"
    End Function
End Class