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

Discussion in 'ASP .Net' started by fredda054@hotmail.com, Jul 4, 2005.

  1. Guest

    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>
    ------------------------------­-----------------------------
     
    , Jul 4, 2005
    #1
    1. Advertising

  2. Karl Seguin Guest

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


    <> wrote in message
    news:...
    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>
    ------------------------------­-----------------------------
     
    Karl Seguin, Jul 4, 2005
    #2
    1. Advertising

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

    "" wrote:

    > 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>
    > ------------------------------­-----------------------------
    >
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 4, 2005
    #3
  4. Guest

    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
     
    , Jul 5, 2005
    #4
  5. 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



    "" wrote:

    > 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
    >
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 5, 2005
    #5
  6. Guest

    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
     
    , Jul 6, 2005
    #6
  7. 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

    "" wrote:

    > 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
    >
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 6, 2005
    #7
  8. Karl Seguin Guest

    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

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


    "london calling" <> wrote in message
    news:...
    > 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
    >
    > "" wrote:
    >
    >> 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
    >>
    >>
     
    Karl Seguin, Jul 6, 2005
    #8
  9. 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

    "Karl Seguin" wrote:

    > 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
    >
    > --
    > 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!)
    >
    >
    > "london calling" <> wrote in message
    > news:...
    > > 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
    > >
    > > "" wrote:
    > >
    > >> 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
    > >>
    > >>

    >
    >
    >
     
    =?Utf-8?B?bG9uZG9uIGNhbGxpbmc=?=, Jul 6, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravikanth[MVP]
    Replies:
    6
    Views:
    3,904
    Aemca
    Jul 18, 2003
  2. Boris Zakharin

    Adding a DBNull value to the database

    Boris Zakharin, Aug 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    394
    Boris Zakharin
    Aug 27, 2003
  3. dave
    Replies:
    1
    Views:
    2,900
    Teemu Keiski
    Nov 26, 2003
  4. Elmo Watson
    Replies:
    3
    Views:
    6,058
    Scott M.
    Dec 25, 2003
  5. Replies:
    0
    Views:
    675
Loading...

Share This Page