referencing the page from a control

D

DC Gringo

I have an myPage.aspx with a myPage.ascx user control. The user control is
the main header of the page. I have some conditional case code in the user
control that I use which depends on a public variable "sectionID" set int he
myPage.aspx's code-behind. How can I reference that variable in my control
dynamically? I can hard card in the name of the myPage.aspx.vb class but
that doesn't help me if the control is used within many different pages.

---------------------------------------------------
In my myPage.aspx.vb, I have:

Public sectionID As String = "home"




---------------------------------------------------
In my myPage.ascx, I have:


Public Sub theSectionID()

Select Case <WHAT GOES HERE???>.sectionID
Case "home"
sectionHeaderLabel.Text = "Welcome section"
Case "about"
sectionHeaderLabel.Text = "Another section"
Case Else
sectionHeaderLabel.Text = "Error"
End Select

End Sub
 
M

Marina

You would need to cast the Page property of the user control, to the type of
your Page (whatever that name is). This means you would need a reference to
the assembly that contains your page in the project for the user control (if
they are separate project0> Once you have a variable of the right type
pointing to the page, the sectionID variable should be available since it is
public.

Another way to do this, is to use reflection on the Page property of the
user control, look for a member called 'sectionID', and then
programmatically retrieve its value. This is kind of messy.

The best solution, would probably to expose a method or property on your
user control, that the page can call to pass in the sectionID. The user
control would store it for later access.

Either that, or have that be a parameter to theSectionID method, if the page
is doing the calling.
 
D

DC Gringo

I'm trying to expose a method on the user control and pass in the sectionID.
It's not working and I get error: "Argument not specified for parameter
'sectionID' of 'Public Sub theSectionID(sectionID As String)'."

What I have is:


myPage.aspx has:
--------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim sectionID As String
sectionID = "home"
mainHeader1.theSectionID(sectionID)
End Sub



myPage.ascx has:
--------------------------
Public Sub theSectionID(ByVal sectionID As String)
Select Case sectionID
Case "home"
sectionHeaderLabel.Text = "Welcome"
Case "about"
sectionHeaderLabel.Text = "About"
Case Else
sectionHeaderLabel.Text = "Error"
End Select
End Sub
 
R

Rick Spiewak

You can have a public function or sub in myPage.ascx which is called during
the page_load event of the hosting page. Pass it the value there, or if you
want it to have more access, pass it the page.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top