This is part two in a series of posts about ASP.Net Controls.
ASP.Net Controls, Part 2: Composite Custom Controls - some assembly required
Now these controls are able to be designed to have some reuse and life outside one particular project. The up side is that you can piece together odds and ends from System.Web.Ui.WebControls just how you like them, and as per normal the actual rendering to HTML is left to ASP.Net.
The down side is you just start with a class, descend from WebControl and piece it together in code.
Here's a bare bones sample to illustrate the concept, but first some notes:
using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; namespace MyProject.Controls { public class CompositeControl : System.Web.UI.WebControls.WebControl { TextBox txtInput = new TextBox(); Button cmdSubmit = new Button(); protected override void CreateChildControls() { EnsureChildControls(); txtInput.Text = DefaultText; Controls.Add(txtInput); cmdSubmit.Text = "Submit"; Controls.Add(cmdSubmit); base.CreateChildControls(); } protected override void Render(HtmlTextWriter writer) { base.Render(writer); } [Category("Appearance"), DefaultValue("Enter text here")] public string DefaultText { get { EnsureChildControls(); return txtInput.Text; } set { EnsureChildControls(); txtInput.Text = value; } } } }
Powered by: newtelligence dasBlog 2.3.9074.18820
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010, James Green
E-mail