How to access an image

S

shapper

Hello,

I my Page_Init event I have the following:

Dim iHeader As New Image
AddHandler iHeader.Init, AddressOf iHeader_Init
Page.Controls.Add(iHeader)

Then I have this:
Private Sub iHeader_Init(ByVal sender As Object, ByVal e As
EventArgs)

' Define iHeader properties
With iHeader ************************* ERROR
***************************
.AlternateText =
Me.GetLocalResourceObject("iHeader.AlternateText")
.ImageAlign = ImageAlign.Top
.ImageUrl = "~/Assets_Design/Images/Header_Drawing.jpg"
.ToolTip = Me.GetLocalResourceObject("iHeader.ToolTip")
End With

End Sub

I am getting the error "iHeader is no declared" in ****************.

How can I do this?

Thanks,
Miguel
 
M

Mark Fitzpatrick

Keep in mind the scope of the iHeader Image control. Since you explicitely
create it in the Page_Init event, that's the only place you can reference it
directly as it's not a global or class variable. If you defined it as part
of the class you could then reference it without problem anywhere within the
class.

When you access it from a subroutine, you can use the Page.FindControl
method to attempt to find the control named "iHeader". You'll create a local
variable as a reference to it within your iHeader_Init code. You'll have to
forgive me, but I haven't used VB.Net in years so I'm going to supply the C#
equivalent

System.Web.UI.WebControls.Image iHeader =
(System.Web.UI.WebControls.Image)Page.FindControl("iHeader");
if(iHeader != null)
{
// then you can manipulate the iHeader reference here since it found
the control OK.
}
 
S

shapper

Hi,

Got it! Thanks!

And does it make any sense to keep:
ByVal sender As Object, ByVal e As EventArgs

in
Private Sub iHeader_Init(ByVal sender As Object, ByVal e As EventArgs)

I am asking this because this is the first time I am adding my controls
to the page at runtime.

Thanks,
Miguel
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top