Finding the name of the page holding the User Controlhttp://forums.asp.net/t/955261.aspx/1?Finding+the+name+of+the+page+holding+the+User+ControlFri, 20 Jan 2006 17:36:34 -05009552611173635http://forums.asp.net/p/955261/1173635.aspx/1?Finding+the+name+of+the+page+holding+the+User+ControlFinding the name of the page holding the User Control <p>I have created a new new Web User Control that&nbsp;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.</p> <p>How would I&nbsp;get the name of the page that the User Control resides on?</p> <p>&nbsp;</p> 2006-01-19T19:09:40-05:001173715http://forums.asp.net/p/955261/1173715.aspx/1?Re+Finding+the+name+of+the+page+holding+the+User+ControlRe: Finding the name of the page holding the User Control <font size="2">That depends on how your page is named.&nbsp; If you are using the ID property of it, then you can just pull the Page.ID to get at it.<br> <br> If you have a custom Page object that defines its name, you will first have to cast it to your custom type.&nbsp; Both code lines below are defined somewhere in the User Control's codebehind.<br> <br> Using the ID property of the page:<br> <font face="Courier New">&nbsp;&nbsp;&nbsp; string pageID = this.Page.ID;</font><br> <br> Using the Name property of your custom page:<br> <font face="Courier New">&nbsp;&nbsp;&nbsp; string pageID = ( ( myCustomPage ) Page ).CustomName;</font><br> <br> bill<br> </font> 2006-01-19T20:13:31-05:001173723http://forums.asp.net/p/955261/1173723.aspx/1?Re+Finding+the+name+of+the+page+holding+the+User+ControlRe: Finding the name of the page holding the User Control Thanks Bill. I should be able to get it using Page.ID, as I had tried that prior to posting, but continue to get null. Not sure why. 2006-01-19T20:25:27-05:001174044http://forums.asp.net/p/955261/1174044.aspx/1?Re+Finding+the+name+of+the+page+holding+the+User+ControlRe: Finding the name of the page holding the User Control <p><font size="2">When are you setting the Page ID property?&nbsp; Normally you would set this in the Page constructor method or by overriding the&nbsp;ID property.&nbsp; I am a fan of the overriding ID property.</font></p> <p><font size="2">It allows you do this</font></p> <p><font face="Courier New" size="2">public override string ID <br> { <br> &nbsp;&nbsp;&nbsp;get { return &quot;MyPageID&quot;; }<br> }</font></p> <p><font size="2">or based on the name of the page class, using reflection.</font></p> <p><font face="Courier New" size="2">public override string ID<br> {<br> &nbsp;&nbsp;&nbsp;return this.GetType().FullName; <br> }<br> </font></p> <p><font size="2">bill<br> </font></p> 2006-01-20T05:11:17-05:001174543http://forums.asp.net/p/955261/1174543.aspx/1?Re+Finding+the+name+of+the+page+holding+the+User+ControlRe: Finding the name of the page holding the User Control <p>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.</p> <p>Currently, I'm using:</p> <font size="2"> <p></font><font color="#0000ff" size="2">string</font><font size="2"> virtualPath = Request.Path.ToString();</p> <p></font><font color="#0000ff" size="2">string</font><font size="2"> pageName = virtualPath.Substring(virtualPath.LastIndexOf(&quot;/&quot;)&#43;1);</p> </font> 2006-01-20T16:24:16-05:001174662http://forums.asp.net/p/955261/1174662.aspx/1?Re+Finding+the+name+of+the+page+holding+the+User+ControlRe: Finding the name of the page holding the User Control <p><font size="2">You can wrap that functionality up in the overridden Page.ID property.&nbsp; Another consideration to use would be interfaces.&nbsp; You could define a marker interface on the page and then look for that marker in your user control to determine what gets shown.</font></p> <font size="2"><font face="Courier New">public interface IPageThatShowsExtraTextBoxes<br> {<br> &nbsp;&nbsp;&nbsp; //marker interface no implementation<br> }<br> <br> //implement the interface on the page that should show the extra stuff.<br> public class MyPage : Page, IPageThatShowsExtraTextBoxes<br> <br> //in your user control you could look for that marker.<br> if ( this.Page is IPageThatShowsExtraTextBoxes )<br> {<br> &nbsp;&nbsp;&nbsp; ExtraTextBox1.Visible = true;<br> &nbsp;&nbsp;&nbsp; ExtraTextBox2.Visible = true;<br> &nbsp;&nbsp;&nbsp; ExtraTextBox3.Visible = true;<br> }</font><br> <br> bill<br> <br> </font> 2006-01-20T17:36:34-05:00