Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have defined class customtextbox inherits webcontol and implement IPostBackDataHandler, it contain all properties and render method.
I have wired the OnTextChanged event and wired it to my custom control like this:
C#
Page.Page.RegisterRequiresPostBack(CustomTextBox1);
Is this a correct way to call?
My doubt is that the parameter inside the method represent correct way or should I change to anything else.

I am not getting my posted back data in text box to my custom page.

My aspx page:
C#
protected override void OnInit(EventArgs e)
   {
       base.OnInit(e);
       if (Page != null)
       {
           //Page.RegisterRequiresPostBack(CustomTextBox1);
           Page.RegisterRequiresRaiseEvent(CustomImageButton1);
       }
   }
   protected void Page_Load(object sender, EventArgs e)
   {

   }

My custom sever control page:
C#
public class CustomTextBox : WebControl,IPostBackDataHandler
    {
     
     public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return s;
            }

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

        public CustomTextBox() : base(HtmlTextWriterTag.Input)
        {
            
            Text = " ";
        }

        public event EventHandler TextChanged;
        protected override void RenderContents(HtmlTextWriter output)
        {
            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.Value, Text);
            output.AddAttribute("name", this.UniqueID);
            base.AddAttributesToRender(output);
            
        }
        public bool LoadPostData(string postDataKey, NameValueCollection postData)
        {
            // Get the posted value and the most recent view state value.
            string postedValue = postData[postDataKey];
            string s = postData[0];
            string viewstateValue = Text;
            // If the value changed, then reset the value of the text property
            // and return true so the RaisePostDataChangedEvent will be fired.
            if (viewstateValue != postedValue)
            {
                Text = postedValue;
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public void RaisePostDataChangedEvent() 
        {
            // Call the method to raise the change event.
            OnTextChanged(new EventArgs());
        }
        protected virtual void OnTextChanged(EventArgs e)
        {
            // Check for at least one listener, and then raise the event.
            if (TextChanged != null)
                TextChanged(this, e);
           
        }

Appreciation in advance
Posted
Updated 19-Nov-11 0:05am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900