I have created a new new Web User Control that I wish to use on 2 different pages. Certain controls on that User control will be hidden based upon the page that's using it.
How would I get the name of the page that the User Control resides on?
That depends on how your page is named. If you are using the ID property of it, then you can just pull the Page.ID to get at it.
If you have a custom Page object that defines its name, you will first have to cast it to your custom type. Both code lines below are defined somewhere in the User Control's codebehind.
Using the ID property of the page:
string pageID = this.Page.ID;
Using the Name property of your custom page:
string pageID = ( ( myCustomPage ) Page ).CustomName;
When are you setting the Page ID property? Normally you would set this in the Page constructor method or by overriding the ID property. I am a fan of the overriding ID property.
It allows you do this
public override string ID
{
get { return "MyPageID"; }
}
or based on the name of the page class, using reflection.
public override string ID
{
return this.GetType().FullName;
}
This gives me the name of the control, but I'm looking for the name of the page that the control resides on to make differences in the control based on the page.
You can wrap that functionality up in the overridden Page.ID property. Another consideration to use would be interfaces. You could define a marker interface on the page and then look for that marker in your user control to determine what
gets shown.
public interface IPageThatShowsExtraTextBoxes
{
//marker interface no implementation
}
//implement the interface on the page that should show the extra stuff.
public class MyPage : Page, IPageThatShowsExtraTextBoxes
//in your user control you could look for that marker.
if ( this.Page is IPageThatShowsExtraTextBoxes )
{
ExtraTextBox1.Visible = true;
ExtraTextBox2.Visible = true;
ExtraTextBox3.Visible = true;
}
steveba
Member
685 Points
137 Posts
Finding the name of the page holding the User Control
Jan 19, 2006 07:09 PM|LINK
I have created a new new Web User Control that I wish to use on 2 different pages. Certain controls on that User control will be hidden based upon the page that's using it.
How would I get the name of the page that the User Control resides on?
billrob458
Contributor
3329 Points
671 Posts
Re: Finding the name of the page holding the User Control
Jan 19, 2006 08:13 PM|LINK
If you have a custom Page object that defines its name, you will first have to cast it to your custom type. Both code lines below are defined somewhere in the User Control's codebehind.
Using the ID property of the page:
string pageID = this.Page.ID;
Using the Name property of your custom page:
string pageID = ( ( myCustomPage ) Page ).CustomName;
bill
steveba
Member
685 Points
137 Posts
Re: Finding the name of the page holding the User Control
Jan 19, 2006 08:25 PM|LINK
billrob458
Contributor
3329 Points
671 Posts
Re: Finding the name of the page holding the User Control
Jan 20, 2006 05:11 AM|LINK
When are you setting the Page ID property? Normally you would set this in the Page constructor method or by overriding the ID property. I am a fan of the overriding ID property.
It allows you do this
public override string ID
{
get { return "MyPageID"; }
}
or based on the name of the page class, using reflection.
public override string ID
{
return this.GetType().FullName;
}
bill
steveba
Member
685 Points
137 Posts
Re: Finding the name of the page holding the User Control
Jan 20, 2006 04:24 PM|LINK
This gives me the name of the control, but I'm looking for the name of the page that the control resides on to make differences in the control based on the page.
Currently, I'm using:
string virtualPath = Request.Path.ToString(); string pageName = virtualPath.Substring(virtualPath.LastIndexOf("/")+1);billrob458
Contributor
3329 Points
671 Posts
Re: Finding the name of the page holding the User Control
Jan 20, 2006 05:36 PM|LINK
You can wrap that functionality up in the overridden Page.ID property. Another consideration to use would be interfaces. You could define a marker interface on the page and then look for that marker in your user control to determine what gets shown.
public interface IPageThatShowsExtraTextBoxes{
//marker interface no implementation
}
//implement the interface on the page that should show the extra stuff.
public class MyPage : Page, IPageThatShowsExtraTextBoxes
//in your user control you could look for that marker.
if ( this.Page is IPageThatShowsExtraTextBoxes )
{
ExtraTextBox1.Visible = true;
ExtraTextBox2.Visible = true;
ExtraTextBox3.Visible = true;
}
bill