I have a Label which gets from my database the data "1_1" this is the file name of an image I would like to show relative to the ID.
my image link is
<img src="Images/Dropox/.jpg"/>
with no file name
I would like to know how to add a label that on load will be populated with the string "1_1" and then for that string to go into my SRC link.
I have tried adding the label into the space required but that doesn't work.
my idea would be something along the lines of
<img src="Images/Dropox/<asp:Label ID="ImageSequencea" runat="server">.jpg"/></asp:Label>
or something like that. can anyone help?
thanks!
Label corresponds to the <span>. Which means that when ASP.NET engine finds a Label on the page, it takes its Text, wraps into <span>, and sends to the output. Most likely you are not looking for:
<img src="Images/Dropox/<span>1_1</span>.jpg"/>
If you just need url to be Images/Dropox/1_1.jpg with 1_1 being inserted at runtime, you can:
Just turn your img tag into server tag and set its value in code behind:
<img id="DropboxImage" runat="server" />
Use scriplet and call function from code behind
<img src="Images/Dropox/<%= GetImageName() %>.jpg"/>
User asp:Image and set its ImageUrl property in code behind:
<asp:Image runat="server" ID="DropboxImage" />
Try this,
<img src="Images/Dropox/<%#Eval("ReturnEntityName")%>.jpg"/></asp:Label>
Related
Following code fetches image URL (imgURL) from sql database in a View:
<img src=#Server.MapPath(item.imageUrl); />
Following Html that it renders for the above line of code in the browser is wrong:
<img src="D:\Visual" studio="" projects\onlinestore\onlinestore\images\products\dry\baking="" goods\dalda.jpg;="">
Correct Html should be:
<img src="D:\Visual studio projects\onlinestore\onlinestore\images\products\dry\baking goods\dalda.jpg;="">
Can anyone please guide?
You have 2 issues.
First the value of the attribute must be quoted (the html you are seeing is because the value contains spaces - at each space, the attribute ends and a new (invalid) attribute is created)
Second, you do not need #Server.MapPath() (and that should not be used in a view anyway). The values of imageUrl property should be in the format
"/images/products/dry/baking goods/dalda.jpg"
and then in the view its
<img src="#item.imageUrl" />
Try
var pathOnDisk = HttpContext.Current.Server.MapPath(uri.AbsolutePath);
var decoded = HttpContext.Current.Server.UrlDecode(pathOnDisk);
I'm using ASP.NET Core 2 with Razor to try and pass an image URL into the View via a ViewModel. Unfortunately, the image URL has spaces in it, which I don't have control over at the moment.
I have a file name like this (note the space):
https://example.com/images/file name.jpg
I'm using Razor to try and show the image like this:
<img src=#item.ImageUrl />
The output HTML looks like this:
<img src="https://example.com/images/file" name.jpg />
Obviously this will not do! I should mention, we know this all works when we use an image that has no spaces in the file name. Here are some ways I've tried to correct the issue:
Using Url.Content helper in Razor (this did not make a difference):
<img src=#Url.Content(item.ImageUrl) />
Using System.Web.HttpUtility.UrlEncode when populating the ViewModel (this results in a "double-encoding" error):
myViewModel.ImageUrl = HttpUtility.UrlEncode(someUrl);
My goal is to get the URL encoded properly (no "double-encoding" and no truncating at the space in the input string). How can I achieve this goal?
The problem is that Razor will attempt to create valid HTML, so when it sees a space it automatically closes the quotation marks, so:
#{string img = "a a.jpg";}
<img src=#img />
Gets rendered as
<img src="a "a.jpg /> // of course invalid
So, you need to be sure you include your quotation marks, then Razor will use them:
<img src="#img" />
Which gets rendered as
<img src="a a.jpg" />
You can try
<img src="#Html.Raw(item.ImageUrl)" />
but I agree that using invalid URLs (with spaces) doesn't seem very useful in the long run.
I'm trying to update alt attribute text to <img> tag in c# behind, have no idea to do so.
Here is my aspx code:
<img id="DetailsImage" runat="server" alt="" class="DetailsImage" />
In aspx.cs
DetailsImage.Src = dsArticleDetails.Tables[0].Rows[0]["ImageURL"].ToString();
Any help appreciated.
For this purpose you should use Alt property.
DetailsImage.Alt = "My alternate text"
You can use AttributeCollection. Also using this you can change, add all attributes of all html element
DetailsImage.Attributes.Add("alt","what you want");
Have you tried DetailsImage.Alt?
I have Hyperlink in a datalist. HTML is given below:
<asp:HyperLink ID="lnkEntry" runat="server"><%# DataBinder.Eval(Container.DataItem, "Title") %></asp:HyperLink>
i have saved the URL of a Entry in the Database, like "http://test.com/posts/New-test-in-March" And used the following way to Bind it to Hyperlink in Item_databound of Datalist.
lnkEntry.NavigateUrl = objEntry.PermaLink;
But when it renders into the browser the URL get changed to "http%3a//test.com/posts/New-test-in-March"
i have tried to use the URLDecode but it doesn't make any change in output.
lnkEntry.NavigateUrl = Server.UrlDecode(objEntry.PermaLink);
Please help me how can i fix this issue.
you should use HttpUtility, like this:
lnkEntry.NavigateUrl = HttpUtility.UrlEncode(objEntry.PermaLink);
or WebUtility if running recent versions of .net.
Check this thread for more info.
I am trying to create an ASP.net server control for displaying formatted code, using this library: http://www.manoli.net/csharpformat/
Here's the trick: I want be able to express code blocks like this...
<custom:CodeFormatter runat="server">
<asp:Label runat="server" ID="something" Text="my text" />
<asp:Image runat="server" ID="something" ImageUrl="header.jpg" />
</custom:CodeFormatter>
...but not have the inner tags actually execute. I want the Label & Image to be seen by CodeFormatter as raw text, not parsed as real server tags.
So my question is, how to prevent the Label from actually rendering as such? And how to access everything within the custom:CodeFormatter as raw, literal text?
My workaround right now is to encode the <>'s as < and >. I'd love not to do that.
Thanks!
How about extending your control to use ITemplate. Here is a article that explains how to extend a control.
http://www.devx.com/codemag/Article/32182