Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to create a link in grid view and attach a pdf file from a folder 1

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
Hi Experts,

I am displaying my result using a gridview control and there is a request for me to have a link in the gridview to attach the pdf file I have in my folder.Can I do that? I am thinking to combine both the result from the database and folder in the dataset? I am not sure how I can handle this. can someone shed a light on this.
 
Assuming your gridview's datasource returns fields Title and hyperlink, you could use something like the below.
Code:
        <asp:TemplateField>
	<ItemTemplate>
		<asp:HyperLink ID="lnkUrl" runat="server" Target="_blank" Text='<%# Eval("Title") %>' NavigateUrl='<%# Eval("hyperlink")%>'></asp:HyperLink>
	</ItemTemplate>
</asp:TemplateField>

~LFCfan

 
I have contid as a return field title and hyperlink. for example the value for the contid field from the database is 090054 and the pdf file which is an addenda for the project is 090054.pdf. how can I create a link when the customer click 090054 get a pdf file in this case 090054.pdf file ? thank you for the help
 
I did create the hyperlink column my next question will be how can my link column open the pdf file. for example my link column is contid = 090053, 090045 and the pdf file that pertain to this will be 090053.pdf. 090045.pdf ... thank you again

<columns>
<asp:BoundField DataField="stateproject" HeaderText="State Project Num">
<ItemStyle Font-Size="11px" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="minnesotaprojectnumber" HeaderText="MN Project Num">
<ItemStyle Font-Size="11px" />
</asp:BoundField>
<asp:BoundField DataField="contid" HeaderText="Contract Id">
<ItemStyle Font-Size="11px" />
</asp:BoundField>
<asp:BoundField DataField="addendanum" HeaderText="Addend. No">
<ItemStyle Font-Size="11px" />
</asp:BoundField>
<asp:BoundField DataField="dateadded" HeaderText="Issue Date">
<ItemStyle Font-Size="11px" />
</asp:BoundField>
<asp:BoundField DataField="Descr" HeaderText="Brief Description Of Addenda">
<ItemStyle Font-Size="11px" />
</asp:BoundField>
<asp:BoundField DataField="Lett_date" HeaderText="Letting Date">
<ItemStyle Font-Size="11px" />
</asp:BoundField>

<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkUrl" runat="server" Target="_blank" Text='<%# Eval("contid") %>' NavigateUrl='<%# Eval("contid")%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
 
I have gone this far and my only problem is

how can I eliminate the space between the contid and pdf ?

thank you for the help

asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkUrl" runat="server" Target="_blank" Text='<%# Eval("contid") %>' NavigateUrl='<%# " %>'> </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
 
REmove the script from your HTML page(that is classic ASP progamming). Use the codebeind.

something like:

linkURL.NaviageURL = " + e.item("contid").tostring.trim + ".pdf
 
jbenson001 thank you for the reply. I always lear a new thing from you and thank you for sharing that ....where do I need to put this code. Is that with in rowdatabond or rowcreated event please clarify that for me.
 
Now I am getting lnkUrl is not declared error...any clue


<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="lnkUrl" runat="server" Target="_blank" Text='<%# Eval("contid")+ Eval("addendanum") %>' > </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>


Protected Sub gvaddenda_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvaddenda.RowCreated
lnkUrl.NaviageURL = " + e.Row.Cells("contid").ToString.Trim + ".pdf"
End Sub
 
Use the RowDataBound event of the grid.

Before you can set the URL property you have to find the link

Dim link as Hyperlink = e.findcontrol("lnkURL")
link.NavigateURL = .....
 
I am almost there but now this is the error I am getting.
once again thank you for your help jbenson001


Server Error in '/' Application.
--------------------------------------------------------------------------------

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 96: ' Dim test As String = e.Row.Cells.Item("contid").ToString.Trim
Line 97:
Line 98: link.NavigateUrl = " + e.Row.Cells("contid").ToString + ".pdf"
Line 99: End Sub
Line 100:End Class
 
YOu will have to trace through the code and find the line that is causing the problem. Pick that line apart until you find the issue.
 
thank you jbenson001 again for your response. the line that cause the problem is
link.NavigateUrl = " + e.Row.Cells("contid").ToString + ".pdf"

the error msg is this : Conversion from string "contid" to type 'Integer' is not valid.
 
This is where you have to use your problem solving skills.
To find this error, simply type e.row.cells. Intellesence will tell you the rest.

However, I made a mistake, you will first have to do this in the begining of the event:
Dim dataItem As DataRowView = CType(e.Row.DataItem, .DataRowView)

Then
link.NavigateUrl = " + dataitem("contid").ToString + ".pdf"
 
thank you jbenson001 for bieng patient with me and I really appreciate your help this far. However, I am still getting an error message Object reference not set to an instance of an object. thank you again for your help I wish I could give you more than one star....
 
I can't help you unless you give the code and the exact line that is causing the error.
 
it is right here


link.NavigateUrl = " + dataitem("contid").ToString + ".pdf"


I checked the value for dataItem and it is nothing.

I will try to find out why I am getting the error message and I really appreciate your your patient and willingness to share your deep knowledge with me.
 
what event are you putting the code in. It should be the RowDataBoundEvent.
 
yes that is the event I am using.

I think I need to check the row type like this. Let me add the below line and let you know that outcome.


If e.Row.RowType = DataControlRowType.DataRow Then

end if
 
yeap this line did it for me. I think it was first going to header and that is why I was getting the above error message if I am not mistaken.

If e.Row.RowType = DataControlRowType.DataRow Then

end if

again thank you jbenson001 for every thing you did to help me out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top