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. Referencing user control in Place holder?

Referencing user control in Place holder?

Scheduled Pinned Locked Moved ASP.NET
questiontutorial
4 Posts 3 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.
  • M Offline
    M Offline
    mittalpa
    wrote on last edited by
    #1

    Hi In the page load of main.aspx, I am adding a user control in a place holder at runtime. As follow: ph.Controls.Add(CType(Me.LoadControl("/lpages/Login.ascx"), lpages.Login)) Here 'lpages' is the name of project. 'login.ascx' is the user control. 'ph' is the place holder. In the user control, I have a public variable for example "strName". I want to access this variable from main.aspx after user control's addition. How can I refer strName from main.aspx code-behind? Please advice Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---

    M J 2 Replies Last reply
    0
    • M mittalpa

      Hi In the page load of main.aspx, I am adding a user control in a place holder at runtime. As follow: ph.Controls.Add(CType(Me.LoadControl("/lpages/Login.ascx"), lpages.Login)) Here 'lpages' is the name of project. 'login.ascx' is the user control. 'ph' is the place holder. In the user control, I have a public variable for example "strName". I want to access this variable from main.aspx after user control's addition. How can I refer strName from main.aspx code-behind? Please advice Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---

      M Offline
      M Offline
      Mariusz Wojcik
      wrote on last edited by
      #2

      First, give the control some name:

      c = Me.LoadControl("/lpages/Login.ascx");
      c.ID = "login";
      ph.Controls.Add(CType(c, lpages.Login))

      and then you need to find control FindControl("login"), cast it to the lpages.Login type and access properties. -- Mariusz 'mAv' Wójcik master e-software engineer (BPC)

      1 Reply Last reply
      0
      • M mittalpa

        Hi In the page load of main.aspx, I am adding a user control in a place holder at runtime. As follow: ph.Controls.Add(CType(Me.LoadControl("/lpages/Login.ascx"), lpages.Login)) Here 'lpages' is the name of project. 'login.ascx' is the user control. 'ph' is the place holder. In the user control, I have a public variable for example "strName". I want to access this variable from main.aspx after user control's addition. How can I refer strName from main.aspx code-behind? Please advice Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---

        J Offline
        J Offline
        Jesse Squire
        wrote on last edited by
        #3

        [edit]Bah... sorry, I'm a bit redundant here. :doh: mavus74.net beat me to the post as I was writing this.[/edit] There are two ways that come to mind. First, you could save a reference to the control that you're loading and then add it to the placeholder. [You'll have to forgive my VB if the syntax is off, I'm more of a C# kinda guy.]

        Dim myControl As lpages.Login = CType(Page.LoadControl("/lpages/Login.ascx"), lpages.Login) ph.Controls.Add(myControl)   myControl.strName = "The Control"

        You could also add your control to the placeholder, and then locate it positionally.

        ' Don't need the CType conversion when loading the control to add this way. ' It will be cast to System.Web.UI.Control when added, anyway.   ph.Controls.Add(Page.LoadControl("/lpages/Login.ascx"))   (CType(ph.Controls.Item(ph.Controls.Count - 1), lpages.Login)).strName = "The Control"

        The first way is preferable, as it cuts down the casting involved if you want to reference your control more then a single time. Another construct to be aware of is the FindControl method of System.Web.UI.Control. It will allow you to find a control in the collection of a Control or a Page by the desired control's id. The reason that I didn't suggest it here is that you aren't specifying an id when you add your control to the placeholder. Hope that helps. :)   --Jesse

        M 1 Reply Last reply
        0
        • J Jesse Squire

          [edit]Bah... sorry, I'm a bit redundant here. :doh: mavus74.net beat me to the post as I was writing this.[/edit] There are two ways that come to mind. First, you could save a reference to the control that you're loading and then add it to the placeholder. [You'll have to forgive my VB if the syntax is off, I'm more of a C# kinda guy.]

          Dim myControl As lpages.Login = CType(Page.LoadControl("/lpages/Login.ascx"), lpages.Login) ph.Controls.Add(myControl)   myControl.strName = "The Control"

          You could also add your control to the placeholder, and then locate it positionally.

          ' Don't need the CType conversion when loading the control to add this way. ' It will be cast to System.Web.UI.Control when added, anyway.   ph.Controls.Add(Page.LoadControl("/lpages/Login.ascx"))   (CType(ph.Controls.Item(ph.Controls.Count - 1), lpages.Login)).strName = "The Control"

          The first way is preferable, as it cuts down the casting involved if you want to reference your control more then a single time. Another construct to be aware of is the FindControl method of System.Web.UI.Control. It will allow you to find a control in the collection of a Control or a Page by the desired control's id. The reason that I didn't suggest it here is that you aren't specifying an id when you add your control to the placeholder. Hope that helps. :)   --Jesse

          M Offline
          M Offline
          mittalpa
          wrote on last edited by
          #4

          Thanks for your suggestions.. After applying them I am getting following exceptions: System.NullReferenceException: Object reference not set to an instance of an object -------This is what I wrote in code:------------ Dim objLogin As lpages.Login objLogin = DirectCast(FindControl("/lpages/Login.ascx"), Login) objLogin.strName = "tomandjerry" ----------------- Moment line 3 is executed, I get NulReference exception. Am I missing something? Please advice. Thanks Pankaj Follow your goals, Means will follow you ---Gandhi---

          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