locked
Custom Server Control RRS feed

  • Question

  • User-2026955127 posted
    Experienced members,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><o:p> </o:p>It’s been quite some time since I have developed custom server controls and am trying to refresh myself. Even in the past I haven’t developed very complex server controls. But this time, I am trying to tackle a somewhat simple one and although I can get the runtime part to work. I am struggling with the design time support. <o:p></o:p><o:p> </o:p>Goal: <o:p></o:p>To create a server control that includes the following;<o:p></o:p><o:p> </o:p>1.       Top level panel<o:p></o:p>a.       Name textbox<o:p></o:p>b.       Email textbox<o:p></o:p>c.       Submit button<o:p></o:p><o:p> </o:p>All three controls inside the panel.<o:p></o:p><o:p> </o:p>Current Code:<o:p></o:p>Created a custom server control that inherits the Panel control and override the CreateChildControls() method.<o:p></o:p><o:p> </o:p>Current Outcome:<o:p></o:p>This works flawless during runtime, but all I see is main Panel box at design time. How can I enable the rendering of the child controls of the panel at design time?<o:p></o:p><o:p> </o:p>See code below;<o:p></o:p><o:p> </o:p><o:p> </o:p><o:p>
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Example.Web.UI.Controls
    {
    	[ToolboxData("&lt;{0}:FTAF runat=server></{0}:FTAF>")]
    	public class ForwardToAFreind : Panel, INamingContainer
    	{
    		private TextBox _nameTextBox = new TextBox();
    		private TextBox _emailTextBox = new TextBox();
    		private TextBox _emailListTextBox = new TextBox();
    		private Button _submitButton = new Button();
    
    		private Label _nameLabel = new Label();
    		private Label _emailLabel = new Label();
    		private Label _emailListLabel = new Label();
    
    		[Browsable(true)]
    		public TextBox NameTextBox
    		{
    			get { return _nameTextBox; }
    			set { _nameTextBox = value; }
    		}
    		[Browsable(true)]
    		public TextBox EmailTextBox
    		{
    			get { return _emailTextBox; }
    			set { _emailTextBox = value; }
    		}
    		[Browsable(true)]
    		public TextBox EmailListTextBox
    		{
    			get { return _emailListTextBox; }
    			set { _emailListTextBox = value; }
    		}
    		[Browsable(true)]
    		public Button SubmitButton
    		{
    			get { return _submitButton; }
    			set { _submitButton = value; }
    		}
    
    		[Browsable(true)]
    		public Label NameLabel
    		{
    			get { return _nameLabel; }
    			set { _nameLabel = value; }
    		}
    		[Browsable(true)]
    		public Label EmailLabel
    		{
    			get { return _emailLabel; }
    			set { _emailLabel = value; }
    		}
    		[Browsable(true)]
    		public Label EmailListLabel
    		{
    			get { return _emailListLabel; }
    			set { _emailListLabel = value; }
    		}
    
    		protected override void Render(HtmlTextWriter writer)
    		{
    			RenderContents(writer);
    		}
    		protected override void CreateChildControls()
    		{
    			// call base
    			base.CreateChildControls();
    
    			// create controls
    			NameLabel.Text = "Your Name";
    			EmailLabel.Text = "Your Email";
    			EmailListLabel.Text = "Enter email addresses, one on each line.";
    			EmailListTextBox.TextMode = TextBoxMode.MultiLine;
    			SubmitButton.Text = "Submit";
    
    			// hook submit event
    			SubmitButton.Click += submitButton_Click;
    
    			// add controls
    			Controls.Add(NameLabel);
    			Controls.Add(new LiteralControl("&lt;br/>"));
    			Controls.Add(NameTextBox);
    			Controls.Add(new LiteralControl("&lt;br/>"));
    
    			Controls.Add(EmailLabel);
    			Controls.Add(new LiteralControl("&lt;br/>"));
    			Controls.Add(EmailTextBox);
    			Controls.Add(new LiteralControl("&lt;br/>"));
    			Controls.Add(EmailListLabel);
    			Controls.Add(new LiteralControl("&lt;br/>"));
    			Controls.Add(EmailListTextBox);
    			Controls.Add(new LiteralControl("&lt;br/>"));
    			Controls.Add(SubmitButton);
    		}
    
    		void submitButton_Click(object sender, System.EventArgs e)
    		{
    			EmailTextBox.Text = user@email.com;
    		}
    	}
    }
      
    </o:p>
    Thanks for any direction.<o:p></o:p><o:p> </o:p>Chris <o:p></o:p><o:p> </o:p>
    Sunday, July 20, 2008 12:44 PM

Answers

  • User-16411453 posted

    I haven't used create child control in quite awhile now. But I think you have to manually call the render method.

    You have to call a render method to the main control container, which is control for you, and use the assigned variable for your HtmlTextWriter

    protected override void CreateChildControls()
    {
    // call base
    base.CreateChildControls();

    // create controls
    NameLabel.Text = "Your Name";
    EmailLabel.Text = "Your Email";
    EmailListLabel.Text = "Enter email addresses, one on each line.";
    EmailListTextBox.TextMode = TextBoxMode.MultiLine;
    SubmitButton.Text = "Submit";

    // hook submit event
    SubmitButton.Click += submitButton_Click;

    // add controls
    Controls.Add(NameLabel);
    Controls.Add(new LiteralControl("&lt;br/>"));
    Controls.Add(NameTextBox);
    Controls.Add(new LiteralControl("&lt;br/>"));

    Controls.Add(EmailLabel);
    Controls.Add(new LiteralControl("&lt;br/>"));
    Controls.Add(EmailTextBox);
    Controls.Add(new LiteralControl("&lt;br/>"));
    Controls.Add(EmailListLabel);
    Controls.Add(new LiteralControl("&lt;br/>"));
    Controls.Add(EmailListTextBox);
    Controls.Add(new LiteralControl("&lt;br/>"));
    Controls.Add(SubmitButton);
    			Controls.Render(writer);


    }
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 22, 2008 8:05 PM