回调示例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlLib
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:CallBackTextBox runat=server></{0}:CallBackTextBox>")]
    public class CallBackTextBox : WebControl,ICallbackEventHandler
    {
        //命名空间.资源文件夹.资源文件名
        public const string JSFile = "ControlLib.Resources.TextBox.js";
       
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        public bool IsValid
        {
            get
            {
                String s = (String)ViewState["IsValid"];
                return ((s == null) ? false : Convert.ToBoolean(s));
            }

            set
            {
                ViewState["IsValid"] = true;
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Input;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "FocusText", "document.getElementById('" + this.ClientID + "').focus();", true);
            string js_file = Page.ClientScript.GetWebResourceUrl(this.GetType(), JSFile);
            Page.ClientScript.RegisterClientScriptInclude("ValidateText", js_file);

        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_div");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.AddAttribute("OnBlur", Page.ClientScript.GetCallbackEventReference(this,Text,"CallBackHandler",null));
        }

        #region ICallbackEventHandler 成员

        public string GetCallbackResult()
        {
            return IsValid.ToString();
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            OnUserNameChanged(EventArgs.Empty);
        }


        public event EventHandler UserNameChanged;
        protected virtual void OnUserNameChanged(EventArgs e)
        {
            if (UserNameChanged != null)
            {
                UserNameChanged(this, e);
            }
        }

        #endregion
    }
}

posted @ 2012-02-23 14:32  玉面麒麟  Views(166)  Comments(0Edit  收藏  举报