On Thu, 14 Dec 2006 12:45:48 -0600, Martin.Coetsee wrote:
Fellow developers,
I need to find out how to display one of the fields listed in my
gridview as a hyperlink. The field displays the physical file path of
for example a pdf file. I want my users to be able to click on the
field(filepath) and the browser must open the file it points to.
I am using VS2005, setup in web developer mode. The database behind it
is MS-SQL 2005
thanks in advance!
MartinCoetsee
Hi Martin
The gridView has an "onRowCommand" that you can assign a delegate to. The
column could be a button whose "text" is the name of the pdf and whose
commandName is say "pdfFinder". Below is a loose example cobbled together
from another app (code is not tested!!). I build my columns dynamically in
the other app.
HTH
Mark
e.g.
<asp:GridView ID="pdfGridView"
runat="server"
Style="z-index: 100; width: 650"
onRowCommand="pdfRowCommand"
BorderStyle="None"
GridLines="None"
ShowHeader="false"
RowStyle-VerticalAlign="Top"
RowStyle-Height="17px" >
<Columns>
<asp:TemplateField HeaderText="PDF File">
<ItemTemplate>
<asp:Button
ID="pdfButton"
runat="server"
CommandName="pdfFinder"
CommandArgument='<%# Eval("pdfFile") %>'
Text='<%# Eval("pdfFile") %/>
</ItemTemplate>
</Columns>
</asp:GridView>
protected void pdfRowCommand(Object src, GridViewCommandEventArgs e)
{
string commandIs = e.CommandName;
if (commandIs == "pdfFinder")
{
// get the row index stored in the CommandArgument property
int index = Convert.ToInt32(e.CommandArgument);
// get the GridViewRow where the command is raised
GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index];
// get the pdfName
string pdfName = selectedRow.Cells[0].Text;
// Show the pdf
Session["PDFFileName"] = pdfName;
Server.Transfer("showPdf.aspx");
}