i found it easier to reply under what you have already
written so i could refer to specific bits...so scroll down
to see my reply.
I've marked my replies with "Dune Says:" and i've marked
code examples with "Dune Start Code Example:" and "Dune
End Code Example".
-----Original Message-----
Hi Dune,
Thanks for that.
I have taken your advice and got a bit further along the track
The following declaration is in my .aspx file
<%@ Register TagPrefix="uc1" TagName="Header" Src="WebControls/Header.ascx"
%>
<uc1:Header id="Header1" runat="server"></uc1:Header> - -- so I need to
reference a variable called "pageTitle" in the header control
"pageTitle" is declared in the .ascx file like so
<asp:Literal id="pageTitle" runat="server"
EnableViewState="true"></asp:Literal>
and in the ascx.vb file like so
public WithEvents pageTitle As System.Web.UI.WebControls.Literal
my problem now is that I want to change the text of "pageTitle" from my
..aspx.vb file i.e the code behind of the web page not the code behind of
the control.
so I need an instance of the user control in the code behind of the webpage.
I add this lke so
This is the line I could have got wrong.
Public myControl As Control =
CType(Page.LoadControl("WebControls/Header.ascx"), Control)
Dune Says:
Ok, the line above should be:
Dune Start Code Example:
Protected Header1 As Header
Dune End Code Example
Dune Says:
and that's all. you don't need to load the control because
you have already put it in the html of your web form (you
only use the LoadControl syntax when you want to create
user controls PROGRAMATICALLY in your code-behind).
Also, be sure to match the name of the variable you're
declaring in the code-behind to the id you gave that
object in the html. Plus, be sure that you match the type
of the variable you're declaring to the correct class it
belongs to.
so know I am thinking I can change the value of the variable in the user
control from the web page like so
myControl .pageTile() = "welcome to my web page"
but it doesn't work. Perhaps it is something to do with the declaration of
the usercontrol being of type control and then cast to the actual user
control rather than declaring an instance of the user control straight away.
Dune Says:
You can't access the variables declared in your user
control because they are private (or protected) to your
user control class. In order to access those variables,
you must set them up as PROPERTIES of your user control
class.
Dune Says:
So, the code behind for your user control class should
look something like this:
Dune Start Code Example:
Public MustInherit Class Header
Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As
System.Web.UI.WebControls.Literal
#Region " Web Form Designer Generated Code "
...
#End Region
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
...
End Sub
End Class
Dune End Code Example
Dune Says:
Note that in the code-behind, you must declare the literal
control (pageTitle) that you have put in your html using
the line:
Dune Start Code Example:
Protected WithEvents pageTitle As
System.Web.UI.WebControls.Literal
Dune End Code Example
Dune Says:
Ok, now you must add a new property so you can reference
the pageTitle literal control, here is what the code-
behind of your user control should look like with the new
property:
Dune Start Code Example:
Public MustInherit Class Header
Inherits System.Web.UI.UserControl
Protected WithEvents pageTitle As
System.Web.UI.WebControls.Literal
Public Property PageTitle() As String
Get
Return pageTitle.Text
End Get
Set(ByVal Value As String)
pageTitle.Text = Value
End Set
End Property
#Region " Web Form Designer Generated Code "
...
#End Region
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
...
End Sub
End Class
Dune End Code Example
Dune Says:
Right, now you should have everything you need. So, in the
code-behind of your web form you can now say:
Dune Start Code Example:
Header1.PageTitle = "blah"
Dune End Code Example
Dune Says:
And that should all work.
More info on Properties:
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/vblr7/html/vborivblangreftopnode.asp
More info on User Controls in general:
http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpconwebformsusercontrols.asp