Lessons in this course (10)
5. Validation controls, master pages, themes and user controls
Validation controls (client-side JavaScript + server-side check, Page.IsValid):
| Control | Checks |
|---|---|
RequiredFieldValidator | Value entered |
CompareValidator | Compare with another control/value/type (Operator="DataTypeCheck") |
RangeValidator | Between min and max |
RegularExpressionValidator | Pattern (email, phone) |
CustomValidator | Your own function (OnServerValidate, ClientValidationFunction) |
ValidationSummary | Shows all errors in one place |
ValidationGroup separates groups of validators on one page; CausesValidation="false" on Cancel buttons. Always check Page.IsValid on the server — client scripts can be bypassed.
Master pages (layout template):
<%@ Master Language="C#" CodeBehind="Site.master.cs" Inherits="ShopWeb.SiteMaster" %>
<html><head runat="server"><title></title></head>
<body><form runat="server">
<header>Divakar Shop</header>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<footer>© 2026</footer>
</form></body></html>Access the master from a page with <%@ MasterType VirtualPath="~/Site.Master" %> and Master.SomeProperty. Nested master pages are allowed. Event order: at runtime the master page becomes a child control of the page. Init (and Unload) run child-first, so the master's Init runs before the page's Init; Load and PreRender run parent-first, so the page's Load runs before the master's Load.
Themes and skins: App_Themes/Dark/Controls.skin sets default control properties (<asp:Button runat="server" CssClass="btn btn-dark" />); apply with Theme="Dark" in the page or web.config <pages theme="Dark">.
User controls (.ascx) — reusable page fragments with their own markup and code-behind:
<%@ Control Language="C#" CodeBehind="AddressEditor.ascx.cs" Inherits="ShopWeb.Controls.AddressEditor" %>
<asp:TextBox ID="txtCity" runat="server" />public partial class AddressEditor : UserControl
{
public string City { get => txtCity.Text; set => txtCity.Text = value; } // expose properties
public event EventHandler? AddressChanged; // and events
}<%@ Register Src="~/Controls/AddressEditor.ascx" TagPrefix="uc" TagName="Address" %>
<uc:Address ID="shipping" runat="server" />(Custom server controls compiled into DLLs are covered in the Custom Controls & Components course.)