ComfortablyNumb
ComfortablyNumb

Reputation: 1456

How to Change div Visibility from a User Control using c#

I have got myself really tied up with a web user control and the changing of a <div>'s visibility in the parent ASPX.

I have a shopping cart which is inside a User Control and within one of the pages the UC is included there is a status <div> which shows a summary of the cart contents. If the cart is empty it shows a different <div>.

Code in UC

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

All I get at the moment is 'cartStatusTrue' does not exist in the current context. 'cartStatusFalse' does not exist in the current context.

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

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

Upvotes: 4

Views: 11017

Answers (5)

gilly3
gilly3

Reputation: 91677

Since the controls exist in the page, and not the control, you have to find them in the page:

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

Or similarly, if they were in a parent control:

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

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

Edit: Another option that is probably a design improvement would be to move the job of hiding the div to the aspx page. You could expose varCartStatus as a property of the control and read that property from the aspx page. In your aspx.cs:

this.cartStatusTrue.Visible = this.CartControl.CartStatus;
this.cartStatusFalse.Visible = !this.CartControl.CartStatus;

Upvotes: 6

Cris Valenzuela
Cris Valenzuela

Reputation: 372

One way is to expose the the controls through public properties or create a public method on the page that allows you to Get & Set the visibility values. Then the User Control will need a method to manipulate the exposed 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;
    }

In the control code file add these methods

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

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

Upvotes: 0

n8wrl
n8wrl

Reputation: 19765

Since there are two more possibilities here:

  1. You don't have control over WHERE you're positioned on the containing-page and
  2. You could be used on OTHER pages in the future

You shouldn't do things like spelunk around on pages you're on. My suggestion would be to break your cart control up into two controls - one to render the cart and the other to display status information. Your containing page then used both controls, places them as it sees fit, and interacts with them however it needs to to consume events, initialize them, etc.

Upvotes: 0

Shashi
Shashi

Reputation: 71

Hi As mentioned by gilly3, it should be fine. You might as well use Attributes.Add("style", "visibility:hiddden") if you want the div to be generated but not to display.

Upvotes: 1

Praveen
Praveen

Reputation: 1449

You can use something like:

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

This is because, the "div" exists in the "Parent" (i.e. Page) of this user control.

Hope this helps!!

Upvotes: 1

Related Questions