How do you pass an ascx user control a variable??? Help please

S

simon

hello. relatively new to vb.net, i'm using VS 2003 and .net 2.0
i have a web app that i'm i have a user control that displays a simple
1 row table as the header of the page. the user control takes one
input variable, the string that will be displayed as the title of the
page. example call:
<uc1:Header runat=server ID="ucHeader" Title="Home Page" />

what i am looking to do on a certain page is have the "title" value be
dynamic, in that i will pull the string from the database and pass
that to the user control.
i have the process working just fine if i'm passing it into a label,
but the user control is not receiving the setting of this value. i'm
assuming that controls on a page are process first before the page.

how would i do this? example code would be very much appreciated!!
thank you.
 
M

Matt MacDonald

Hi Simon,
You need to specify a property in your control as well as a variable to
hold the value. For example:
Dim _Title As String
Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property

Then, from within your control you can use _Title just like any other
variable. You can also reference this in your page code by doing
mycontrol.Title = "something" assuming that you add the appropriate
mycontrol declaration.

Thanks,
Matt
 
Y

YurikoEX

I would use session variables. Easy quick and affective. set the
session in the main page like, Session["Title"] = "My New Title" then
in the control have the control look for Session["Title]" != null then
set the title to the session variable
 
S

simon

hello matt,
thanks for the reply. what you described at the end ...
mycontrol.Title = "something"
is exactly what i was looking to do.
where would the property definition go? is that in the ascx code
behind?
also, once this is done, would the normal way of calling the control
still work? like having a title hard coded in the call (as they are
now)
thanks again
 
M

Matt MacDonald

The property definition would need to go in the codebehind for the control.

As far as calling the control, you would need to drag it into your page html
like you said in your first post:
<uc1:Header runat=server ID="ucHeader" Title="Home Page" />

In VS2003 you need to add the control declaration to the codebehind of your
page as well in order to be able to reference it:
Protected WithEvents ucHeader as Header <-- This would go in the code behind
up where the rest of the control definitions are.

See if that works for you.

Thanks,
Matt
 

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