hyperlink in repeater...what if url is dbNull ??

F

fredda054

Hi everybody !


I have a little repeater/hyperlink issue I'm not sure how to solve.
I use a repeater to list subjects available at a specific school.
The datasource of this repeater is a dataset filled with info from a
SQL Server database.
All the subjects are displayed as hyperlinks, and when a user clicks a
subject, it opens up a new browser with the website specific to that
subject (ie the specific department website).

Everything is working just fine except if the URL field in the database

table is 'NULL'. then the new browser will just display the "page
cannot be displayed" message, which is pretty obvious.

What I want to do is to make a check if the specific item in the
dataset is 'null' and if so I want the user to be redirected to my
global "message page" just passing a querystring saying, "sorry, no
available url for the moment", or something like that.

I think I should use the ItemDataBound event but I'm not sure how to do
it. I've tried a few options but I can't get it to work.
Any help would be highly appreciated !

Thanks a lot !

I include the repeater code below just in case...
In the code behind I just set the datasource of the repeater and bind
it. Nothing else is going on there...


------------------------------­----------------------------
<asp:repeater id="rptSubjects" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HyperLink NavigateUrl='<%# "http://" &
Container.DataItem("Dep_Websit­eURL") %>' text='<%#
Container.DataItem("SubjectNam­e") %>' Runat="server" Target="_blank"
ID="Hyperlink1" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:repeater>
------------------------------­-----------------------------
 
K

Karl Seguin

Check out:
http://openmymind.net/index.aspx?documentId=8

You are right about the ItemDataBound (as one of two solutions, the better
one!).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Hi everybody !


I have a little repeater/hyperlink issue I'm not sure how to solve.
I use a repeater to list subjects available at a specific school.
The datasource of this repeater is a dataset filled with info from a
SQL Server database.
All the subjects are displayed as hyperlinks, and when a user clicks a
subject, it opens up a new browser with the website specific to that
subject (ie the specific department website).

Everything is working just fine except if the URL field in the database

table is 'NULL'. then the new browser will just display the "page
cannot be displayed" message, which is pretty obvious.

What I want to do is to make a check if the specific item in the
dataset is 'null' and if so I want the user to be redirected to my
global "message page" just passing a querystring saying, "sorry, no
available url for the moment", or something like that.

I think I should use the ItemDataBound event but I'm not sure how to do
it. I've tried a few options but I can't get it to work.
Any help would be highly appreciated !

Thanks a lot !

I include the repeater code below just in case...
In the code behind I just set the datasource of the repeater and bind
it. Nothing else is going on there...


------------------------------­----------------------------
<asp:repeater id="rptSubjects" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<asp:HyperLink NavigateUrl='<%# "http://" &
Container.DataItem("Dep_Websit­eURL") %>' text='<%#
Container.DataItem("SubjectNam­e") %>' Runat="server" Target="_blank"
ID="Hyperlink1" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:repeater>
------------------------------­-----------------------------
 
G

Guest

You could use an iif statement (Assuming VB) e.g
iif(container.dataitem("Dep_WebsiteURL") is dbnull.value,
"/messagepage.aspx?message=.....", container.dataitem("Dep_WebsiteURL"))

or you could use the same technique to enable/disable the link or change the
link text... jd
 
F

fredda054

Hi ! Thanks for the help so far !
I'm getting a little bit closer to solve thism, but I'm not there yet.

Could someone please help me with the code in the ItemDataBound event.
What I want is something like this, but in proper vb code off course...

Private Sub rptSubjects_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
rptSubjects.ItemDataBound

