Link to home
Start Free TrialLog in
Avatar of fwong1
fwong1

asked on

Repeater item if statement

Hi,

I currently have a repeater item, that displays a text box and then a description of an item next to it with a link.  not ll of the items have a link though, so I need to create an if statement in the repeater to oly show the link version if the database field url is not null.  all my attempts so far have not worked.  here is the code for the basic link.  any help/guidance is appreciated.  all my attempts have ended with the Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. errer.

The area in question is simply the url string link.

td><asp:TextBox ID=txtBox Width=50 CssClass="CenterItem" runat=server ></asp:TextBox> <asp:HiddenField ID="hdnID" Value=<%# Eval("ID").ToString() %> runat=server /></td>
                                            <td><a href="<%# Eval("url").ToString() %>"><%# Eval("title").ToString() %></a> - <%# Eval("NumPerBox").ToString() %> per box
                                                <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtBox" MinimumValue="0" MaximumValue="1500" ErrorMessage="Number must be between 0 and 1500."></asp:RangeValidator></td>

Open in new window

Avatar of prairiedog
prairiedog
Flag of United States of America image

You can use Label to display URL like this:
<td><asp:Label ID ="lblURL" runat="server"><a href='<%# Eval("url").ToString() %>'><%# Eval("title").ToString() %></a></asp:Label></td>

Open in new window

Avatar of fwong1
fwong1

ASKER

That will shorten my code somewhat, but I am not following how that will help creating an if statement to make the title not a url if the database has no url value?
Have you tried that code yet? If the database has no URL value, no hyperlink will be created at all, so you do not need a if statement.
Avatar of fwong1

ASKER

When I place the code in it does the same as mine did before.  I creates a blank link, that if clicked reloads the page back to the first view...
>>>I creates a blank link
Are you sure you placed my code in the right place? You cannot use <a> in your case, but you have to use Label and then assign a "<a hre...." string to the label. If Eval("url") is empty, then no link will be created. See my attached screen shot of a repeater using Northwind's Suppliers table.
One thing you need to keep in mind is that the URL needs to be a full URL if the link is for an external site, for example, "http://www.google.com", instead of "www.google.com".
2008-06-17-110014.jpg
Avatar of fwong1

ASKER

First, thanks for the help.  I'm going to show you 2 screen prints...

This one here is what I need,  this is what I could do with the old ASP... I am upgrading everything to the ASP .ET 2.0, but I am stuck here....
1.jpg
Avatar of fwong1

ASKER

And here is what I am getting
2.jpg
This is not what you asked in your question, though.
ASKER CERTIFIED SOLUTION
Avatar of fwong1
fwong1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial