conditionaly combine databound fields?

G

Guest

I have a Gridview ItemTemplate which displays company info as follows
<ItemTemplate>
<asp:Label ID="WEmployerLabel" runat="server" Text='<%# Bind("Employer")
%>'></asp:Label><br />
<asp:Label ID="WAddress1Label" runat="server" Text='<%#
Bind("WAddress1") %>'></asp:Label><br />
<asp:Label ID="WCityLabel" runat="server" Text='<%# Bind("WCity")
%>'></asp:Label><br />
<asp:Label ID="WPhoneTextLabel" runat="server" Text="Phone:
"></asp:Label><asp:Label ID="WPhoneLabel" runat="server" Text='<%#
Bind("WPhone") %>'></asp:Label>
</ItemTemplate>

The problem is that if a column is blank the <br/> is still appended so the
cell is taller then it needs to be and I have less space to display more
rows. And in this data there are a lot of rows one or more blank columns.

Is there any way to test if a field is blank and not include the field or
the <br /> if it is? Not sure if this can be done in the sql select statement.

Thanks for any suggestions.
 
G

Guest

In SQLServer a select statment along the lines of:

"Select Employer + '<br />' AS Employer, Address1 + '<br />' AS Address1
from tablename"

should work, but you might have to use a literal control to get the HTML
rendered in the browser, rather than dispalyed as text.
 
G

Guest

Replace the instances of <br /> markup within the ItemTemplate with:
<%#Eval("FieldName")==DBNull.Value?"":"<br/>"%>
 
G

Guest

I would never have come up with that even with a million monkeys typing
script beside me ;) It's like having Zip for GridView cells!

Thanks so much!
 
Joined
Dec 21, 2009
Messages
2
Reaction score
0
How to do this in VB

Took me a few min to convert this syntax from C# to VB so I figured I would post it here. I added if = "" b/c i keep default values in db.

<%#If(Eval("fieldname").Equals(DBNull.Value) Or Eval("fieldname") = "", "", "<br/>")%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top