Displaying .NET User Controls within Pages that Inherit from a Master Page

April 16, 2010

Development

I ran across an odd situation this morning regarding a web application I’m building. The business rules for the application calls for a certain form to be used during multiple transactions — hence multiple pages. Rather than creating the same form elements multiple times, I wanted to take advantage of ’s reusable code flavor: Web User Controls. (They are called Web User Controls in 2008, as I understand it, in previous versions of Studio they are referred to as simply Web Controls.)

The Problem

As this was my first time using this control, I wanted to start small. I created a new that contained a simple form; one label, and one text box. I saved it as the .ascx file, and then created a test (.aspx) page that inherited from my application’s to drag the control onto. After I had included the reference to the control, I switch studio to the design view to ensure it registered correctly. Sure enough, my super simple custom form appeared within the page’s content place holder. However; when I published the page and viewed it in the browser, it completely ignored the user control. There were no errors, and it and displayed the components included in the , but the content place holder was completely empty.

To make matters even more peculiar, I created another simple page that did not inherit from my application’s Mater Page and published that to the browser. Sure enough, on this page the control rendered correctly, and I could see all it’s contents.

The Solution

With the help of a co-worker, we figured the issue out. Comparing the source of each file, there is a very noticeable, albeit strange, omission in the page that inherits from the Master Page. The directive that registers the component with the page was present in both source files, however the actual control tag that invokes its presence was missing. Once I included the user control tag within the content place holder, the page rendered the control correctly.

The source for the page without the Master Page inheritence:


<!-- 

Notice the user control tag

-->

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm3.aspx.vb" Inherits="APP.WebForm3" %>

<%@ Register src="userControl.ascx" tagname="userControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <uc1:userControl ID="viewCaseForm1" runat="server" />

    </div>
    </form>
</body>
</html>

The source for the page with the Master Page Inheritence:


<!-- 

Notice the absense of the user control tag

-->

<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/mainStructure.master" CodeBehind="ViewCaseFormContainer.aspx.vb" Inherits="APP.UserControlContainer"
    title="Untitled Page" %>
<%@ Register src="userControl.ascx" tagname="userControl" tagprefix="uc1" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"></asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <form id="form1" runat="server">

    </form>

    </asp:Content>

As to why the Visual Studio designer showed the control being rendered fully when the appropiate control wasn’t included in the content place holder, I’m assuming that’s a bug in the IDE.

, , , ,

No comments yet.

Leave a Reply