if The databound Item, (Dep_WebsiteURL), is dbNull
Response.Redirect("MessagePage.aspx?msg=Sorry, no available url for
the moment")
End If

End Sub

I haven't figured out how to get the exact code together yet, but I'm
kind of in a hurry...So any help would very very appreciated !

Thanks,
Fredrik
 
G

Guest

Hi Fredik, If i follow your pseudo code correctly:
Private Sub rptSubjects_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
rptSubjects.ItemDataBound

if The databound Item, (Dep_WebsiteURL), is dbNull
Response.Redirect("MessagePage.aspx?msg=Sorry, no available url for
the moment")
End If

End Sub

your plan will redirect the user on the databinding event to the message
page if any of the urls are dbnull. This would mean that if _any_ of the
links in the list was dbnull then the user would be automatically redirected
and would not have the opportunity to see the links that were available.

Also from a usability point of view you are forcing the user to view a page
that tells them the link is not available.

IMHO you would be better changing the link text to something like "this link
is not currently available" and disabling the link. You could then display it
inline with the rest of the active links. If you were to do this with
declarative iif statements on the aspx/ascx template instead of ondatabinding
it would have the benefit that you could change the functionality without
recompiling the dll.

If you _really_ want to redirect the user as in the pseudo code you need
something like:

Private Sub rptSubjects_OnItemDataBound(ByVal o As Object, ByVal e As
RepeaterItemEventArgs) Handles rptSubjects.ItemDataBound

If e.Item.ItemIndex > -1 Then

If CType(e.Item.DataItem, DataRowView)("Dep_WebsiteURL") Is
DBNull.Value Then

Response.Redirect("/messagepage.aspx?message=blah blah blah")

End If

End If

End Sub

My suggestion would be to use the following hyperlink in your repeater
template

<asp:HyperLink NavigateUrl='<%# "http://" &
Container.DataItem("Dep_Websit­eURL") %>' text='<%#
iif(Container.DataItem("Dep_Websit­eURL") is dbnull.value
,Container.DataItem("SubjectNam­e") & ": link not available",
Container.DataItem("SubjectNam­e") ) enabled="<%# iff(
Container.DataItem("Dep_Websit­eURL") is dbnull.value, false, true)%>"
Runat="server" Target="_blank"
ID="Hyperlink1" />

which:
1. checks to see if the website url is null
2. if it is null, it replaces the link text to "subject name : link not
available" and disables the link (stops it from being clickable)

if the website url is not null it leaves the link text as "subject name" and
leaves the link enabled.

HTH jd
 
F

fredda054

Hi again !
Thanks a lot "London Calling" ! I think the solution you suggested is
better than what I initially intended, and I've decided to go with it.
Only one little thing left...I get a parse error, because the server
tag is not well formed. I can see why, the " ' " and the <#% tags are
not matched perfectly. I've tried to correct it but I can't seem to get
everything right. Any help would be highly appreciated.

by the way, you know of any good online tutorials concerning
programming like the above ??

thanks,
Fredrik
 
G

Guest

Hi Frederik,

sorry about that it was a bit sloppy of me... I was typing directly into the
newsgroup box, sometimes I rely on intellisense and highlighting more than I
should, but then thats what it's for...

any way revised code (watch for line wrapping):

<asp:HyperLink

NavigateUrl='<%# iif(Container.DataItem("Dep_WebsiteURL") is DBNull.Value,
"#" , "http://" & Container.DataItem("Dep_WebsiteURL")) %>'

text='<%# iif(Container.DataItem("Dep_WebsiteURL") is dbnull.value,
Container.DataItem("SubjectName") & ": link not available",
Container.DataItem("SubjectName"))%>'

enabled='<%# iff(Container.DataItem("Dep_WebsiteURL") is dbnull.value,
false, true)%>'

Runat="server"

Target="_blank"

ID="Hyperlink1"

/>

In case you havent realised, you wrap attributes in single quotes
(atttribute='value') instead of double quoutes (attribute="value") if the
attribute contains an evaluation that contains quotes, in this case
Container.DataItem("ItemName")

HTH jd
 
K

Karl Seguin

No offense, but the code bellow is less than ideal for maintaining. If you
just look at it, it isn't overly entuitive what's going on. I'd still favor
the onItemDataBound approach because (a) it doesn't mix your presentation
layer with your presentation logic layer , (b) you get intellisense support
and (c) as your logic needs to grow, exponentially harder will this code be
to maintain.

Karl
 
G

Guest

Hi Karl, no offense taken, in fact I agree completely. Its just another
_Quick_ (short term at least) solution that allows for a couple of changes
without having to recompile and may conceptually be easier to get started
with. jd
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top