How to change the Visibility div from a user control using C #

I really got attached to the web user control and changing the visibility of the <div> in the parent ASPX.

I have a shopping cart that is inside a user control, and on one of the pages that UC is included in, there is a <div> status that shows a summary of the contents of the cart. If the basket is empty, it shows another <div> .

Code in UC

 if (varCartStatus) { cartStatusTrue.Visible = true; cartStatusFalse.Visible = false; else { cartStatusTrue.Visible = false; cartStatusFalse.Visible = true; } 

All that I get now 'cartStatusTrue' does not exist in the current context. 'cartStatusFalse' does not exist in the current context.

How do I get UC to change the <div> visibility in parent ASPX?

Sorry, I'm very new to .net and C # and I'm completely lost (again!)

+4
source share
5 answers

Since the controls exist on the page, not the control, you should find them on the page:

 this.Page.FindControl("cartStatusTrue").Visible = varCartStatus; this.Page.FindControl("cartStatusFalse").Visible = !varCartStatus; 

Or similarly if they were in the parent control:

 this.Parent.FindControl("cartStatusTrue").Visible = varCartStatus; this.Parent.FindControl("cartStatusFalse").Visible = !varCartStatus; 

Of course, also make sure your divs have runat="server" and ID="cartStatusTrue" or ID="cartStatusFalse" .

Edit: Another possibility, which is likely to be improved, would be to move the task to hide the div on the aspx page. You can open varCartStatus as a property of the control and read this property on an aspx page. In your aspx.cs:

 this.cartStatusTrue.Visible = this.CartControl.CartStatus; this.cartStatusFalse.Visible = !this.CartControl.CartStatus; 
+6
source

You can use something like:

  this.Parent.FindControl("cartStatusTrue").Visible = true; this.Parent.FindControl("cartStatusFalse").Visible = false; 

This is because a β€œdiv” exists in the Parent (for example, page) of this user control.

Hope this helps!

+1
source

Hi As mentioned by gilly3, this should be fine. You can also use Attributes.Add ("style", "visibility: hiddden") if you want the div to be generated but not displayed.

+1
source

Since there are two more possibilities:

  • You have no control over WHERE, which you position on the content page and
  • You can use on other pages in the future

You should not do something like spelunk on the pages you are on. My suggestion was to break your cart into two controls - one to display the cart, and the other to display status information. Your contained page then used both controls, placed them at its discretion and interacted with them, however, for this you had to use events, initialize them, etc.

0
source

One way is to open controls through public properties or create a public method on the page that allows you to get and set visibility values. Then the user control will need a method to handle open fields.

In the page code file, add the following methods

  public void CartStatusTrueVisible(bool Visible) { cartStatusTrue.Visible = Visible; } public void CartStatusFalseVisible(bool Visible) { cartStatusFalse.Visible = Visible; } 

Add these methods in the control code file.

  public void CartStatusTrueVisible(bool Visible) { ((_Default)Page).CartStatusTrueVisible(Visible); } public void CartStatusFalseVisible(bool Visible) { ((_Default)Page).CartStatusFalseVisible(Visible); } 
0
source

All Articles