how to load an ascx file in class.vb ?

  • Thread starter Thread starter Quentin
  • Start date Start date
Q

Quentin

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.
 
LoadControl is what you want. Example in VB:

Dim myControl as Control = LoadControl("MyControl.ascx")
Me.Controls.Add(myControl)
 
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.
 
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.
 
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
 
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.
 
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
 
Back
Top