image url Concatenate string - asp.net

i want to achievet the following Url
ImageUrl='~/products_pictures/(imageId)_middle.jpg
im using gridview and datalist
im trying the follwoing combination but it not working
<asp:Image ID="Image1" ImageUrl='~/products_pictures/<%#Eval("Id")%>_middle.jpg' runat="server" /></td>
<asp:Image ID="Image1" ImageUrl=<%"~/products_pictures/"%><%#Eval("Id")%><%"_middle.jpg"%> runat="server" />

I would use String.Format for this. It makes concatenation much easier:
<asp:Image ID="Image1" runat="server" ImageUrl='<%# String.Format("~/products_pictures/{0}_middle.jpg", Eval("ID"))%>'

this should work:
</td><asp:Image ID="Image1" ImageUrl="~/products_pictures/<%#Eval("Id")%>_middle.jpg" runat="server" /></td>
if not debug from this:
</td><%#Eval("Id")%></td>
ells you can try
<%# ((objectName)Container.DataItem).Id%>
((DataRowView)Container.DataItem)["Id"]

This might work
<asp:Image ID="Image1" runat="server" ImageUrl="~/products_pictures/<%#Eval("id") %>_middle.jpg"/>
If not you can put a asp:Literal, and a HiddenField to store id and in GridView.RowDataBound Event you can add image as a text to the literal

The server tag is not well formed because of the quotes
put single quotes on the outside and try again
ImageUrl='~/products_pictures/<%#Eval("Id")%>_middle.jpg'

Related

Repeater Control in ASP.NET

Please help me to display images from database to webpage. This is my source code, but it shows error creating repeater control
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand ="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Image ID="myImage" ImageUrl='<%# "~/ImageHandler.ashx?BImID="+Eval("img_id") %>' runat="server" alt=" " ;style="height:200px;width:200px;border:1px solid gray"/><asp:Literal ID="litSeparator" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
I think the problem is in this "ImageUrl='<%# "~/ImageHandler.ashx?BImID="+Eval("img_id") %>'".
Try to reorganize yor code like this:
ImageUrl='<%# GetImageUrl(Eval("img_id")) %>'
and then in code behind define the method GetImageUrl like this:
protected string GetImageUrl(object id)
{
return "~/ImageHandler.ashx?BImID=" + id;
}
Regards,
Uroš

Image not getting displayed in datalist

I am binding images to datalist. Taking the image name from database and giving the path.
My code is:
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px" ImageUrl='<%# Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg") %>' runat="server" />
</ItemTemplate>
</asp:DataList>
On .cs page:
ds = gc.GetDataToListBinder("select DISTINCT PageOrderID,PageName from ScreenMaster order by PageOrderID")
dlImages.DataSource = ds.Tables(0)
dlImages.DataBind()
I am facing 2 problems :
When imagename has space in between it adds %20 in between
Eg. if imagename is "API Message", it takes it as: "API%20Message"
I tried On this Problem:
Added ImageUrl='<%#Server.HtmlDecode(Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg"))'
But I got error:
XML literals and XML properties are not supported in embedded code within ASP.NET
If there is not space Eg.image name is "Charges" , Then also its not showing it in datalist.
When i ran project, and right clicked on it and view source, then its showing me correct path as:
src="D:\Sagar\Kinston\WebSite\ScreenMasterImages\Charges.jpg"
but not showing image.
Please help me with above code.
Where i have made mistake?
What else i should add in it?
Keep it easy and simple.
When trying things like that with URL, to know exactly what to write down, try typing it in your address bar so you'll be able to find the exact syntax needed in order to make it work correctly.
I've done this tons of time and work for me... So you could try something like :
<asp:DataList ID="dlImages" runat="server" RepeatColumns="4">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px"
ImageUrl='<%# String.Format("{0}{1}.jpg", "~\ScreenMasterImages\", Server.HtmlDecode(Eval("PageName"))) %>'
runat="server" />
</ItemTemplate>
</asp:DataList>

How to cast float to integer in ASP.NET?

I am trying to assign a specific width to my image in asp.net (using vb.net).
So I used the next code:
<asp:Image ID="PercentageImage" runat="server" Height="7px"
Width='<%# Eval("votsGraph") %>'
ImageUrl="~/images/bar.JPG" />
So how do i cast this one?
(The type of votsGraph is float.)
Use CInt():
<asp:Image ID="PercentageImage" runat="server" Height="7px" Width='<%# string.format("{0}px", cint(Eval("votsGraph"))) %>' ImageUrl="~/images/bar.JPG" />
I've also added String.Format() to your code as you were missing px:
Width='<%# string.format("{0}px", cint(Eval("votsGraph"))) %>'

Static path to dynamic path issue in ASP.NET

I am trying to get the path for my page and I succeed to get it using an HTML image tag:
<img src="images/slider/<%# Eval("BannerImage") %>.jpg" alt="" />
Is there a way to make it dynamic using the asp:Image tag?
I tried to make it like this:
<asp:Image ID="Image1" runat="server" ImageUrl="~/Classified/Images/Slider/'<%# Eval("BannerImage") %>'.jpg" />
And I got an error:
The server tag is not well formed.
Is there a solution for my problem?
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# "~/Classified/Images/Slider/" + Eval("BannerImage") + ".jpg" %>'/>

Form tag is not well formed (control Id)

Hi can anyone help me regarding my problem. I assigned a dynamic Id into the ID property of an image control. The problem is I got an error saying "form tag is not well formed". I replaced the double quote into a single or removed those quotes but I got the same error. How can I resolved this issue? By the way I used c# language.
<img ID="<%# DataBinder.Eval(Container.DataItem, "Id")%>" runat="server" />
Make this
<img ID="<%# DataBinder.Eval(Container.DataItem, "Id")%>" runat="server" />
to this:
<img ID='<%# DataBinder.Eval(Container.DataItem, "Id")%>' runat="server" />
Update: apparently it's indeed not possible to do this. I tried in a test application further with both the html image and Image server control. After thinking a bit further, and taking some coffee, it kind of makes sense that an ID cannot be set as such. How would you set properties on something without an ID in codebehind?
An alternative way what you can do however is this:
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server" ID="phTest"></asp:PlaceHolder>
</div>
</form>
and in codebehind:
HtmlImage image = new HtmlImage();
image.ID = "SomeRandomId";
image.Src = "urltosomeimage";
phTest.Controls.Add(image);
I got a similar problem and I solved it like this:
<img ID='<%# DataBinder.Eval(Container.DataItem, "Id")%>' runat="server" />
The reason for this is that the expression between <%# %> cannot be evaluated if it is not in single quotes.
How about this?
<asp:img ID='<%# DataBinder.Eval(Container.DataItem, "Id")%>' runat="server" />
or try to remove the runat="server" since your img tag is not a server control
If you're using .NET 4.0 set ClientIDMode=Static or change ID not in markup but in code-behind.
And use instead of if you need access it from code-behind.

Resources