Add a download Hyperlink in gridview in visual studios 2010 - c#

My problem is simple, I would like to make the last column of my gridview to be a download link to a file in local on my computer.
I have my datatable with 3 columns :
User_id, request_id, FilePath
FilePath is the path (string) to the file on my computer, what I've been trying is :
In my gridview on the webpage display the 2 first columns (User_id and request_id).
On the 3rd column I added a hyperlink field and tried to link it (with the property DataNavigateUrlField) to the content of the column FilePath.
I've ended up with dead links on which i can't even click, they just change color on mouseover.
Any one have a clue about that ? Thanks a lot

A possible solution could be to use a TemplateField for your Hyperlink column:
<asp:GridView AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href='<%# DataBinder.Eval(Container.DataItem, "FilePath") %>'>
<%# DataBinder.Eval(Container.DataItem, "FilePath")%>
</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
will create the desired output:
<tr>
<td>
<a href='c:/directory/file.xxx'>c:/directory/file.xxx</a>
</td>
</tr>
It seems that a HyperLinkField does not accept a file path for its DataNavigateUrlFieldsproperty.
I tried to outsmart it by setting the DataNavigateUrlFormatString property:
<asp:HyperLinkField DataNavigateUrlFormatString="file:///{0}" DataNavigateUrlFields="FilePath" DataTextField="FilePath" />
but the issue remains and will only produce
<tr>
<td>
<a>c:/directory/file.xxx</a>
</td>
</tr>

try to use this
WebClient.DownloadFile(Uri,String)
And read this how to get needed event hyperlink

Related

AJAX Toolkit and Listview

I am working on a web application. The process regarding the problem is that I am fetching users comments from DB using Entity Framework Model. I am using a listview to show Customers Name, Rating and Comments.
The problem is that I am using a Ajax toolkit control called HTMLEditor for storing comments in the DB, so when I have to display the comments, I need the same control ie HTML Editor. Now when I use the following code to create the ITEMTEMPLATE for the listview,
<ItemTemplate>
<tr style="background-color:#EDECB3;color: #000000;">
<td><%# Eval("CustomerName")%></td>
<td> <img src='Styles/Images/ReviewRating<%# Eval("Rating") %>.png' alt="">
<br />
</td>
<td> <cc1:Editor ID="Comments" runat="server" Text="<%#Eval("Comments") %>"/>
</td>
</tr>
</ItemTemplate>
Everything is working except the line
<td> <cc1:Editor ID="Comments" runat="server" Text="<%#Eval("Comments") %>"/>
</td>
It says that the server tag is not well formed. please help.
Updated Error: I added nounicode="true". and the error I encountered is
Could not find any resources appropriate for the specified culture or
the neutral culture. Make sure
"AjaxControlToolkit.Properties.Resources.NET4.resources" was correctly
embedded or linked into assembly "AjaxControlToolkit" at compile time,
or that all the satellite assemblies required are loadable and fully
signed.
you should try
<cc1:Editor ID="Comments" runat="server" Text="<%#Eval('Comments') %>"/>
the text proprty is not the same.
Try below code:
Text='<%#Eval("Comments") %>'
Using the ScriptManager on the page solve the problem.
As far as the problem of displaying HTML text in HTML Editor is concerned, I decided not to use the HTML Editor from Ajax Control Toolkit. Rather I displayed the html text raw on the page using the html decoding utility like this,
<%#Server.HtmlDecode(Eval("Comments").ToString()) %>
"rr_only4you" told that answer is correct, i got the error like use blow one
Text="<%#Eval("Comments") %>"
after that i change that to
Text='<%# Eval("Comments") %>'
this format it will correct,
you use this one
<cc1:Editor ID="Comments" runat="server"><%# Eval("Comments") %></cc1>

how to add a link button in the grid view by visual studio?

