Hi nameless person,
I've put together a simple example that changes the appearance of the user
control's label and changes the location of an image as the main loads.
The full code is below, but the key part is this from the current .aspx page
( see the comments in line:
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Let's say this page needs to
' change Label2 of the user control
' and turn the image to French
Dim navcntrl As UserControl
Dim navlabel As Label
Dim navimgbtn As ImageButton
' Get an instance of the user control called Nav1
' by finding it inside the current page
navcntrl = Page.FindControl("Nav1")
' Now we have an instance of the user control
' find the label inside it using FindControl
navlabel = navcntrl.FindControl("label2")
' Change the color of the label that we just found
navlabel.BackColor = Color.ForestGreen
' Likewise get an instance of the image button
' by finding it within the user control
navimgbtn = navcntrl.FindControl("ImageButton1")
' Change the URL of the image control
navimgbtn.ImageUrl = "
http://www.gc.ca/images/francaisbt.gif"
End Sub
Does this help?
'---------------------------------------------
' usenav.aspx
<%@ Register TagPrefix="uc1" TagName="nav" Src="nav.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="usenav.aspx.vb"
Inherits="p4320work.usenav"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>usenav</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<uc1:nav id="Nav1" runat="server"></uc1:nav>
</form>
</body>
</HTML>
'usenav.aspx.vb
Public Class usenav
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Let's say this page needs to
' change Label2 of the user control
' and turn the image to French
Dim navcntrl As UserControl
Dim navlabel As Label
Dim navimgbtn As ImageButton
navcntrl = Page.FindControl("Nav1")
navlabel = navcntrl.FindControl("label2")
navlabel.BackColor = Color.ForestGreen
navimgbtn = navcntrl.FindControl("ImageButton1")
navimgbtn.ImageUrl = "
http://www.gc.ca/images/francaisbt.gif"
End Sub
End Class
' nav.ascx
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="nav.ascx.vb"
Inherits="p4320work.nav"
TargetSchema="
http://schemas.microsoft.com/intellisense/ie5" %>
<P>
<asp:Label id="Label1" runat="server" BackColor="#FFFFC0">One</asp:Label>
<asp:Label id="Label2" runat="server" BackColor="#FFFFC0">Two</asp:Label>
<asp:Label id="Label3" runat="server" BackColor="#FFFFC0">Four</asp:Label>
<asp:Label id="Label4" runat="server" BackColor="#FFFFC0">One</asp:Label>
<asp:Label id="Label5" runat="server"
BackColor="#FFFFC0">Five</asp:Label></P>
<P>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="
http://www.gc.ca/images/englishbt.gif"></asp:ImageButton></P>
'nav.ascx.vb
Public Class nav
Inherits System.Web.UI.UserControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Label4 As System.Web.UI.WebControls.Label
Protected WithEvents Label5 As System.Web.UI.WebControls.Label
Protected WithEvents ImageButton1 As
System.Web.UI.WebControls.ImageButton
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub ImageButton1_Click(ByVal sender As System.Object, ByVal e As
System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
Label4.Text = "Changed this!"
End Sub
End Class