Click to See Complete Forum and Search --> : GridView Hyperlink Show/Hide
unseth
10-11-2007, 03:26 PM
Visual Studio 2005 / ASP.NET2.0 / VB
I have a gridView that returns data from a datasource.
My first column returns a list of Part Numbers - I need to make this a hyperlink. No problem here..........
My problem is that I need the cell(s) to do one of Three things .
Take the Part number and make it link(NavigateURL or DataNavigateUrlFormatString) to Documents/{0}.doc if the file exsist and it is a doc file.
Documents/{0}.pdf if the file exsist and it is a PDF file
or Do not have a hyperlink if the file is not located in the Documents directory.
My gridview is named "GridView1" and my Bound field is "Part Number"
An Example of the page is at:
http://systelusaco.web149.discountasp.net/PartsData/Parts_Manual_Search.aspx
Model Number 4472NAV-02
Serial Number 504947-001
Hopefully someone can help me out with this one - I have been on Google for the last two days with no luck
lmf232s
10-12-2007, 10:36 AM
Well you could create your grid with a hyperlink field for the document link.
In the NavigateUrl you would call a function and pass in your file path and/or item number.
Once in the function you can then use the System.IO to see if the file exists where ever it lives. If the file exits then you return a valid path back to the file. If the document does not exist you return an empty string.
If you return an empty string then the hyper link will not render as a link but as text.
You could do this other ways as well but basically you need to call a function and verify that the document is a valid file.
Here is an example. While this is not the exact code you should be able to go from there.
'aspx (HTML)
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkDocument" runat="server" Text='<%# Eval("Item") %>' NavigateUrl='<%# CreateLink(_Utilities.myCstr(Eval("FilePath")), Eval("Item")) %>' />
</ItemTemplate>
</asp:TemplateField>
'VB Function
Public Function CreateLink(ByVal FilePath As String, ByVal Item As String) As String
Dim IsValid = DoesFileExist(FilePath)
If IsValid Then
Return FilePath
Else
Return ""
End If
End Function
Post back if you have any problems or questions.
unseth
10-12-2007, 11:35 AM
First - thank you for your reply.
Now my code is not working - but I will try to go through the steps and explain what I did:
With in my page I made this change:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkDocument" runat="server" Text='<%# Eval("Part Number") %>' NavigateUrl='<%# CreateLink((Eval("FilePath")), Eval("Part Number")) %>' />
</ItemTemplate>
</asp:TemplateField>
---Text= Eval("Part Number") - the text for the cells should be the part numbers that are being returned (I think this is correct)
--- CreateLink - Sine I placed the Function in my code-behind page - how do I reflect that?
My Code behind page:
Not sure I am going down the right path here but this is what I have
Public Function CreateLink(ByVal FilePath As String, ByVal Item As String) As String
'Dim IsValid = DoesFileExist(FilePath)
Dim Path As String = "Documents/"
If System.IO.File.Exists(Path + "{0}" + ".doc") Then
Return FilePath + "{0}" + ".doc"
Else
If System.IO.File.Exists(Path + "{0}" + ".pdf") Then
Return FilePath + "{0}" + ".pdf"
Else
If System.IO.File.Exists(Path + "{0}") Then
Return ""
End If
End If
End If
End Function
Hope this makes since
lmf232s
10-12-2007, 01:54 PM
It looks good except for where your concatonating {0}. Instead of using {0} use the item.
Also are you using C#? If not use & to concatonate instead of +
If System.IO.File.Exists(Path & Item & ".doc") Then ........
unseth
10-12-2007, 02:34 PM
If I use FilePath in:
<%# CreateLink((Eval("FilePath")), Eval("Part Number")) %>
I get DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'FilePath'.
If I change "FilePath" to "Part Number" then I get the table to return but no Hyperlinks under the Part Numbers
lmf232s
10-15-2007, 09:26 AM
Then I assume you dont have a field called FilePath.
This example does not have to be followed 100%, it was just ment as a guide.
So if you dont have a field named Filepath then you can just put a string that is the path to your file location or you can omit that from the function and just put it in the function.
Dim Path As String = "Documents/"
<%# CreateLink("/Documents/" & Eval("Part Number")) %>
Or
<%# CreateLink(Eval("Part Number")) %>
Again this is only ment to be used as a guidline as I dont know exactly what fields your working worth. From the 2 examples above you can see that you can just as easly pass in 1 parameter to the function and create the full path and pass in the full file path to the function or you can just pass in the part number and then create the link in your function. Kind of the way your example was posted.
unseth
10-15-2007, 04:40 PM
I would like to thank you for all your help on this. I had someone else give me some pointers on grabing the information..... it is working so here is what I did
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource2"
Visible="False">
<Columns>
<asp:TemplateField HeaderText="Part Number">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Target="_blank" NavigateUrl="" Text='<%# Eval("[Part Number]") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="continued" HeaderText="Description cont." SortExpression="continued" >
<ItemStyle Wrap="False" />
</asp:BoundField>
</Columns>
</asp:GridView>
CODE BEHIND
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim hl As HyperLink = CType(e.Row.FindControl("Hyperlink1"), HyperLink)
Dim Doc As String = e.Row.DataItem("Part Number") & ".doc"
Dim PDF As String = e.Row.DataItem("Part Number") & ".pdf"
Dim un As String = ""
If File.Exists(Server.MapPath("Documents/" & Doc)) Then
hl.NavigateUrl = "Documents/" & e.Row.DataItem("Part Number") & ".doc"
Else
If File.Exists(Server.MapPath("Documents/" & PDF)) Then
'hl.NavigateUrl = Server.MapPath(PDF)
hl.NavigateUrl = "Documents/" & e.Row.DataItem("Part Number") & ".pdf"
Else
If System.IO.File.Exists(un) Then
hl.Visible = False
End If
End If
End If
End If
End Sub
THANKS AGAIN!!!!