Prevent User Control Code from running (ASP.NET)

You can prevent code within a user control (ascx file) from running if it is invisible on the containing page. To do so, just check base.Visible (C#) or MyBase.Visible (VB). For example:

C#

private void Page_Load(object sender, EventArgs e)
{
	if (base.Visible) {
		// run code
	}
}

VB

Private Sub Page_Load(sender As Object, e As EventArgs)
	If MyBase.Visible Then
		' run code
	End If
End Sub

Comments