Browse DevX
Sign up for e-mail newsletters from DevX


ASP.NET: Extending the Power of Web Pages with User Controls : Page 3

User controls represent an interesting and powerful level of page reusability in ASP.NET. You can build user controls by extracting and combining blocks of code from existing pages. You can also assemble ASP.NET controls together in a sort of embeddable form for use in Web pages. In this article, you'll get a crash-course on how to design, write and use ASP.NET user controls.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

A layout-only user control is useful because of its user interface. However, some associated code is still necessary for at least letting programmers extract values from embedded controls. Let's add some. Table 1 lists the properties and the methods defined for the DateBox pagelet.

Table 1: The programming interface of the DateBox user control.

DayReturns an integer denoting the day of the currently set date
MonthReturns an integer denoting the month of the currently set date
SelectedDateGets or sets the current date as a MM/DD/YYYY string
SeparatorGets or sets the character(s) to be used as the date separator
YearReturns an integer denoting the year of the currently set date

GetDateReturns the current date as a DateTime object
GetDateAsStringSimilar to the SelectedDate property, return the date as a string
SetDateSets the current date. Accepts both a string and a DateTime object

In Listing 1, you can see the C# implementation of the properties. Notice the use of the get/set accessors to control how the property value is read and written.

Listing 2 details the methods of the control.

The method SetDate has two overloads and can accept the date both in a MM/DD/YYYY format and as a DateTime object—the official date and time object in the .NET Framework. When a new date is set, the month, day and year components are used to refresh the control's user interface. As shown earlier, the layout of the control is made of three textbox controls called _txtMonth, _txtDay and _txtYear, respectively, as shown in Figure 1. These controls are not externally accessible from the host page unless you deliberately decide to expose them using properties.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date