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 February 7th, 2008, 01:30 PM
Authorized User
 
Join Date: Jan 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hiding Formview If No Results

Hello. I've got a FormView that outputs several variables into a paragraph. Is there any way to hide this control (and the entire paragraph that is inside it) if the data source that the control is using returns no results (or maybe certain results)? I was thinking maybe I should use the data binding function of the control, but I couldn't find out how to refer to any of the result set fields from the data binding function. Thank you! :)

 
Old February 9th, 2008, 08:23 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Doesn't the FormView hide itself when there's no data?

I just did a quick test and when I have no items in the data source, I don't see the FormView's contents appear, even though I had set up a <HeaderTemplate>

That said, you can do what you want by handling the events of the data source control. How to do this depends on the data source. With a SqlDataSource control you can handle the Selected event and test the AffectedRows property of the e argument:
Code:
protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
  FormView1.Visible = e.AffectedRows > 0;
}
Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old February 11th, 2008, 10:18 AM
Authorized User
 
Join Date: Jan 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now that I think about it, it does not display when there are no results. I have some records though, that have empty strings in their fields. When that is the case, how can I make the control invisible? It seems that maybe the datasource doesn't treat empty strings as nulls? Thanks for your help.

 
Old February 11th, 2008, 01:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Null is indeed not the same as an empty string. However, my guess is that even with null values they would still show up, because there is still a row. Whether or not your row consists of null values doesn't matter, a row is a row.

Depending on your data structure, you can filter them out at the database level. Alternatively, hook into events like RowDataBound of your data-aware control and filter out the relevant rows there.

Without knowing more of your setup / code, it's difficult to recommend something more accurate.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
FormView Question mikener ASP.NET 2.0 Professional 9 September 24th, 2008 05:39 PM
formview mcarol44 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 5 January 5th, 2008 06:44 AM
FormView Control lokraj ASP.NET 2.0 Basics 0 September 11th, 2007 08:51 AM
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM
Inserting in two formview shahzadgodil ASP.NET 2.0 Professional 2 October 18th, 2006 11:33 AM





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