how to load an ascx file in class.vb ?

Discussion in 'ASP .Net' started by Quentin, Jul 30, 2003.

  1. Quentin

    Quentin Guest

    I need to import an *;ascx, as an object, in my class, that is in
    myclass.vb. How can i do ? i saw the method LoadControl but i can't use it,
    or maybe i did wrong, if could help me it would be very nice :)

    Bisous de France et merci d'avance.
     
    Quentin, Jul 30, 2003
    #1
    1. Advertising

  2. Quentin

    Marina Guest

    LoadControl is what you want. Example in VB:

    Dim myControl as Control = LoadControl("MyControl.ascx")
    Me.Controls.Add(myControl)

    "Quentin" <> wrote in message
    news:...
    > I need to import an *;ascx, as an object, in my class, that is in
    > myclass.vb. How can i do ? i saw the method LoadControl but i can't use

    it,
    > or maybe i did wrong, if could help me it would be very nice :)
    >
    > Bisous de France et merci d'avance.
    >
    >
     
    Marina, Jul 30, 2003
    #2
    1. Advertising

  3. Quentin

    Quentin Guest

    Thank you for your response but when i've already tried
    LoadControl("myfile.ascx") and an error shows up saying that LoadControl
    hasn't been declared.

    "Marina" <> a écrit dans le message de news:
    ...
    > LoadControl is what you want. Example in VB:
    >
    > Dim myControl as Control = LoadControl("MyControl.ascx")
    > Me.Controls.Add(myControl)
    >
    > "Quentin" <> wrote in message
    > news:...
    > > I need to import an *;ascx, as an object, in my class, that is in
    > > myclass.vb. How can i do ? i saw the method LoadControl but i can't use

    > it,
    > > or maybe i did wrong, if could help me it would be very nice :)
    > >
    > > Bisous de France et merci d'avance.
    > >
    > >

    >
    >
     
    Quentin, Jul 30, 2003
    #3
  4. Quentin

    Marina Guest

    This is a method of the Page calss, so it is certainly there.

    How and where are you trying to do this? Show your code.

    "Quentin" <> wrote in message
    news:...
    > Thank you for your response but when i've already tried
    > LoadControl("myfile.ascx") and an error shows up saying that LoadControl
    > hasn't been declared.
    >
    > "Marina" <> a écrit dans le message de news:
    > ...
    > > LoadControl is what you want. Example in VB:
    > >
    > > Dim myControl as Control = LoadControl("MyControl.ascx")
    > > Me.Controls.Add(myControl)
    > >
    > > "Quentin" <> wrote in message
    > > news:...
    > > > I need to import an *;ascx, as an object, in my class, that is in
    > > > myclass.vb. How can i do ? i saw the method LoadControl but i can't

    use
    > > it,
    > > > or maybe i did wrong, if could help me it would be very nice :)
    > > >
    > > > Bisous de France et merci d'avance.
    > > >
    > > >

    > >
    > >

    >
    >
     
    Marina, Jul 30, 2003
    #4
  5. Quentin

    Quentin Guest

    I'm trying to do this in a file of classes named MesClasses.vb and here is a
    simple version of the code :

    -----------------------------MesClasses.vb ---------------------------------
    ---------

    Public Class MaFenetre : inherits WebControl
    Private MyTable As New HtmlTable

    Public Sub New()


    Dim MyRow As New HtmlTableRow
    Dim MyCell As New HtmlTableCell

    MyRow.Cells.Add(MyCell)
    MyTable.Rows.Add(MyRow)

    End Sub

    Public Property AscxFile() As String
    Get
    Return AscxFile
    End Get
    Set (ByVal Value As String)
    AscxFile = Value
    AddAscx(AscxFile)
    End Set
    End Property

    Private Sub AddAscx(ByVal PathAscx As String)
    Dim MyObjectAscx As Control = LoadControl(PathAscx)
    MyTable.Rows(0).Cells(0).Controls.Add(MyObjectAscx)
    End Sub

    End Class

    --------------------------

    ----------------------
    index.aspx -------------------------------------------------------
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim InnerPage As New MaFenetre
    InnerPage.AscxFile("myfile.ascx")
    End Sub
    ---------------------------
    Thanks you again for your response so quick :) and excuse my english :)

    regards.

    "Marina" <> a écrit dans le message de news:
    #...
    > This is a method of the Page calss, so it is certainly there.
    >
    > How and where are you trying to do this? Show your code.
    >
    > "Quentin" <> wrote in message
    > news:...
    > > Thank you for your response but when i've already tried
    > > LoadControl("myfile.ascx") and an error shows up saying that LoadControl
    > > hasn't been declared.
    > >
    > > "Marina" <> a écrit dans le message de news:
    > > ...
    > > > LoadControl is what you want. Example in VB:
    > > >
    > > > Dim myControl as Control = LoadControl("MyControl.ascx")
    > > > Me.Controls.Add(myControl)
    > > >
    > > > "Quentin" <> wrote in message
    > > > news:...
    > > > > I need to import an *;ascx, as an object, in my class, that is in
    > > > > myclass.vb. How can i do ? i saw the method LoadControl but i can't

    > use
    > > > it,
    > > > > or maybe i did wrong, if could help me it would be very nice :)
    > > > >
    > > > > Bisous de France et merci d'avance.
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Quentin, Jul 30, 2003
    #5
  6. Quentin

    Quentin Guest

    oh thank you again :-D I'm a newbie in .net programming and you help me so
    much :)

    i'm ashamed to say that i still have a problem... Me.Page.LoadControl works
    fine but i got an error when i run the page, saying that an object reference
    is not defined at the "instance" of an object (in french : "La référence
    d'un objet n'est pas définie à l'instance d'un objet")... Again excuse my
    poor english :-\ And i got this error on this line :

    Me.Page.LoadControl("demo.ascx")

    Maybe it's because my file of classes MesClasses.vb is located in an other
    project so the path is not correct ? The tree of y solution is like :

    Solution
    --- Web Project
    ------ index.aspx
    --- Classes Project
    ------ MesClasses.vb

    I hope you understood me... Merci encore.



    "Marina" <> a écrit dans le message de news:
    ...
    > If you looked up LoadControl, you would have seen that this is a property

    of
    > the Page class. However, you are not inheriting from the Page class - so
    > you cannot inherit this method.
    >
    > You are inheriting from WebControl - so you need to find a way to get to

    the
    > Page object on which your WebControl will live.
    >
    > Conveniently, WebControl class has a Page property. And because you are
    > inheriting from WebControl, you are inheriting this property as well.
    >
    > So you would call it:
    >
    > Me.Page.LoadControl("MyControl.ascx")
    >
    > "Quentin" <> wrote in message
    > news:%...
    > > I'm trying to do this in a file of classes named MesClasses.vb and here

    is
    > a
    > > simple version of the code :
    > >

    >
    > -----------------------------MesClasses.vb -------------------------------
    > --
    > > ---------
    > >
    > > Public Class MaFenetre : inherits WebControl
    > > Private MyTable As New HtmlTable
    > >
    > > Public Sub New()
    > >
    > >
    > > Dim MyRow As New HtmlTableRow
    > > Dim MyCell As New HtmlTableCell
    > >
    > > MyRow.Cells.Add(MyCell)
    > > MyTable.Rows.Add(MyRow)
    > >
    > > End Sub
    > >
    > > Public Property AscxFile() As String
    > > Get
    > > Return AscxFile
    > > End Get
    > > Set (ByVal Value As String)
    > > AscxFile = Value
    > > AddAscx(AscxFile)
    > > End Set
    > > End Property
    > >
    > > Private Sub AddAscx(ByVal PathAscx As String)
    > > Dim MyObjectAscx As Control = LoadControl(PathAscx)
    > > MyTable.Rows(0).Cells(0).Controls.Add(MyObjectAscx)
    > > End Sub
    > >
    > > End Class
    > >
    > > --------------------------
    > >
    > > ----------------------
    > > index.aspx -------------------------------------------------------
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > > Dim InnerPage As New MaFenetre
    > > InnerPage.AscxFile("myfile.ascx")
    > > End Sub
    > > ---------------------------
    > > Thanks you again for your response so quick :) and excuse my english :)
    > >
    > > regards.
    > >
    > > "Marina" <> a écrit dans le message de news:
    > > #...
    > > > This is a method of the Page calss, so it is certainly there.
    > > >
    > > > How and where are you trying to do this? Show your code.
    > > >
    > > > "Quentin" <> wrote in message
    > > > news:...
    > > > > Thank you for your response but when i've already tried
    > > > > LoadControl("myfile.ascx") and an error shows up saying that

    > LoadControl
    > > > > hasn't been declared.
    > > > >
    > > > > "Marina" <> a écrit dans le message de

    > news:
    > > > > ...
    > > > > > LoadControl is what you want. Example in VB:
    > > > > >
    > > > > > Dim myControl as Control = LoadControl("MyControl.ascx")
    > > > > > Me.Controls.Add(myControl)
    > > > > >
    > > > > > "Quentin" <> wrote in message
    > > > > > news:...
    > > > > > > I need to import an *;ascx, as an object, in my class, that is

    in
    > > > > > > myclass.vb. How can i do ? i saw the method LoadControl but i

    > can't
    > > > use
    > > > > > it,
    > > > > > > or maybe i did wrong, if could help me it would be very nice :)
    > > > > > >
    > > > > > > Bisous de France et merci d'avance.
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Quentin, Jul 30, 2003
    #6
  7. Quentin

    Ryan Gregg Guest

    Quentin,
    Try using the ~ character in your path, to specify the root of the
    current webspace. In order words, try doing this:
    Me.Page.LoadControl("~/demo.ascx")

    That should load a user control that is located in the root folder of
    your web application named demo.ascx. I haven't tried a configuration
    exactly like yours, but I think this should work.

    Ryan Gregg

    "Quentin" <> wrote in message
    news:...
    > oh thank you again :-D I'm a newbie in .net programming and you help me so
    > much :)
    >
    > i'm ashamed to say that i still have a problem... Me.Page.LoadControl

    works
    > fine but i got an error when i run the page, saying that an object

    reference
    > is not defined at the "instance" of an object (in french : "La référence
    > d'un objet n'est pas définie à l'instance d'un objet")... Again excuse my
    > poor english :-\ And i got this error on this line :
    >
    > Me.Page.LoadControl("demo.ascx")
    >
    > Maybe it's because my file of classes MesClasses.vb is located in an other
    > project so the path is not correct ? The tree of y solution is like :
    >
    > Solution
    > --- Web Project
    > ------ index.aspx
    > --- Classes Project
    > ------ MesClasses.vb
    >
    > I hope you understood me... Merci encore.
    >
    >
    >
    > "Marina" <> a écrit dans le message de news:
    > ...
    > > If you looked up LoadControl, you would have seen that this is a

    property
    > of
    > > the Page class. However, you are not inheriting from the Page class -

    so
    > > you cannot inherit this method.
    > >
    > > You are inheriting from WebControl - so you need to find a way to get to

    > the
    > > Page object on which your WebControl will live.
    > >
    > > Conveniently, WebControl class has a Page property. And because you are
    > > inheriting from WebControl, you are inheriting this property as well.
    > >
    > > So you would call it:
    > >
    > > Me.Page.LoadControl("MyControl.ascx")
    > >
    > > "Quentin" <> wrote in message
    > > news:%...
    > > > I'm trying to do this in a file of classes named MesClasses.vb and

    here
    > is
    > > a
    > > > simple version of the code :
    > > >

    > >

    >
    > -----------------------------MesClasses.vb -------------------------------
    > > --
    > > > ---------
    > > >
    > > > Public Class MaFenetre : inherits WebControl
    > > > Private MyTable As New HtmlTable
    > > >
    > > > Public Sub New()
    > > >
    > > >
    > > > Dim MyRow As New HtmlTableRow
    > > > Dim MyCell As New HtmlTableCell
    > > >
    > > > MyRow.Cells.Add(MyCell)
    > > > MyTable.Rows.Add(MyRow)
    > > >
    > > > End Sub
    > > >
    > > > Public Property AscxFile() As String
    > > > Get
    > > > Return AscxFile
    > > > End Get
    > > > Set (ByVal Value As String)
    > > > AscxFile = Value
    > > > AddAscx(AscxFile)
    > > > End Set
    > > > End Property
    > > >
    > > > Private Sub AddAscx(ByVal PathAscx As String)
    > > > Dim MyObjectAscx As Control = LoadControl(PathAscx)
    > > > MyTable.Rows(0).Cells(0).Controls.Add(MyObjectAscx)
    > > > End Sub
    > > >
    > > > End Class
    > > >
    > > > --------------------------
    > > >
    > > > ----------------------
    > > > index.aspx -------------------------------------------------------
    > > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs) Handles MyBase.Load
    > > > Dim InnerPage As New MaFenetre
    > > > InnerPage.AscxFile("myfile.ascx")
    > > > End Sub
    > > > ---------------------------
    > > > Thanks you again for your response so quick :) and excuse my english

    :)
    > > >
    > > > regards.
    > > >
    > > > "Marina" <> a écrit dans le message de

    news:
    > > > #...
    > > > > This is a method of the Page calss, so it is certainly there.
    > > > >
    > > > > How and where are you trying to do this? Show your code.
    > > > >
    > > > > "Quentin" <> wrote in message
    > > > > news:...
    > > > > > Thank you for your response but when i've already tried
    > > > > > LoadControl("myfile.ascx") and an error shows up saying that

    > > LoadControl
    > > > > > hasn't been declared.
    > > > > >
    > > > > > "Marina" <> a écrit dans le message de

    > > news:
    > > > > > ...
    > > > > > > LoadControl is what you want. Example in VB:
    > > > > > >
    > > > > > > Dim myControl as Control = LoadControl("MyControl.ascx")
    > > > > > > Me.Controls.Add(myControl)
    > > > > > >
    > > > > > > "Quentin" <> wrote in message
    > > > > > > news:...
    > > > > > > > I need to import an *;ascx, as an object, in my class, that is

    > in
    > > > > > > > myclass.vb. How can i do ? i saw the method LoadControl but i

    > > can't
    > > > > use
    > > > > > > it,
    > > > > > > > or maybe i did wrong, if could help me it would be very nice

    :)
    > > > > > > >
    > > > > > > > Bisous de France et merci d'avance.
    > > > > > > >
    > > > > > > >
    > > > > > >
    > > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Ryan Gregg, Jul 30, 2003
    #7
    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. Quentin
    Replies:
    1
    Views:
    488
    Quentin
    Jul 29, 2003
  2. Jason Shohet
    Replies:
    1
    Views:
    484
    Kevin Spencer
    Nov 10, 2003
  3. Holger (David) Wagner
    Replies:
    2
    Views:
    1,943
    Holger (David) Wagner
    Jul 3, 2004
  4. Jiri Zidek
    Replies:
    4
    Views:
    225
    Steven Cheng[MSFT]
    Dec 6, 2004
  5. Joe
    Replies:
    1
    Views:
    374
    Jeffrey Tan[MSFT]
    Feb 16, 2004
Loading...

Share This Page