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!)
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;
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!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With