Skip to Content
Beginning ASP.NET 3.5: In C# and VB
book

Beginning ASP.NET 3.5: In C# and VB

by Imar Spaanjaars
March 2008
Intermediate to advanced content levelIntermediate to advanced
766 pages
21h 15m
English
Wrox
Content preview from Beginning ASP.NET 3.5: In C# and VB

8.2. Adding Logic to Your User Controls

Although using controls for repeating content is already quite useful, they become even more useful when you add custom logic to them. By adding public properties or methods to a user control, you can influence its behavior at runtime. When you add a property to a user control, it becomes available automatically in IntelliSense and in the Properties Grid for the control in the page you're working with, making it easy to change the behavior from an external file like a page.

To add properties or methods to a user control, you add them to the Code Behind of the control. The properties you add can take various forms. In its simplest form, a property looks exactly like the properties you saw in Chapter 5. For more advanced scenarios you need to add ViewState properties that are able to maintain their state across postbacks. In the next two exercises you see how to create both types of properties.

8.2.1. Creating Your Own Data Types for Properties

To make the banner control more useful, you can add a second image to it that displays as a horizontal banner. By adding a property to the user control you can then determine whether to display the vertical or horizontal image. You could do this by creating a numeric property of type System.Byte. Then 0 would be vertical and 1 would be horizontal for example. However, this makes it hard to remember what each number represents. You can make it a bit easier by creating a String property that accepts ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning ASP.NET 4: in C# and VB

Beginning ASP.NET 4: in C# and VB

Imar Spaanjaars
Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Professional ASP.NET 3.5 SP1 Edition: In C# and VB

Bill Evjen, Scott Hanselman, Devin Rader

Publisher Resources

ISBN: 9780470187593Purchase book