example of setting a user controls properties using code behind instead of script tags?

D

Dave

hi all,

I am looking for an example for setting/getting a user control's properties
using a code behind page instead of <script></script> tags embedded in the
..aspx page.
There seems to be millions of examples using embedded code but none actually
showing how to do it in a code behind.

for example: say there is a user control called "catsplash.ascx" containing
a simple image tag:
<asp:Image id="imgCat1" runat="server" ImageAlign="Left"></asp:Image>

so far, I have a code behind file for the control called "catsplash.ascx.vb"

Public Class CatSplash
Inherits System.Web.UI.UserControl

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub

Protected WithEvents imgCat1 As System.Web.UI.WebControls.Image
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub

Private sImageURL1 As String

Public Property imageurl1() As String
Get
Return sImageURL1
End Get
Set(ByVal Value As String)
sImageURL1 = Value
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

imgCat1.ImageUrl = sImageURL1

End Sub
End Class


Then, in the .aspx page containing the control (category.aspx), I have
<%@ Page CodeBehind="category.aspx.vb" Language="vb" AutoEventWireup="false"
Inherits="BVCommerce.category" %>
<%@ Register TagPrefix="uc1" TagName="CatSplash"
Src="controls/CatSplash.ascx" %>
....
<uc1:catsplash id="CatSplash1" runat="server"
Visible="False"></uc1:catsplash>
....


how do I reference the control in the codebehind? (category.aspx.vb)
for example, I just want to set the image url to something else.
but the following does not work in the code behind:

CatSplash.imageurl1 = "images/mycategoryheader.jpg"

I am not sure how I need to reference this - "CatSplash" is the name of the
class as you can see above and I have a public property called "imageurl1",
yet it does not work as expected. (in the books, they reference it by the
id of the control inside a <script> tag, but this won't work either in a
code behind)

What am I doing wrong here?

TIA,
Dave
 
A

Andy Gaskell

Try removing the code form Page_Load and change your property to this. You
can also remove the private field (sImageURL1).

Public Property imageurl1() As String
Get
Return Me.imgCat1.ImageUrl
End Get
Set(ByVal Value As String)
Me.imgCat1.ImageUrl = Value
End Set
End Property
 
D

Dave

AG,
I tried that with the same results: "Object reference not set to an instance
of an object."
after running the result.
I changing the code in the category.aspx.vb to

Dim cs as CatSplash
cs.imageurl1 = "images/mycategoryheader.jpg"

allowed me to actually compile the page,
otherwise I get "Reference to a non-shared member requires an object
reference."
when using:
CatSplash.imageurl1 = "images/mycategoryheader.jpg"

I tried also to change public property to read
Public Shared Property imageurl1 as string ....

but it mentions needing an instance of a class ...yada yada yada...
I think the problem is that this is a circular reference (like it has to be
defined before it is defined)

I'm stumped.
 
A

Andy Gaskell

You don't want to change the property to shared. I noticed you already have
the tag in the HTML so the codebehind of the page (most likely) already has
declared that control.

try this:

CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"

instead of:

Dim cs as CatSplash
cs.imageurl1 = "images/mycategoryheader.jpg"
 
D

Dave

I looked in the code behind and there is no declaration of the user control.
That would explain why CatSplash1.ImageUrl1 = "images/mycategoryheader.jpg"
does not work.
Do you know how to declare it?
 
A

Andy Gaskell

Put this right under the Inherits line

Protected WithEvents CatSplash1 As CatSplash
 
D

Dave

DOH! That was it! (note to self: declare the control in the main class,
not inside a sub like "page_load")
One day a neuron in my brain will fire and OOP will sink in - that day has
yet to come. ;)
Thanks AG!
 
A

Andy Gaskell

No problem - good luck!

Dave said:
DOH! That was it! (note to self: declare the control in the main class,
not inside a sub like "page_load")
One day a neuron in my brain will fire and OOP will sink in - that day has
yet to come. ;)
Thanks AG!

(category.aspx),
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top