Formatting one column of gridview based on value of another column

Posted on 2012-08-16
Last Modified: 2012-09-15

I am programming a web application in VB.NET.

I have a Gridview that I would like to display that lists document names, and their links. When the user clicks on the link - they will have access to the document.

The list of documents is in a database table called 'msgdocs'. The column with the document link is called 'linkURL' and the column with the type of link is called 'linktype'.

The problem is, I have 4 different types of documents (linktypes):

1. PDF documents - when the user clicks on this document, I would like it to open in a window... something like onclick="'linkURL',null, 'width=700,height=500'); return false"

2. Other local documents - just have the browser download the document (ex: like a powerpoint file)

3. External documents - have the browser open a new tab (ie: code would be <a href='linkURL' Target = '_blank')

4. Another page on the portal - have the browser refresh the current page (ex: using a querystring variable, code would be 'mypage.aspx?pagnum=4')

So what I am trying so understand is how it could be possible to "wrap" the 'linkURL' in code to provide the above 4 behaviours, based on the value of the 'linktype' column.

Perhaps using onrowdatabound ?

Help !

Thanks, Pete
Question by:pbissegger
    LVL 83

    Expert Comment

    You can use the RowDataBound event to add logic around this

    Author Comment


    I have rowdatabound that creates #2 and #3 above. But I am having problems with the PDf functionality.

    Here's a subset of my code:

        If e.Row.RowType = DataControlRowType.DataRow Then
            link1 = e.Row.Cells(5).Text
            Select Case e.Row.Cells(0).Text
                Case "0" ' There is no link, just display "-"
                    e.Row.Cells(5).Text = "-"
                Case "1" ' External link, open in new tab
                    e.Row.Cells(5).Text = "<a href='" & link1 & "' target='_blank'>" & right(link1,3) & "</a>" 
                Case "2" ' Internal document, just download
                    e.Row.Cells(5).Text = "<a href='docs/" & e.Row.Cells(5).Text & "'>" & right(link1,3) & "</a>"
                Case "3" ' PDF document, open popup window
                    e.Row.Cells(5).Text = "<a href='javascript:void(0)' onclick=''docs/" & link1 & "','')'>" & right(link1,3) & "</a>"
                Case "4" ' Internal document, just refresh page with new querystring
                    ' Call LoadDocument routine with an ID 
            End Select
        End If
      End Sub

    Open in new window

    but the PDF functionality isn't working. When I copy the link it just shows the "link" as void(0) ... I don't know what is happening to the rest of it.

    I have also tried:

    e.Row.Cells(5).Text = "<a href=''docs/" & link1 & "')'>" & right(link1,3) & "</a>"

    but that doesn't work either.

    I need help with the syntax to get it right.

    Thanks, Peter
    LVL 83

    Expert Comment

    >e.Row.Cells(5).Text = "<a href='javascript:void(0)' onclick=''docs/" & link1 & "','')'>" & right(link1,3) & "</a>"

    >When I copy the link it just shows the "link" as void(0)

    That's because you set it to void(0). Does clicking it work? Why don't you just add a plain link?

    Author Comment


    I was looking for a solution to open up a browser in a popup. The only way I know how to do this is on

    Is there another way to do this  using a hyperlink ?

    Thanks, peter
    LVL 83

    Accepted Solution

    You already have

    Case "1" ' External link, open in new tab
                    e.Row.Cells(5).Text = "<a href='" & link1 & "' target='_blank'>" & right(link1,3) & "</a>"

    Using target="_blank" opens in new window/tab.

    You did not say what happens when you click your current link.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now