How to create an instance of user control in a class(simple) file notin Page class file?

Discussion in 'ASP .Net' started by chandan, Nov 28, 2007.

  1. chandan

    chandan Guest

    Hi,
    How to create an instance of user control in a class(simple) file not
    in Page class file? As I tried I can do it in page class file but not
    in simple class file.
    Let me know the solution!!

    Thanks,
    Chandan kumar
    chandan, Nov 28, 2007
    #1
    1. Advertising

  2. chandan

    Jayakrishnan Guest

    Re: How to create an instance of user control in a class(simple)file not in Page class file?

    chandan wrote:
    > Hi,
    > How to create an instance of user control in a class(simple) file not
    > in Page class file? As I tried I can do it in page class file but not
    > in simple class file.
    > Let me know the solution!!
    >
    > Thanks,
    > Chandan kumar


    Add the following tags in web.config

    <pages>
    <controls>
    <add tagPrefix="Page" tagName="ActionPane" src="~/UserControl/Header.ascx"/>
    </controls>
    </pages>

    Add the following method in class file

    Public Class Sample
    Public Function GetControl() As Control
    Dim objHeader As Control
    Dim objLoad As New System.Web.UI.UserControl

    objHeader = objLoad.LoadControl("~/Controls/Header.ascx")
    Return objHeader
    End Function
    End Class

    If you need to access in page add the following code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim obj As New Sample
    Panel1.Controls.Add(obj.GetControl())
    End Sub

    Jayakrishnan V
    Technology Group
    Jayakrishnan, Nov 28, 2007
    #2
    1. Advertising

  3. chandan

    chandan Guest

    Re: How to create an instance of user control in a class(simple) filenot in Page class file?

    On Nov 28, 1:25 pm, Jayakrishnan <>
    wrote:
    > chandan wrote:
    > > Hi,
    > > How to create an instance of user control in a class(simple) file not
    > > in Page class file? As I tried I can do it in page class file but not
    > > in simple class file.
    > > Let me know the solution!!

    >
    > > Thanks,
    > > Chandan kumar

    >
    > Add the following tags in web.config
    >
    > <pages>
    > <controls>
    > <add tagPrefix="Page" tagName="ActionPane" src="~/UserControl/Header.ascx"/>
    > </controls>
    > </pages>
    >
    > Add the following method in class file
    >
    > Public Class Sample
    > Public Function GetControl() As Control
    > Dim objHeader As Control
    > Dim objLoad As New System.Web.UI.UserControl
    >
    > objHeader = objLoad.LoadControl("~/Controls/Header.ascx")
    > Return objHeader
    > End Function
    > End Class
    >
    > If you need to access in page add the following code
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim obj As New Sample
    > Panel1.Controls.Add(obj.GetControl())
    > End Sub
    >
    > Jayakrishnan V
    > Technology Group


    Thanks Jayakrishnan...

    But my actual proble is:
    How to create object of a class which is outside of App_Code folder in
    Asp.net 2.0 using C#.??

    Let me know if you have solution???
    chandan, Dec 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chris brat
    Replies:
    1
    Views:
    617
    chris brat
    May 10, 2006
  2. Sridhar R
    Replies:
    14
    Views:
    1,382
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. Sandra-24
    Replies:
    18
    Views:
    1,844
    Steve Holden
    Apr 29, 2006
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    367
    Martin P. Hellwig
    Mar 26, 2010
  5. Peter Otten
    Replies:
    1
    Views:
    151
    Andreas Perstinger
    Jun 18, 2013
Loading...

Share This Page