How to pass image url from vb.net to asp.net? - 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.

Related

Data URI is double-encoded by AntiXssEncoder

We have an ASP.Net Webforms (.Net 4.7.2) site. We've enabled the built-in XSS protection by adding to web.config:
<httpRuntime encoderType="System.Web.Security.AntiXss.AntiXssEncoder" ... />
This works fine, apart from in one place: we have some code that generates a small image, and embeds it within the page using a Data URI:
(aspx)
<asp:Image ID="image1" runat="server">
(C#)
image1.ImageUrl = dataURI;
and dataURI is normally something like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMMAAADDAQMAAAA ...
This works fine without the AntiXssEncoder, but with that in place the rendered HTML turns into:
<img id="image1" src="data%3Aimage/png%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAAA ...
... so the unsafe characters in the "header" of the src has been encoded, and the image doesn't display on the browser.
How can I disable the AntiXssEncoder for this one image object, or otherwise force the Data URI to get to the browser without being re-encoded? There is no user input on this particular page.
One way is to "do it yourself". Reference: https://stackoverflow.com/a/7406983/11534
Bascially
Declare a public property in your code-behind file with the image data. Let's say "ImageData" (public string ImageData {get;set;}) and set it to hold the base64 data.
Replace <asp:Image ID="image1" runat="server"> with <img src="<% =ImageData %>" />

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 to give path for image in a master page for two sub-pages in two locations?

Hi all my work environment is asp.net c# vs2008. My issue is this, i have a master page outside.master in shared folder.Inside it i have an image control with
<img src="App_Themes/Home/images/logo.png" />
i am referring this master page from in two sub pages. One is Index.aspx which is located in the root level and Secondly registration.aspx which is under masters folder. The problem is that when i run, the index.aspx will show the logo where registration.aspx is not showing the logo. Please tell me how to specify the path so that i will get logo in both pages.
Tilde sign ~ will resolve for server side controls.
So you need to add runat="server" as img in HTML element.
Try this:
<img src="~/App_Themes/Home/images/logo.png" runat="server"/>
The most foolproof method is to have something like this
<asp:Image runat="server" id="myImage" ImageUrl='<%# Eval("imageFile") %>' />
Then in the code-behind assign the variable imageFile to something like Server.MapPath("App_Themes/Home/images/logo.png");
Try the following:
<img src="~/App_Themes/Home/images/logo.png" runat="server" />

relative url in asp.net website applications

I have an image that I want to use it's src attribute in relative format
when my website URL was http://localhost/ I used to use this code to access this image file:
<img alt="something" src="/Files/pic.png">
But now I have to add an application to my site and change my site URL to http://localhost/mysite.
Now none of my images load in this site because the path is still http://localhost/Files/pic.png not http://localhost/mysite/Files/pic.png
how can I change my root URL (/) to http://localhost/mysite/?
Thanks
Use tilde ~ in a server control to use a relative path.
<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" />
You can use ~ symbol to represent root in ASP.Net
<asp:Image ID="Image1" runat="server" ImageUrl="~/Files/pic.png"/>
#rrrr is right, that the way to do it,
<asp:Image runat="server" ID="myImage" ImageUrl="~/Files/pic.png" />
but I would use a standard html image with runat="server"
<img runat="server" src="~/YourPath/image.png">
Reason : less server side controls

image in gridview

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.

Resources