accessing control's members from aspx code.

M

Marko Rangel

I am interested in accessing a property from a use defined web control from
the codebehind of an aspx page.

If my web control has a property, say Name, how would I reference the
control in my aspx code so that I can access that instance of the control?

I beleive (though I have not tried) to create an instance by it's full
assembly name, but I want to use the one that is currently loaded in that
aspx page.

tia
marko
 
K

Ken Cox [Microsoft MVP]

Hi Marko,

You need to get a handle on the control itself and then you can mess with its
properties. Here's the idea, full code below:

Dim tpban As UserControl
Dim tpbanimage As System.Web.UI.WebControls.Image
tpban = Page.FindControl("Tpbanner1")
tpbanimage = tpban.FindControl("image1")
tpbanimage.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
tpbanimage.ID = "newid"

Does this help?

Ken


<%@ Control Language="vb" AutoEventWireup="false" Codebehind="tpbanner.ascx.vb"
Inherits="p733workev.tpbanner"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:image id="Image1" runat="server"
imageurl="http://www.gc.ca/images/englishbt.gif"></asp:image>


Public Class tpbanner
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 Image1 As System.Web.UI.WebControls.Image

'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

End Class


<%@ Register TagPrefix="uc1" TagName="tpbanner" Src="tpbanner.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="useusercntrl.aspx.vb" Inherits="p733workev.useusercntrl"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>useusercntrl</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">
<p>
<uc1:tpbanner id="Tpbanner1" runat="server"></uc1:tpbanner></p>
<p>
<asp:button id="Button1" runat="server" text="Change
Image"></asp:button></p>
</form>
</body>
</html>


Public Class useusercntrl
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
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

'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 Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim tpban As UserControl
Dim tpbanimage As System.Web.UI.WebControls.Image
tpban = Page.FindControl("Tpbanner1")
tpbanimage = tpban.FindControl("image1")
tpbanimage.ImageUrl = "http://www.gc.ca/images/francaisbt.gif"
tpbanimage.ID = "newid"
End Sub
End Class


--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/security_bulletins/ms03-026.asp



I am interested in accessing a property from a use defined web control from
the codebehind of an aspx page.

If my web control has a property, say Name, how would I reference the
control in my aspx code so that I can access that instance of the control?

I beleive (though I have not tried) to create an instance by it's full
assembly name, but I want to use the one that is currently loaded in that
aspx page.

tia
marko
 
M

Marko Rangel

I think I may have reposted, so sorry if this appears twice...

What I did, with your advice below, is have my user control registered in
the aspx page like so:
<%@ Register TagPrefix="ctl" TagName="controlname" Src="controlname.ascx"%>

Instead of creating a variable with System.Web.UI.WebControls.Image (code
below), I referenced my control. So the variable that holds my control
should know what routines/properties are available.

The next problem I have is that using the Page object, I could not find a
control named "controlname" (example above). How should I be referencing
this control if it is registered in the aspx page?

mr
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top