How can a UserControl reference controls from another UC? RRS feed

  • Question

  • User695775321 posted

    I have a single ASPX page (it's not a MasterPage). On this page is a UserControl that basically consists of a GridView. Within this GridView are a couple other UserControls. The problem that I'm having is comunicating back and forth between the UC's. How is this done? The UC that contains the GridView is where most of the driving code is. I'm pretty new to ASP.Net and I'm quite certain that I'm lost in this code...which really isn't that much. Unfortunately, this application is setup so that the Debugger can't be used so I can't step through code like I normally would. I don't know why it's that way.

    Friday, August 24, 2018 1:45 PM

All replies

  • User475983607 posted

    User controls are just classes.  Classes expose information through properties and methods.   A good example is the TextBox.Text property. 

    Since ASP Web Forms is event based, it is common to read properties in response to an event like row binding.  ASP also has the FindControl method which can be used to find a server control within another server control container.  


    I've found that many folks on the forum implement user controls when there is no good reason.  Then they run into problems like this because they need to write more code to expose data to the outside.   Be sure that you are not over complicating the code. 

    Secondly, not being able to debug is odd.  IF you are running this project locally on development machine then make sure you have the solution configuration set to debug mode and not release.  There's a "Solution Configuration" dropdown in the main VS toolbar.

    Friday, August 24, 2018 1:59 PM
  • User695775321 posted

    Unfortunately, before I was told, I tried to build/rebuild the solution and it screwed things up pretty bad in Development. It took my boss over 4 hours to figure it out. Consequently, I can't use the debugger.

    Unfortunately, your response didn't quite help me enough. Is it possible for a UC to refer back to other controls from the calling UC or Page?

    Here is my scenario:

    GridInspect.ascx (this is the main usercontrol and it drives all the logic and contains the GridView (GV))

          UC1 - IQ_CMMLabel.ascx (usercontrol that occurs 5 times in each row of GV. It consists of 1 Label control and 1 Checkbox control)

    The flow of the logic is as such:

    1. A User selects a value from a DropDownList in Cell 19 of the GV. The Cell needs to turn green when a value is selected. This "enables" the Checkboxes in UC1. Database updates occur in the code-behind for UC1.
    2. Of the 5 UC1 uc's, a User can check only 1 of the Checkboxes. When this is done, the Cell needs to turn green as well. Database updates should occur in the code-behind when this action is complete.
    Friday, August 24, 2018 2:16 PM
  • User475983607 posted


    Unfortunately, your response didn't quite help me enough. Is it possible for a UC to refer back to other controls from the calling UC or Page?

    Again, you can use FindControl to, well, find a control within a container.  This is just an example...

    CheckBox cb = this.Parent.FindControl("theotherusercontrol").FindControl("thecheckbox") as CheckBox

    The trick is you have navigate the control tree.  If you are deep within the control tree then this.Parent moves up one container.  Then drill from that container to other controls.  One caveat is that the control hierarchy might not exist at that time.  It depending on where you are in the current page and control life cycle.

    IMHO, if a user control is reaching out to find other controls then you have a design bug.  That means the user control has a dependency on the existence if other controls.  That's why it is better to use properties to expose state to the containing controls.  The main parent container should orchestrate everything.

    As for your specific business logic requirements, that not something for a support forum unless you are willing to share your normal fee.

    Friday, August 24, 2018 3:23 PM