locked
How to raise Lost Focus event to a CustomTextBox RRS feed

  • Question

  • User-313715986 posted

    Hi everyone,

    I'm created a CustomTextBox and I don't know how to raise Lost Focus event to it, I'm trying to use Leave Event but It's doen't work, here's my code:

    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;
    using System.Collections.Specialized;
    
    namespace ASPServerControl
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:CustomTextBox runat=server></{0}:CustomTextBox>")]
        public class CustomTextBox : WebControl, IPostBackDataHandler
        {
    
            public CustomTextBox() : base(HtmlTextWriterTag.Input) { }
    
            protected override void AddAttributesToRender(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "Text");
                writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
                writer.AddAttribute("Name", this.UniqueID);
    
                base.AddAttributesToRender(writer);
            }
    
    
            //Text Attribute
            [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;
                }
            }
    
            protected override void RenderContents(HtmlTextWriter output)
            {
                output.Write(Text);
            }
    
            public bool LoadPostData(string postDataKey, NameValueCollection postData)
            {
                string newValue = postData[postDataKey];
    
                string oldValue = Text;
    
                if (oldValue != newValue)
                {
                    Text = newValue;
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
    
            public event EventHandler TextUpdated;
    
            public void RaisePostDataChangedEvent()
            {
                OnTextUpdated(new EventArgs());
            }
    
            protected virtual void OnTextUpdated(EventArgs e)
            {
                if (TextUpdated != null)
                {
                    TextUpdated(this, e);
                }
            }
    
        }
    }
    


     

    Saturday, August 29, 2009 1:05 PM

Answers

  • User-2106054853 posted

     Hi,

    You can add onblur event for the control. Assume you want to post back when the control loses focus:

       [DefaultProperty("Text")]
        [ToolboxData("<{0}:CustomTextBox runat=server></{0}:CustomTextBox>")]
        public class CustomTextBox : WebControl, IPostBackDataHandler
        {
            protected override void OnPreRender(EventArgs e)
            {
                this.Attributes.Add("onblur",Page.ClientScript.GetPostBackClientHyperlink(this, string.Empty));
                base.OnPreRender(e);
            }
            public CustomTextBox() : base(HtmlTextWriterTag.Input) { }

            protected override void AddAttributesToRender(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Type, "Text");
                writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
                writer.AddAttribute("Name", this.UniqueID);

                base.AddAttributesToRender(writer);
            }


            //Text Attribute  
            [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;
                }
            }

            protected override void RenderContents(HtmlTextWriter output)
            {
                output.Write(Text);
            }

            public bool LoadPostData(string postDataKey, NameValueCollection postData)
            {
                string newValue = postData[postDataKey];

                string oldValue = Text;

                if (oldValue != newValue)
                {
                    Text = newValue;
                    return true;
                }
                else
                {
                    return false;
                }
            }


            public event EventHandler TextUpdated;

            public void RaisePostDataChangedEvent()
            {
                OnTextUpdated(new EventArgs());
            }

            protected virtual void OnTextUpdated(EventArgs e)
            {
                if (TextUpdated != null)
                {
                    TextUpdated(this, e);
                }
            }

        }  

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 30, 2009 11:52 PM