I want to add a link button in grid view that will navigate to the next form in the website. but i want to know how to insert that button into the grid view column? please help me..
You can add Link buttons in the Item template of a gridview.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Link" runat="server" PostBackUrl="MyNextPage.aspx">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
// TEXT FOR COLUMN NAME
</HeaderTemplate>
<ItemTemplate>
// ADD LINK BUTTON HERE.
</ItemTemplate>
</asp:TemplateField>
</Columns>
Add this inside grid view.
try this
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:ButtonField ButtonType="Link" HeaderText="LinkButton"/>
</Columns>
</asp:GridView>
Make Sure your GridViews datasource is bound. Set Auto-Generate Columns to false.
Open GridViewTask , add new column with Hyperlink Field proper name and path . You are done.

if statement in gridview template filed

I have a gridview. its data source is a datatable that is loaded from the database.
In this gridview, i have a template column. The content of this column is not directly pulled from the database but instead, i use the id of the current item to make a name and look for that name in a directory of images. The template field is:
<asp:TemplateField>
<itemtemplate>
<img src='../user/images/<% =itemType %><%# DataBinder.Eval(Container.DataItem, "id") %>.jpg?'
alt='<%# DataBinder.Eval(Container.DataItem, "Title") %>' />
</itemtemplate>
</asp:TemplateField>
Not all the items have images so I would like to check if this file exists. If it does, I'd like to use the above code to place it, if it doesn't i'd like to leave the field empty.
In the .cs file this is a matter of an if statement with the condition set to File.Exist(). But I could not find the syntax to do it in the .aspx file.
Is this possible and if so how?
Thanks.
you could code this behavior on the RowDataBound event. somethink like the below
Remember to make the image runat="server"
protected void GridViewProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType = DataControlRowType.DataRow){
if(!File.Exist(yourFileName){
//hide the image
var img=e.Row.FindControl("theImageId");
img.visible=false;
}
}
}
I think you should go for the above solution.
anyway Just add the below
<asp:TemplateField>
<itemtemplate>
<img src='../user/images/<% =itemType %><%# DataBinder.Eval(Container.DataItem, "id") %>.jpg?'
alt='<%# DataBinder.Eval(Container.DataItem, "Title") %>'
<%= File.Exists("yourFileName")? string.Empty : "style='display: none'" %> />
</itemtemplate>
</asp:TemplateField>
img tag has an event called onerror. So, if your requirement is to do something if the image is not found, do something like this.
onerror="this.src='Images/default.png'"

Show image from sql on non javascript link

Hi say of have a table of cars in my SQL database. That table has a column for the car make and a column called picture of type:
Picture(image, null)
I'm then displaying my cars in a repeater and so it might look like this:
<asp:Repeater id="carsRepeater" runat="server" DataSourceID="CarsDataSource>
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label id="carMakeLabel" runat="Server" Text='<%# Eval("Make") %>' />
</td>
<td>
Some how display a clickable image here
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</Footer>
</asp:Repeater>
What I'm wanting is in column two to get the picture of the car to display and make it so you can click on that picture and it will run a method in the code behind. Also I have this requirement where the picture click method mustn't require javascript to run.
Can someone please give me an idea of how I would go about this?
I'm currently thinking about putting the image somehow in a link but I'm not sure how to display the image. Maybe the asp:Image but that requires an ImageUrl.
Try image button, it displays an image, doesn't require javascript and you can run a method on click event from code behind
Write an HTTPHandler (say CarImage.ashx) and in that handler, given ID of the image return the binary data with ContentType of image/gif (or whatever). Render an image tag in the datagrid with src as CarImage.ashx?ImageID=xxxxx. The process is described many place including this one.

GridView trouble in .Net

i think this maybe an easy one...
I have a Gridview, bound to a dataSource with this query:
select userid, name, phone from users
I need to change the content of the Name cell to a link, like so:
User's Name
so OnRowDataBound i'm doing this:
e.Row.Cells[1].Text = "" + e.Row.Cells[1].Text + "";
It all works fine this way. My problem is that i don't want to display the UserId column in the Gridview, but when i attribute it (asp attribute or server-side) Visible="false" to the BoundField UserId, i can't capture it's value to build the Name Cell.
Thanx for your help
Make your gridView to use template items instead of simple grivdiew column.
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName")%>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You can find a nice intro Here
Agree with Jani, this should be done with a template field. If you really need to do it the other way, see Can data be kept in a dynamically bound GridView's invisible fields?.
you can set the visibility on the onrowdatabound event

Categories