how to load an ascx file in class.vb ?

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.
 
M

Marina

LoadControl is what you want. Example in VB:

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

Quentin

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.
 
M

Marina

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.
 
Q

Quentin

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
 
Q

Quentin

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.
 
R

Ryan Gregg

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top