User Control needs to refer to Page

J

Joe Fallon

I have a user control which can appear on many pages.
The control needs to know which page it is on so it can render itself
correctly.
What is the best way for the control to find out what page is being
requested?
Code snippet would be great.

Thanks!
 
R

Raterus

You could set a property of the usercontrol on the calling page.

<my:usercontrol runat="server" pageimon="blah.aspx" />
 
F

Felbrigg

Hey man try this, it's been givin me good vibes for weeks.

string pagename = this.Page.Request.CurrentExecutionFilePath;

int pos = pagename.LastIndexOf("/");

pagename = pagename.Substring(pos+1,pagename.Length - pos - 1);
 
P

Peter Rilling

You could either refer to the current context (I think "this" will get you
access to the context in a UC). Or you could pass a reference to the parent
page when the control is created so that you have direct access.

A better way might be to define methods and properties for the usercontrol
and define the state using attribute on the HTML tags. This way the control
does not need to know what page it is one, but rather just needs to know its
state. What happens if you add a new page that requires the usercontrol, do
you always want to update the control, or just pass in parameters?
 
D

Dan

Or even simpler:

Dim pagename As String =
System.IO.Path.GetFileName(Page.Request.CurrentExecutionFilePath))
 
J

Joe Fallon

Lots of good ideas.
Thanks!
--
Joe Fallon


Dan said:
Or even simpler:

Dim pagename As String =
System.IO.Path.GetFileName(Page.Request.CurrentExecutionFilePath))
 

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,007
Latest member
obedient dusk

Latest Threads

Top