Add UserControl To Page From Another Class

Posted by Raika on Stack Overflow See other posts from Stack Overflow or by Raika
Published on 2010-12-23T12:58:08Z Indexed on 2010/12/26 9:54 UTC
Read the original article Hit count: 196

Filed under:
|

I have page and call method inside my page. I want to add some control to my page Control (not page itself) inside that method.

namespace Program
{
    public partail class Default : Page
    {
         protected void Page_Load(object sender, Eventargs e)
         {
              MyClass.Calling(this); 
         }
    }
}

in another class

namespace Program
{
    public class MyClass
    {
         public static void Calling(Page page)
         {
              Textbox txt = new Textbox()
              // I want somthing like this.
              // page.PlaceHolder1.Controls.Add(txt);
         }
    }
}

Is this possible?

My Default.aspx :

<%@ Page Title="Home Page" MasterPageFile="~/Site.master"  ...  %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</asp:Content>

Update: thanks to The King for help. his suggest work correctly if control is inside page not Content of master page like my defualt sample code.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET