Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 11th, 2008, 12:16 PM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , , .
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!
Reply With Quote
  #2 (permalink)  
Old September 11th, 2008, 12:33 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
  #3 (permalink)  
Old September 16th, 2008, 12:09 PM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , , .
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

Reply With Quote
  #4 (permalink)  
Old September 16th, 2008, 01:53 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
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
Reply With Quote
  #5 (permalink)  
Old September 21st, 2008, 08:00 PM
Authorized User
Points: 195, Level: 3
Points: 195, Level: 3 Points: 195, Level: 3 Points: 195, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2006
Location: , , .
Posts: 42
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thank you very much - I will try this.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:05 PM.


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