Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If then statement with DataBinder.Eval

Status
Not open for further replies.

skendrick

Technical User
Mar 31, 2003
34
0
0
Is it possible to do an If then Statement and show something only if the DataBinder.Eval has info in it? What I am trying to achive is by having a client name in a list with a hyperlink so that you can click the name and go to their website. Or if the client doesn't have a link then it will just display the name and if you try to click it then it doesn't click since it's not a hyperlink. I have used a data reader and binded the data and used the asp repeater control to repeat the clients in the database. That works fine.
Code:
	<alternatingitemtemplate>
		<a href="[URL unfurl="true"]http://<%#[/URL] DataBinder.Eval(Container.DataItem, "client_Website") %>" target="_blank"><%# DataBinder.Eval(Container.DataItem, "client_Name") %></a><br>
		<%# DataBinder.Eval(Container.DataItem, "client_Comment") %>
	</alternatingitemtemplate>

Now how this works is if the client has a link it will work. If no link then it will just give you I have to add http:// because in the database it's just the domain name with out the http://

I am new to asp.net only used it for about a week now. I have gotten everything else to work just getting this to work is my problem now. Does any one have any ideas?

Thanks in advance
Sterling
 
How about within your SQL Statement?

This is a guess using a table structure i had in place...
Code:
SELECT  t1.ContactID, t1.ContactFName + ' ' + t1.ContactLName AS ClientName,
(SELECT ContactID = CASE WHEN contact[URL unfurl="true"]WWW IS[/URL] NULL THEN '' ELSE '[URL unfurl="true"]http://'[/URL] + contact[URL unfurl="true"]WWW END[/URL] FROM hs_tblContact WHERE contactID = t1.ContactID) AS client_web
FROM hs_tblContact t1
Then change your <a> tag to an asp:Hyperlink

<asp:HyperLink id=hlLinkOrName runat=server class=hyper
text='<%# DataBinder.Eval(Container.DataItem, "client_web") %>' navigateURL='<%# DataBinder.Eval(Container.DataItem, "client_web") %>' />

So the thought here is that if the navURL is nothing, it wont underline?

This will hopefully get you started at least. Sorry i dont have a page up that i can test the hyperlink completely.
 
Hi,

If they don't have a site then in the db field you could write none(or whatever) and then do;

<ItemTemplate>
<%# FormatHyperlink(Container.DataItem( "client_Name")) %>
</ItemTemplate>


Function FormatHyperlink(ByVal client_Name As String) As String

If client_Name <> "None" Then
Return "<a href = ' & client_Name & "' target='_blank'>" & client_Name & "</a>"
ElseIf client_Name = "None" Then
Return client_Name

Else
End If
End Function

Cheers,

JB

----------------------------------------------------------------------------------------
 
Thank you both for responding. I will try out both. If I have any problems I'll post back.

Thanks again,
Sterling
 
Is there any way to use sirjon's method without having to fill in the null field with something in the database? Is there any way to make the asp.net page fill in the null fields with something so that you don't get the dbnull error?

Thanks,
Sterling
 
You can use the IsDBNull() test function to check for it in VB (not sure about C#).

Also, a <%# block %> just expects a single command to return output, so if your conditional is simple enough, try the a ternary operator, (condition ? truepart : falsepart) for C# or iif(condition, truepart, falsepart) for VB.

________________________________________
Andrew

I work for a gift card company!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top