Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000776
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:49:24+00:00 2026-05-20T07:49:24+00:00

I’m reading through some MSDN example code on Composite controls and finding that there

  • 0

I’m reading through some MSDN example code on Composite controls and finding that there is one thing I don’t understand. Using the code here I’ve created the LabelTextBox control in my project and added it to a page. If I enter a new value into the textbox in the control, the button click event has the old value when I post back to the server. If I allow the page to continue processing the page finishes reloading with the new value though. Is this a poor example or am I missing something? Shouldn’t the changed value be loaded into the control before the button event fires?

The composite control code:

public class LabelTextBox : WebControl, INamingContainer
{
    public string Text
    {
        get
        {
            object o = ViewState["Text"];
            if (o == null)
                return String.Empty;
            return (string)o;
        }
        set { ViewState["Text"] = value; }
    }
    public string Title
    {
        get
        {
            object o = ViewState["Title"];
            if (o == null)
                return String.Empty;
            return (string)o;
        }
        set { ViewState["Title"] = value; }
    }
    protected override void CreateChildControls()
    {
        Controls.Clear();
        CreateControlHierarchy();
        ClearChildViewState();
    }
    protected virtual void CreateControlHierarchy()
    {
        TextBox t = new TextBox();
        Label l = new Label();
        t.Text = Text;
        l.Text = Title;
        Controls.Add(l);
        Controls.Add(t);
    }
}

I’ve tried creating the control declaratively and programatically with no change in the results. The programatic creation code:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Form.Controls.Add(new LiteralControl("<br /><br />"));

        TextBox tb = new TextBox();
        tb.Text = "before";
        Form.Controls.Add(tb);

        Form.Controls.Add(new LiteralControl("<br /><br />"));

        LabelTextBox ltb = new LabelTextBox();
        ltb.Title = "title";
        ltb.Text = "before";
        Form.Controls.Add(ltb);


    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach( var c in Form.Controls)
        {
            var control = c as TextBox;
            if (control != null) 
            {
                System.Diagnostics.Debug.WriteLine(String.Format("TextBox: {0}", control.Text)); 
            }
            else
            {
                var control2 = c as LabelTextBox;
                if (control2 != null)
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("LabelTextBox: {0}", control2.Text));
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:49:25+00:00Added an answer on May 20, 2026 at 7:49 am

    I think the problem with your first approach is that you are keeping the values in the parent’s Viewstate, but never reading the values back in from the children. Try something like:

    public class LabelTextBox : WebControl, INamingContainer
    {
        private Label _Label     = new Label() { ID="uxLbl" };
        private TextBox _TextBox = new TextBox() { ID="uxTb" };
    
        public string Text
        {
            get { return _TextBox.Text; }
            set { _TextBox.Text = value; }
        }
        public string Title
        {
            get { return _Label.Text; }
            set { _Label.Text = value; }
        }
        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();
        }
        protected virtual void CreateControlHierarchy()
        {
            Controls.Add(_Label);
            Controls.Add(_TextBox);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.