Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 December 16th, 2004, 09:30 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditional Display in a Datalist

I need to display data conditionally in one of my datalist columns.
Code:
<asp:TemplateColumn>
    <ItemTemplate>
      <%#Container.DataItem("retVal")%>
    </ItemTemplate>
</asp:TemplateColumn>
If retVal is a certain value, I don't want to display it. I tried using an if..then structure, but it errored out.

Thanks in advance for your comments.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old December 16th, 2004, 09:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Is retVal coming from a database? You could exclude it from your SQL.

 
Old December 16th, 2004, 09:38 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good point, Stu. I'll give that a try. However, I am still curious about the possible datalist technique.

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old December 16th, 2004, 09:39 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can add a function to the Code Behind file that examines the state of the object and returns whatever it is you want it to return:
Code:
public string TestValue (object myValue)
{
  if ((string) myValue == "Some Value")
  {
    return "":
  }
  else
  {
    return (string) myValue;
  }
}
You'll need to change the ASPX file accordingly:

<%# TestValue (Container.DataItem("retVal"))%>


HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 16th, 2004, 09:46 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 218
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, Imar - that did the trick!

Also, Stu...I gave the SQL solution a try, but without entirely excluding the entire record the technique would have been trouble.

Thanks to you all again!

- - - - - - - - - - - - - - - - - - - - - - -
In God we trust, everything else we test.
 
Old December 16th, 2004, 09:48 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, one more thing.

If the database returns a null value, you'll need to compare your value in the function against DBNull.Value

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old December 16th, 2004, 05:48 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Colonel,

For future reference....

You can embed some field condition logic in the SQL statement:

SELECT field1, field2, CASE field3 WHEN 'somevalue' THEN '' ELSE field3 END as field3 FROM sometable ...
 
Old December 17th, 2004, 05:17 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi there,
IMO,you could fetch all rows and then use DataView.RowFilter Property,then if you need again to change your condition,you wont need another hit to your datasource,
Code:
dv.RowFilter="retVal='Value1'";
//Rebind your datalist
_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.
 
Old December 19th, 2004, 02:04 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

mehdi,

Your point about using the DataView.RowFilter property is valid however it's somewhat impractical. With nearly all designs I've seen, the datasource (DataSet or DataTable) is not persisted across page hits because it is a very inefficient use of server memory (and introduces the problem of stale data). In all the ASP.NET applications I have constructed, whenever there is a change to the criteria for data, I requery the database, thus letting the DB engine perform the filtering of the data resulting in less data being returned over the database connection to the web server.

In addition, I think the original posted was implying that only a certain column's value should be suppressed if it meets a given condition. For this, RowFilter is of no use because it will hide the entire row.
 
Old December 19th, 2004, 10:36 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

I didnt write how we should persist data among several requests in my previous post(putting to cache is one of the ways),
however something I told not suitable for all situations,for example,
suppose we have a site doesnt change very much,also it could have many users hitting it every day,(like a page(grid) informs beautiful final exam grades(!),and suppose every student could sort it and find(filter) his grades)in my opinion using cache in such sites is the best choice,it could improve the performance of such sites very well ...
Quote:
quote:I think the original posted was implying that only a certain column's value should be suppressed if it meets a given condition. For this, RowFilter is of no use because it will hide the entire row.
if original poster wants that,he could use Imar's method(however,instead of showing a "" row we could hide it).

_____________________________
Mehdi.
software engineering student.
Looking for a good job for summer 2005.





Similar Threads
Thread Thread Starter Forum Replies Last Post
datalist display almotions ASP.NET 2.0 Basics 1 February 18th, 2008 03:19 AM
Conditional display/hide of images bertvanpet Crystal Reports 0 May 25th, 2007 02:37 AM
unable to display my datalist frresh C# 2005 0 November 14th, 2006 11:52 AM
Datalist does not display data sanjeet ADO.NET 1 August 8th, 2003 11:26 PM
add conditional DataList item jtyson ASP.NET 1.0 and 1.1 Basics 1 July 1st, 2003 01:10 PM





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