Casting of derived classes

M

Matt Brandwood

I tend to agree with Dave..I can see nothing wrong with it either...but
..Net begs to differ!

I've gone even further backward and have created a simple project with
one user control that I'm dynamically tryign to load into default.aspx.

UC (all done within VS 2003):
Public Class testuc
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

End Sub

End Class

default.aspx:

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim myuc As dynUCload = CType(Page.LoadControl("testuc.ascx"),
dynUCload)
Page.Controls.Add(myuc)
End Sub

End Class
Public Class dynUCload
Inherits UserControl

End Class

testuc.ascx sits in the same folder as default.aspx- the root of the
virtual folder.

It's driving me nuts- any other ideas?
 
D

Dave

Your example confuses me a bit.

I see the first user control named, "testuc" and I'm assuming it's name is testuc.ascx.
I then see a Page declaration.
Then another, empty, UserControl named "dynUCload".

But in the following snippet from your custom Page:
Dim myuc As dynUCload = CType(Page.LoadControl("testuc.ascx"),
dynUCload)

You are loading "testuc.ascx" and trying to cast it to dynUCload.

This will not work since dynUCload does not derive from testuc.

Try the following instead:

Dim myuc As testuc = CType(Page.LoadControl("testuc.ascx"), testuc)


I hope it helps.
 
M

Matt Brandwood

Dave...thanks for your help. I think there was initially a problem with
the path to the usercontrol as you suggested, but more than that I had
the user contol inheriting from user control when it should have been
inheriting from the derived class. Amending this seems to have solved
the problem.
Thaks again
 

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

Top