Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 11th, 2008, 12:16 PM
Authorized User
 
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default Codebehind for conditional visibility

I suspect that the answer to this is going to be so simple that I will kick myself when I see it, but I can seem to figure it out...

I have a shopping cart engine that has fields for both Price and SalePrice. Right now the Price displays just fine, and the SalePrice displays as well if I include it inside the cart's ItemTemplate tag:

Code:
<dotnetCART:DisplayCatalog>
<ItemDetailsTemplate>
<asp:label id="myPrice" Text="Price:" runat="server" />
<%#Container.Price%>
<br />
<%#Container.SalePrice%>
</ItemDetailsTemplate>
</dotnetCART:DisplayCatalog>
I would like to arrange it so that the cart only shows the SalePrice when there is an actual value in the database's SalePrice field for that product's row. The aspx page code could look something like this:

Code:
<asp:panel id="pnlSalePrice" runat="server" visible="false">            
<asp:label id="mySalePrice" Text="Sale Price:" runat="server" />
<b>
<%#Container.SalePrice%>
</b>
</asp:panel>
What would the code behind for this look like to switch this panel "on" when the SalePrice does not equal null or $0.00?

Thanks for any help!
 
Old September 11th, 2008, 12:33 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I think you should be able to databind the visible property like this:

<asp:panel id="pnlSalePrice" runat="server" visible='<%# CanShowPanel(Container.SalePrice) %>'>

Then, you could create a protected method in the code behind that does the test:

protected bool CanShowPanel(object salePrice){
   return ((decimal)salePrice) != 0M;
}

You could put the test logic in the data binding code, but that gets things messier and you loose the compile type checking. It's not bulletproof, but at least it's better than clumsy code blobs in the markup.

-Peter
compiledthoughts.com
 
Old September 16th, 2008, 12:09 PM
Authorized User
 
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thank you, this worked perfectly! So that I understand how it is doing this, the code
Code:
protected bool CanShowPanel(object salePrice)
{
   return ((decimal)salePrice) != 0M;
}
...is asking CanShowPanel to return true if salePrice is not equal to 0M - M being the suffix to show currency. If I were creating a function like this to read a boolean field instead of a price, would it just be
Code:
return ((decimal)salePrice) != 0;
?

Edit:

I notice that while this function works fine for the product details view of my catalog, when I try to apply to a higher level og my displayCatalog component (a display of all products in a category), I get this error:

Conversion from string "" to type 'Decimal' is not valid.
100107
DisplayCatalog


...any idea why it might be doing this?

Thanks

 
Old September 16th, 2008, 01:53 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

The M denotes that the literal value (0) is to be considered a decimal, versus an integer.

Testing for bool just becomes:

   return (bool)someValue;

It's breaking because whatever value it is that you are passing into the method is a string, not a decimal and thus can not be converted.

A generally better way of going about this would be to simply pass the whole object you are dealing with to the method and let the method check the appropriate property. That way you also move the property part of the non-compiled code into compiled code:

   <asp:panel id="pnlSalePrice" runat="server" visible='<%# CanShowPanel(Container) %>'>

Then you convert "Container" to whatever the whole object is, then query its property:

protected bool CanShowPanel(object myObject){
   return ((MyObjectType)myObject).SalePrice != 0M;
}

Then you risk less if you change the properties, because that code will break on compiled if you change SalePrice for example.

-Peter
compiledthoughts.com
 
Old September 21st, 2008, 08:00 PM
Authorized User
 
Join Date: Oct 2006
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thank you very much - I will try this.






Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in calendar visibility premnaath ASP.NET 2.0 Professional 1 November 16th, 2007 09:25 AM
visibility dynamic in report ermy78 Access 2 December 18th, 2006 06:19 PM
Chart Visibility baaul Reporting Services 1 September 28th, 2006 03:53 PM
Visibility Issue braheem ASP.NET 1.0 and 1.1 Basics 1 July 14th, 2005 05:20 PM
Procedure visibility rgerald Access VBA 6 December 10th, 2003 05:03 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.