Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
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 27th, 2008, 12:41 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default asp:hyperlink in repeater visibility

Hello,

I have an ASP:repeater object in my page and I'm trying to control the visiblity of an ASP:hyperlink object within the repeater based on whether the specific record which is returned has a website link or not.

Obviously if the database record contains a website link then I want the repeater to display the hyperlink. If it doesn't then I do not want the repeater to display it. Any suggestions?

 
Old February 27th, 2008, 03:29 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

There are a few ways to do this. First of all, you could embed an expression in the Visible property that checks whether the string is empty:
Code:
<asp:HyperLink runat="server" NavigateUrl='<%# Eval("Url") %>' 
    Visible='<%# Not System.String.IsNullOrEmpty(Eval("Url")) %>'>
  Go to web site
</asp:HyperLink>
This code checks if the Url field contains a hyper link. If not, the entire HyperLink is hidden.

Alternatively, you could hook into the ItemCreated (or ItemDataBound) event of the Repeater, find the HyperLink, look at e.DataItem (that represents the data you are binding ) and then determine its visibility. For example, when binding to a Company object that has a Url property you could do something like this:
Code:
Protected Sub Repeater1_ItemCreated(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) _ 
    Handles Repeater1.ItemCreated
  Select Case e.Item.ItemType
    Case ListItemType.AlternatingItem, ListItemType.Item
      Dim myCompany As Company = CType(e.Item.DataItem, Company)
      ' No need to CType to a HyperLink because Control has the 
      ' Visible property
      Dim myLink As Control = e.Item.FindControl("hyperUrl")
      myLink.Visible = Not String.IsNullOrEmpty(myCompany.Url)
  End Select
End Sub
This code uses FindControl to locate the HyperLink and then sets its visibility.

If your data is different (e.g. you're binding to a DataSet) you need a different cast (CType), probably into a DataRowView.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old February 27th, 2008, 03:52 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I tried what you suggested, but something isn't quite right. This is what I have as my page code.

Code:
<asp:Hyperlink NavigateUrl="video.aspx?id=<%# DataBinder.Eval(Container.DataItem, "iVideoId") %>"
Text="Watch video" Visible='<%# Not System.String.IsNullOrEmpty(Eval("iVideoId")) %>'></asp:Hyperlink>
Strange thing is, when I added the part that you suggested to the 'Visible' parameter I only got 1 of my 6 repeater records returned and the link actually isn't invisible.
 
Old February 27th, 2008, 04:06 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Do you have other data in your Repeater as well? Or just the hyperlink? To make it easier to see what happens, try adding another control to the ItemTemplate like a Label.

And what does iVideoId contain? Do you know what data it contains exactly for the records you are displaying?

And this doesn't look right either:
Code:
NavigateUrl="video.aspx?id=<%# DataBinder.Eval(Container.DataItem, "iVideoId") %>"
Shouldn't that be:
Code:
NavigateUrl='<%# "video.aspx?id=" & DataBinder.Eval(Container.DataItem, "iVideoId") %>'
Finally, can you add a few line breaks here and there when you post code here at P2P? The page is about 6 feet wide now.... ;)

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old February 27th, 2008, 04:17 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Yes, there are about 6 different instances of <%# DataBinder.Eval(Container.DataItem, "someField") %> in my repeater.

The 'iVideoId' should contain an integer value, being the id to pass to my video player to roll a specific video. At this stage all iVideoId's in my database are null because I want to test that at this stage the hyperlink should not be displaying for any of the records without a video ID.

(Sorry about the code, I usually do insert line breaks for that reason, but I forgot).

 
Old February 27th, 2008, 04:36 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

In that case, you may need to compare against DBNull.Value, the database counterpart of null / Nothing:

Visible='<%# Eval("Url") IsNot DBNull.Value %>'

It's probably better to go the ItemCreated route as it gives you more flexibility and better control over the data you are testing.

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old February 27th, 2008, 05:40 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks Imar, that seems to have sorted out my link visibility problems, however the code now does not appear to be populating the querystring of my NavigateUrl.

Code:
<asp:Hyperlink runat="server" NavigateUrl='video.aspx?id=<%# Eval("iVideoId") %>'
Text='Watch video' Visible='<%# Eval("iVideoId") IsNot DBNull.Value %>'></asp:Hyperlink>
What's happening is that navigate URL location is actually being set to "video.aspx?id=<%# Eval("iVideoId") %>" rather than "video.aspx?id=1234" when the page renders in the browser.
 
Old February 27th, 2008, 05:46 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take a look at my second reply. The entire expression, including the string literal video.aspx?id= should be part of the data binding expression enclosed in '<%# %>'

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old February 27th, 2008, 06:06 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Oops, I missed part of what you were saying in your second reply. I noticed you had changed my double quotes to single quotes, but I didn't pick up that you had made the string literal a part of the expression. It's working now!!

Just one problem I've noticed, the link is now invisible when iVideoId is Null, except the <li> bullet next to the link is still always visible which looks a little bit strange if the hyperlink is invisible. Is there a way to instruct <li id="video"> to be invisible when the link is invisible?

Here is my full code

Code:
<div id="Link">[list]
<li id="video">
<asp:Hyperlink runat="server" 
NavigateUrl='<%# "video.aspx?id=" & DataBinder.Eval(Container.DataItem, "iVideoId") %>' 
Text='Watch video' Visible='<%# Eval("iVideoId") IsNot DBNull.Value %>'></asp:Hyperlink>
</li>
</ul>
</div>
 
Old February 27th, 2008, 06:11 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, there is a way. Simply make the <li> runat='server", and move your code that determines visibility to the <li>:
Code:
<li id="video" runat="server" Visible='<%# Eval("iVideoId") IsNot DBNull.Value %>'>
BTW, shouldn't the[list] be located in a different template like the HeaderTemplate?

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




Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP:Repeater and Mouseover gbrown ASP.NET 1.0 and 1.1 Basics 1 June 4th, 2007 01:17 AM
hyperlink in nested datalist calling a repeater debjanib C# 1 May 25th, 2006 08:17 AM
Hyperlink in repeater control? johno ASP.NET 2.0 Professional 1 April 19th, 2006 05:22 AM
ASP Repeater without DataBinder safin ASP.NET 2.0 Professional 1 February 3rd, 2006 12:20 PM





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