Hi Everyone,

I have one master page and one user control

I want to add and use the user control at runtime in the master page.

I have used LoadControl Method to add usercontrol but in that case i was unable in getting the tagprefix and tagname value of the user control so that i can use it on master page file also.

Can anyone help me in resolving this issue

Thanks

..
 Control c = LoadControl("~/Test.ascx");
 form1.Controls.Add(c);
 ..
..
 Control c = LoadControl("~/Test.ascx");
 form1.Controls.Add(c);
 ..

I have also used this code
but in my master page i have give certain condition to include that user control

Like

<%if (pageName2 == "Flight Search Result") { %> <searchAgain:searchAgain ID="sSearchAgain" runat="server" />
<%} %>

so if this condition becomes true then after i want to add the user control

Egad! Put the "if" statement in the code behind around the LoadControl call and do not under any circumstances use the <% %> coding mechanism. The horror!

But here's a full working example.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="loadcontroldemo.aspx.cs" Inherits="loadcontroldemo" %>

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

And the code file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class loadcontroldemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (true)
        {
            Control userControl = this.LoadControl("SampleControl.ascx");
            controlPlaceholder.Controls.Add(userControl);
        }
    }
}

Egad! Put the "if" statement in the code behind around the LoadControl call and do not under any circumstances use the <% %> coding mechanism. The horror!

But here's a full working example.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="loadcontroldemo.aspx.cs" Inherits="loadcontroldemo" %>

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

And the code file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class loadcontroldemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (true)
        {
            Control userControl = this.LoadControl("SampleControl.ascx");
            controlPlaceholder.Controls.Add(userControl);
        }
    }
}

Thanks a lot apegram

You guys are genius

Its solve my issue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.