 |
| 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
|
|
|
|

February 27th, 2008, 12:41 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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?
|
|

February 27th, 2008, 03:29 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

February 27th, 2008, 03:52 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 27th, 2008, 04:06 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

February 27th, 2008, 04:17 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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).
|
|

February 27th, 2008, 04:36 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|

February 27th, 2008, 05:40 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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.
|
|

February 27th, 2008, 06:06 AM
|
|
Authorized User
|
|
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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>
|
|

February 27th, 2008, 06:11 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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.
|
|
 |