image in gridview - asp.net

HI
Im trying to show an image in a gridview, its held on the filesystem and the filename is returned in the datasource query.
If the files are held in C:\TEMP, how would I properly structure the code below?
thanks
<asp:Image ID="Image" runat="server" ImageUrl='<%#"C:\TEMP\" + Eval("ImagePath") %>'

I believe it is along these lines
<asp:Image ID="Image" runat="server" ImageUrl="C:\TEMP\<%= Eval("ImagePath") %>"/>

In this scenario, your ImageUrl value needs to be relative to the root of your web application so if your webapp is in:
c:\iis\MyWebApp
and images are in:
c:\iis\MyWebApp\img the ImageUrl should evaluate to something like ~/img/Img1.jpg
You could store the full path e.g. `~/img/Image1.jpg' in the db or just the filename, in which case you need to add the path to the ImageUrl value either in code-behind or in the markup.
See this article for more information.
Hth.

Related

I can not set THM files to asp:image control

Why doesn't the image show up when the file extension is THM.
<ItemTemplate>
<asp:image id="ThumbImg" runat="server" imageurl='<%# GeThumb(Container.DataItem) %>' Width ="60px" Height = "60px" />
</ItemTemplate>
The above lines are within asp:gridview in ASPX page.
GetThumb returns url as below.
http://www.xyz.com/images/mythumbnailimage.THM
The image doesn't show on the grid and I don't get to see the typical RED Colored x mark but it is different. In any case the image doesn't show in the grid. The image file exists on the server but will render when I change extension to JPG or TIF.
So my question is what's wrong with THM files. What am I doing wrong here?
Thanks.
Found the image extensions (THM in my case) needs to be added in the server. Added and it started working.

How to pass image url from vb.net to asp.net?

I m fetching the image name path from vb.net and want to pass it to image URL in asp.net.. How to do... I m doing this but display nothing
IN Vb.net
dim myLogo as string = ResolveUrl("C:\Test\Logo\" & img_name)
Me.DataBind()
IN ASP.net
<asp:Image ID="test" ImageUrl='<% myLogo %>' runat="server" Height="100px" Width="100px" />
ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.
The following example shows the ~ operator used to specify a root-relative path for an image when using the Image server control In this example, the image file is read from the Images folder that is located directly under the root of the Web application, regardless of where in the Web site the page is located.
<asp:image runat="server" id="Image1"
ImageUrl="~/Images/SampleImage.jpg" />
You can use the ~ operator in any path-related property in server controls. The ~ operator is recognized only for server controls and in server code. You cannot use the ~ operator for client elements.
For more details refer:
ASP.NET Web Project Paths
Eg.
dim myLogo as string = "~\Logo\" & img_name
Surely the url to the file will be:
"file://c:\Test\Logo\" & img_name
Have you tried that?
Try this
<asp:Image ID="test" ImageUrl='<%= myLogo %>' runat="server" Height="100px" Width="100px" />
You need to declare myLogo variable as protected in general section of code and in aspx page you can use folling code to bind imageurl.
<asp:image runat="server" Height="100px" Width="100px" imageurl='<%#myLogo%>' />
Please let me know if this does not work.

ASP.NET server tags rendered in client HTML, not values?

Maybe I've forgotten how to use these, but I am going crazy trying to inject a server-side value into an HTML output. There are reasons why I am doing this inline, and not server-side, so please don't suggest that as a solution.
This code on the server side:
<asp:Label ID="Label1" runat="server" Text='<%= DateTime.Now.ToString() %>' />;
Renders as this in the client HTML sent to the browser:
<span id="Label1"> <%= DateTime.Now.ToString()></span>;
And it displays as big fat empty space, and nothing output to the interface.
If I change the ASP source to using the "#" character to define as data-binding syntax, then the rendered output to browser becomes:
<span id="Label1"></span>
EDIT:
Setting Label text was just a simplified object for the sake of asking the question. In real life, I am setting the CssClass attribute, which does not allow me to use the "wrapping" workaround some have suggested. I wanted to set a public property and have all the controls update from it dynamically on page load.
Ideally, since I already have all the controls laid out on the aspx page. Just looking to add an attribute. I wanted to have:
<asp:textbox ID='MyTxtBox1' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox2' CssClass='<% strVal1 %>' />
<asp:textbox ID='MyTxtBox3' CssClass='<% strOtherVal %>' />
<asp:textbox ID='MyTxtBox4' CssClass='<% strVal1 %>' />
Now what it looks like I need to do is repeat all my (250+) controls on the codebehind in a block of code that looks like:
MyTxtBox1.CssClass=strVal1
MyTxtBox2.CssClass=strVal1
MyTxtBox4.CssClass=strVal1
MyTxtBox3.CssClass=strOtherVal
I believe that may not work on a compiled Web Application as it's not interpreted at run-time like a C# "Web Site". However, I was able to get it to work wrapping the label around the value:
<asp:Label runat="server"><%= DateTime.Now.ToString() %></asp:Label>
Set the Label1.Text = value instead of trying to use server side attrs inside of the server control

How can i display the first element of a navigation property collection in my asp.net listview (Using Entity Framework)?

I am using a listview to display a list of all the users of my site. A user can upload multiple images, so when i generated my model with entity framework, 2 entities were created: Users and Images. User had a navigation property to a collection of images and Images had a navigation property to a single user.
I am now trying to display in my listview 1 of the images uploaded by the user (if he has uploaded any).It doesn't matter which one is being displayed.
I created an EntityDataSource, and i've added "Images" into it's include property. Then, i branched my listview to the datasource. usually, to display the content of the navigation property, i use something like
<asp:Label ID="fn" runat="server" Text='<%# Eval("Users.Firstname") %>' />
However, since it's column of a collection that i'm trying to display,can i use something like this? IF so, what would be a propper way to manage the case where the user didn't upload any image?
<asp:image ID="img" runat="server" ImageUrl='<%# Eval("Images[0].path") %>' AlternateText="" />
Eval is using reflection and it has its limitations on more complicated terms. You should not be afraid of direct casting:
<asp:image ID="img" runat="server"
ImageUrl='<%# ((YourObjectType)Container.DataItem).Images[0].path") %>'
AlternateText="" />
Note that in order to cast to YourObjectType you will probably have to add an <#Import > directive to the page (or alternatively use a full type name, including the namespace).

Image Binding in ASP.NET

I have the following piece of code
ImageUrl='<%# Bind("ImageUrl") %>'
I've got a GridView I want to show images on a ItemTemplate
I have a Database where I'm saving on the name of the images e.g --> img1.jpg
I want to append the full path of the image at before its name so It can be viewed on the ItemTemplate on the GridView.
I tried to use concat, or the + operator but it doesn't seem to work
can any1 tell me what 2 do ???
Use inline Eval like this ImageUrl='<%# "path/to/image" + Bind("ImageUrl") %>'
No need to use Bind. Its used for two-way, read/write databinding.
Hi try this it ll works
' height="120" width="180" alt="" />
' OnClick="BtnImg_Click" height="120" width="180"/>

Resources