0

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

Edited by __avd: Remove [b] tags.

3
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by manish_partey
0
..
 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

1

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);
        }
    }
}

Edited by apegram: n/a

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.