Lessons in this course (10)

5. Validation controls, master pages, themes and user controls

Intermediate★ Must knowLesson 5 of 10

Validation controls (client-side JavaScript + server-side check, Page.IsValid):

ControlChecks
RequiredFieldValidatorValue entered
CompareValidatorCompare with another control/value/type (Operator="DataTypeCheck")
RangeValidatorBetween min and max
RegularExpressionValidatorPattern (email, phone)
CustomValidatorYour own function (OnServerValidate, ClientValidationFunction)
ValidationSummaryShows 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):

aspx
<%@ 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:

aspx
<%@ Control Language="C#" CodeBehind="AddressEditor.ascx.cs" Inherits="ShopWeb.Controls.AddressEditor" %>
<asp:TextBox ID="txtCity" runat="server" />
csharp
public partial class AddressEditor : UserControl
{
    public string City { get => txtCity.Text; set => txtCity.Text = value; }   // expose properties
    public event EventHandler? AddressChanged;                                  // and events
}
aspx
<%@ 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.)