Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Getting the parent name inside a user control in ASP.NET (VB.NET)

Getting the parent name inside a user control in ASP.NET (VB.NET)

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netwinformsdesign
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jimf1inmd
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • J jimf1inmd

      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.

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, Basically, the FindForm method gives you the fullname of the form and the value of the Text property. However, the Page classs does not have any Text property, it just has a Title 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 the Page 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.

      J 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, Basically, the FindForm method gives you the fullname of the form and the value of the Text property. However, the Page classs does not have any Text property, it just has a Title 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 the Page 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.

        J Offline
        J Offline
        jimf1inmd
        wrote on last edited by
        #3

        Thanks, I knew about but it slipped my mind last week. Also, I figured there must be an easier way. I will go through the code later today or tomorrow and I will repost if I have any questions. Thanks again, Jim. </x-turndown>

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups