Link to home
Create AccountLog in
Avatar of miguel_j
miguel_jFlag for Australia

asked on

how do i dynamically add a user control

hello

this website applies a different masterpage based on the domain.
once the masterpage is applied i want to add different content templates that load different user controls dynamically

error is in my class file

Compilation Error Type 'BreadCrumb' is not defined.

below is my code

--------------
test.aspx

<%@ Page Language="vb" EnableViewState="false" AutoEventWireup="false" MasterPageFile="~/MasterPages/Website.Master" CodeBehind="test.aspx.vb" Inherits="Website.test" %>

<%@ Reference Control="~/Controls/BreadCrumb.ascx" %>

---------------
test.aspx.vb

Public Partial Class test
    Inherits System.Web.UI.Page

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

        'code to determine domain and apply correct masterpage (irrelevant to the question)
        Dim m As New ApplyMasterPage
        Me.Page.MasterPageFile = m.MasterLocation.ToString()

        MyBase.AddContentTemplate("BreadCrumbContent", New ApplyBreadCrumbContent)

    End Sub

End Class

--------------
App_Code/ApplyBreadCrumbContent.vb

Public Class ApplyBreadCrumbContent
    Implements System.Web.UI.ITemplate

    Dim b As BreadCrumb 'error here

    Dim p As Page

    Sub InstantiateIn(ByVal container As Control) Implements System.Web.UI.ITemplate.InstantiateIn

        b = CType(p.LoadControl("~/Controls/BreadCrumb.ascx"), BreadCrumb)

        container.Controls.Add(b)

    End Sub

End Class

---------------
BreadCrumb.ascx
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="BreadCrumb.ascx.vb" Inherits="Website.BreadCrumb" %>

'contains a repeater control

--------------------------
BreadCrumb.ascx.vb

Public Partial Class BreadCrumb
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'contains data to render the repeater

    End Sub

End Class

------------------
MasterPage contains a ContentPlaceHolder
<asp:ContentPlaceHolder ID="BreadCrumbContent" runat="server"></asp:ContentPlaceHolder>
Avatar of Kumaraswamy R
Kumaraswamy R
Flag of India image

Try this sample you will get an idea.


Master pages (aspx)

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Common.master.vb" Inherits="Test.Common" %>

<!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></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>




<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Master1.master.vb" Inherits="Test.Master1" %>

<!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></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Master1
        <asp:ContentPlaceHolder ID="Test1" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>



<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="Master2.master.vb" Inherits="Test.Master2" %>

<!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></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Master2
        <asp:ContentPlaceHolder ID="Test2" runat="server">
       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>



Template

Public Class Template1
    Implements ITemplate

    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        Dim p As New Page
        container.Controls.Add(p.LoadControl("~/MasterPages/User1.ascx"))
    End Sub
End Class



Public Class Template2
    Implements ITemplate


    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
        container.Controls.Add(container.Page.LoadControl("~/MasterPages/User2.ascx"))
    End Sub
End Class



Usercontrol

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="User1.ascx.vb" Inherits="Test.User1" %>
<asp:Label ID="Label1" runat="server" BackColor="#FF5050" Text="UserControl1"></asp:Label>



<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="User2.ascx.vb" Inherits="Test.User2" %>
<asp:Label ID="Label1" runat="server" BackColor="#FF6600" Text="UserControl2"></asp:Label>




Test page aspx

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Test.aspx.vb" Inherits="Test.Test"  MasterPageFile="~/MasterPages/Common.Master"%>

 




codebehind

Public Partial Class Test
    Inherits System.Web.UI.Page
    Private Const masterid As Integer = 1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   

    End Sub

    Private Sub Test_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        If masterid = 0 Then
            Me.Page.MasterPageFile = "Master1.Master"
            MyBase.AddContentTemplate("Test1", New Template1)
        Else
            Me.Page.MasterPageFile = "Master2.Master"
            MyBase.AddContentTemplate("Test2", New Template1)
        End If


    End Sub
End Class
ASKER CERTIFIED SOLUTION
Avatar of madhevan_pillai
madhevan_pillai
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of miguel_j

ASKER

thank you for your help and also thanks for the other additions experts