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. General Programming
  3. C#
  4. Find Control on webform

Find Control on webform

Scheduled Pinned Locked Moved C#
6 Posts 3 Posters 1 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.
  • T Offline
    T Offline
    Terick
    wrote on last edited by
    #1

    Hi, I have a button on my webform that I need to access from my user control code behind, not the page on which the button resides, but I'm not sure how I find it. I've thought it would be something like this, but this doesnt work: Button button=(Button)FindControl("btn"); Thanks!

    J 1 Reply Last reply
    0
    • T Terick

      Hi, I have a button on my webform that I need to access from my user control code behind, not the page on which the button resides, but I'm not sure how I find it. I've thought it would be something like this, but this doesnt work: Button button=(Button)FindControl("btn"); Thanks!

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Have you tried Page.FindControl("..")?

      T 1 Reply Last reply
      0
      • J J4amieC

        Have you tried Page.FindControl("..")?

        T Offline
        T Offline
        Terick
        wrote on last edited by
        #3

        Yes have tried that: Button button=(Button)Page.FindControl("btn"); The Page will return the correct page, but nothing gets assigned to button, so I am getting nullReferenceException error.

        T R 2 Replies Last reply
        0
        • T Terick

          Yes have tried that: Button button=(Button)Page.FindControl("btn"); The Page will return the correct page, but nothing gets assigned to button, so I am getting nullReferenceException error.

          T Offline
          T Offline
          Terick
          wrote on last edited by
          #4

          When I drill down into the error, into the Page property, the Page directs itself correctly and the button is listed under the [ASP.Page_aspx] base but there is no FindControl option listed. Any ideas as to why this is?

          1 Reply Last reply
          0
          • T Terick

            Yes have tried that: Button button=(Button)Page.FindControl("btn"); The Page will return the correct page, but nothing gets assigned to button, so I am getting nullReferenceException error.

            R Offline
            R Offline
            Rutvik Dave
            wrote on last edited by
            #5

            I hope that Button is not in the ContentPlaceHolder and you are not using MasterPages. Try Page.Parent.FindControl() If that button is inside the ContentPlaceHolder then stack trace and find the actual value for that button. something like xxxxxctl001_btn. then first find the content place holder (or any other container) using container c = Page.FindControl('container') and then c.FindControl('btn') PS: container means any container i.e. Panel / ContentPlaceHolder etc...

            T 1 Reply Last reply
            0
            • R Rutvik Dave

              I hope that Button is not in the ContentPlaceHolder and you are not using MasterPages. Try Page.Parent.FindControl() If that button is inside the ContentPlaceHolder then stack trace and find the actual value for that button. something like xxxxxctl001_btn. then first find the content place holder (or any other container) using container c = Page.FindControl('container') and then c.FindControl('btn') PS: container means any container i.e. Panel / ContentPlaceHolder etc...

              T Offline
              T Offline
              Terick
              wrote on last edited by
              #6

              Yes! That was my problem. I was not referencing the Master Page and ContentPlaceHolder. Thank you for pointing that out to me.

              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