Wrox Programmer Forums
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 28th, 2007, 04:41 AM
Registered User
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Custom Controls


I've been reading my way through the Web Parts and Custom Controls in ASP.Net 2.0 book by Peter Vogel. I got to chapter 3 on building a custom control and got confuesed and need help!

What I'm trying to do is create a custom control to send Email! This control would have three text boxes:

1. Email address
2. Subject
3. Message

Each of these would have a corresponding title label and error label! There would also be a button to submit the form and run some validation against the fields.

I added each control to the controls collection in a CreateChildControls method which worked fine! My confusion came when I wanted to set the layout of the control.

What I want is to create a table with two columns. The first column on each row would contain the title label, the second would contain the text box with a whitespace after it and then the error label. The final row would be a merged cell containing the submit button.

However I'm really unsure of how to go about doing this. The book talks about overriding various render methods (render begin, render end etc) and using writer.RenderBeginTag(). Unfortunatly the book does say you can do this and says you break it up into 3 sections:

1. Write out the initial table tag
2. Render the constituent controls and their HTML
3. Write out the end table tag

but the book, nor the downloadable code examples show you how to do this and I can't work it out!

Can someone please help me out with this!?

Many thanks in advance


Old June 28th, 2007, 08:28 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

I'm no expert in custom controls, but you could try this:

When you add your controls in CreateChildControls, also add the formatting controls.

Create an HtmlControls.HtmlTable control and the necessary instances of HtmlControls.HtmlTableRow and HtmlControls.HtmlTableCell. Add your labels and textboxes to the cells and the cells to the rows and rows to the table to form the tabular layout, then add the table to the main Controls collection.

I think the approach indended by the book is to do this:
write out a chunk of html to start (i.e. <table>....<td>)
render the first row label
write out the next html chunk (</td><td>)
render the first row textbox
and so on and so forth until you get to
write out the end html chunk (</td></tr></table>)

Personally, I think the first approach is better because you can let the control render itself using the built in html controls so it's significantly easier to write. But that might not be the preferred way of creating custom controls. As I said, I'm no expert.

Old July 2nd, 2007, 04:07 PM
Friend of Wrox
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts

I agree, I find I have fewer problems the more I can rely on the .NET class libraries. They tend to be easier to code and manipulate. But you can write it all out manually, if you have to.

I think you're only problem here is that you're expecting it to be harder than it is. And most of the time MS, makes it so. ;) But I think this time, it really is as simple as writing chunks of of table structure (e.g. "<table><tr><td>") and concatenating that in with the other controls in your custom control.

Good luck.


Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Controls pro-logic C# 2008 aka C# 3.0 1 November 26th, 2008 10:55 AM
Custom Controls apry BOOK: Beginning ASP.NET 1.0 2 May 23rd, 2004 11:04 PM
Custom Controls bmains ASP.NET 1.x and 2.0 Application Design 1 April 8th, 2004 05:26 PM
Custom Controls bmains VS.NET 2002/2003 3 February 5th, 2004 09:54 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.