Getting the parent name inside a user control in ASP.NET (VB.NET)
-
Good afternoon. For some reason, I have run into a brick wall trying to figure this out. I have a user control on a WebForm. I want to find out the name of WebForm on which the control is placed. I know that in WinForms, you can get the parent name by using the following convention (VB.NET) Dim mstrWhichForm as String = Me.FindForm().ToString The above will give you {NameOfProject}.{NameOfForm}, Text: {TextOfForm} I know that in WebForms you have access to Page (System.UI.Web.Page) within the control but for the life of me, I cannot figure out how to get the same type of information from the WebForms environment. I am pretty sure that it is something easy that I am missing, but I figure I would post just in case somebody else knew the answer. Thanks, Jim.
-
Good afternoon. For some reason, I have run into a brick wall trying to figure this out. I have a user control on a WebForm. I want to find out the name of WebForm on which the control is placed. I know that in WinForms, you can get the parent name by using the following convention (VB.NET) Dim mstrWhichForm as String = Me.FindForm().ToString The above will give you {NameOfProject}.{NameOfForm}, Text: {TextOfForm} I know that in WebForms you have access to Page (System.UI.Web.Page) within the control but for the life of me, I cannot figure out how to get the same type of information from the WebForms environment. I am pretty sure that it is something easy that I am missing, but I figure I would post just in case somebody else knew the answer. Thanks, Jim.
Hi there, Basically, the
FindForm
method gives you the fullname of the form and the value of theText
property. However, thePage
classs does not have anyText
property, it just has aTitle
element defined in the markup of the page, so I think you may be interested in the value of this element. There are two things here that you want to know at runtime: + The fullname of the Page object, it can get done via thePage
property of the control as you already know:string fullname = this.Page.GetType().BaseType;
+ The inner text of the title element, here I do that by first declaring a public
Title
property for each page.protected System.Web.UI.HtmlControls.HtmlGenericControl title;
public string Title
{
get{return title.InnerText;}
}The title element in the html editor is something like this:
<title runat="server" id="title"> Page Title</title>
Then this value can be easily retrieved at runtime:
Page page = this.Page;
Type type = page.GetType();
string title = type.GetProperty("Title").GetValue(page, null) as string;And what you need is something like:
string info = fullname + ", Title: " + title;
You now can easily wrap it up in a helper method. In addition, you can also create your own custom base class and override the
ToString
method. This method basically does the same thing as above. -
Hi there, Basically, the
FindForm
method gives you the fullname of the form and the value of theText
property. However, thePage
classs does not have anyText
property, it just has aTitle
element defined in the markup of the page, so I think you may be interested in the value of this element. There are two things here that you want to know at runtime: + The fullname of the Page object, it can get done via thePage
property of the control as you already know:string fullname = this.Page.GetType().BaseType;
+ The inner text of the title element, here I do that by first declaring a public
Title
property for each page.protected System.Web.UI.HtmlControls.HtmlGenericControl title;
public string Title
{
get{return title.InnerText;}
}The title element in the html editor is something like this:
<title runat="server" id="title"> Page Title</title>
Then this value can be easily retrieved at runtime:
Page page = this.Page;
Type type = page.GetType();
string title = type.GetProperty("Title").GetValue(page, null) as string;And what you need is something like:
string info = fullname + ", Title: " + title;
You now can easily wrap it up in a helper method. In addition, you can also create your own custom base class and override the
ToString
method. This method basically does the same thing as above.