mmedi005
asked on
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. is the ERROR i get, why?
My class
Imports Microsoft.VisualBasic
Imports System.Web.UI
Public Class menu
Public Shared Sub Create_Side_Bars(ByVal x As Integer)
Dim Page As New Page
Dim myImage As Image = CType(Page.Master.FindCont rol("img_l eft_bar"), Image)
myImage.Height = x
End Sub
End Class
ERROR:
Exception Details: System.NullReferenceExcept ion: Object reference not set to an instance of an object.
And highlights this line
Line 8: Dim myImage As Image = CType(Page.Master.FindCont rol("img_l eft_bar"), Image)
Can anyone help me?
Imports Microsoft.VisualBasic
Imports System.Web.UI
Public Class menu
Public Shared Sub Create_Side_Bars(ByVal x As Integer)
Dim Page As New Page
Dim myImage As Image = CType(Page.Master.FindCont
myImage.Height = x
End Sub
End Class
ERROR:
Exception Details: System.NullReferenceExcept
And highlights this line
Line 8: Dim myImage As Image = CType(Page.Master.FindCont
Can anyone help me?
when you do this:
Dim myImage As Image = CType(Page.Master.FindCont rol("img_l eft_bar"), Image)
the "Page" is already defined as the current page you're working with
Dim myImage As Image = CType(Page.Master.FindCont
the "Page" is already defined as the current page you're working with
It can't find the control called "img_left_bar". Either you are doing this too early in the page life cycle an need to wait until Page_PreRender, or the control "img_left_bar" is in a subcontrol of the Page.Master.
For example if in the masterpage you have:
<asp:Panel id="something" runat="server">
<asp:image id="img_left_bar" ruant="server" imageurl="something.jpg"/>
</asp:Panel>
You might need to do this:
((Panel)Page.Master.FindCo ntrol("som ething")). FindContro l("img_lef t_bar")
<asp:Panel id="something" runat="server">
<asp:image id="img_left_bar" ruant="server" imageurl="something.jpg"/>
</asp:Panel>
You might need to do this:
((Panel)Page.Master.FindCo
ASKER
I in the transition of going from asp to asp .net, would a Panel be the right way to go? I have thimage inside a div. Reason why is because the control I have with the CSS. Can I manipulate the position of a div with a panel.
I do keep getting errors where ever I place the Sub routine. Pre Render- Pre Load, etc...
Keeps referring that Page is Null.....
I do keep getting errors where ever I place the Sub routine. Pre Render- Pre Load, etc...
Keeps referring that Page is Null.....
>>Keeps referring that Page is Null....
.
When you do this:
Dim Page As New Page
Dim myImage As Image = CType(Page.Master.FindCont rol("img_l eft_bar"), Image)
You:
1. Instantiate a page.
2. try to find the master page of the page you just instantiated.
the page exists, but only the instance in the first line...it doesn't actually exist as a real "something.aspx" page...that's why you're getting the error.
.
When you do this:
Dim Page As New Page
Dim myImage As Image = CType(Page.Master.FindCont
You:
1. Instantiate a page.
2. try to find the master page of the page you just instantiated.
the page exists, but only the instance in the first line...it doesn't actually exist as a real "something.aspx" page...that's why you're getting the error.
ASKER
But if I don't set it to new, visual studio complains about a possible runtime error if I just have
Dim myImage As Image = CType(Page.Master.FindCont rol("img_l eft_bar"), Image)
------
and it does error out....
so basically, i can not do this in a class, it would have to be in the actual webpage?
Dim myImage As Image = CType(Page.Master.FindCont
------
and it does error out....
so basically, i can not do this in a class, it would have to be in the actual webpage?
if you do it in a class, then you can pass the page to the class function...like...
class file:
Public Sub SetImage(byval ThePage as system.web.ui.page)
Dim myImage As Image = CType(ThePage.Master.FindC ontrol("im g_left_bar "), Image)
end sub
class file:
Public Sub SetImage(byval ThePage as system.web.ui.page)
Dim myImage As Image = CType(ThePage.Master.FindC
end sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
also this:
http://aspnet.4guysfromrolla.com/articles/041305-1.aspx
between the 2 articles, you can piece together a way to use a base page class; which also works for master pages and you can create a base master page for code..and how to pass properties between a master/content pages
http://aspnet.4guysfromrolla.com/articles/041305-1.aspx
between the 2 articles, you can piece together a way to use a base page class; which also works for master pages and you can create a base master page for code..and how to pass properties between a master/content pages
Dim Page As New Page