Static path to dynamic path issue in ASP.NET - 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" %>'/>

Related

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 pass querystring in anchor tag?

I want to pass query string in anchor tag but facing some error i.e "server tag is not well formed".
My code is:-
<a href="EditUser.aspx?uid='<%# Eval("Userid") %>'" id="aa" runat="server" />
I generally use:
<a href='<%# Eval("Userid","EditUser.aspx?uid={0}") %>'>Text</a>/pre>
If you remove runat="server" and single quotes before and after eval expression, your code will also start working
Happy coding
Try this
<a href='<%# "EditUser.aspx?uid=" + Eval("Userid") %>' id="aa" runat="server" />
You cannot use code breaks with string text together for an attribute value for an element which is ran server side. Instead you can use String.Format() to form the same response, using single quotes.
<a href='<%# String.Format("EditUser.aspx?uid={0}", Eval("Userid")) %>' id="aa" runat="server" />

image url Concatenate string

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'

asp buttonimage alternate text not visible

I've a problem with asp:image and asp:imagebutton alternate text: it's not visible when I move the mouse over the image. I declared the object in that way:
<asp:ImageButton ID="ButtonStatus"
ImageUrl='<%# IIF(Eval("Active")=1,"../images/folders/actived.png","../images/folders/deactivate.png")%>' CommandName="ChangeStatus" CommandArgument='<%# Eval("id_repository") %>' OnCommand="FolderCommand"
Enabled = '<% #iif(mySecurity.Admin=1, "True", "False") %>' Width="24px" AlternateText="Change the status"
runat="server" />
and for asp:image
<asp:HyperLink runat="server" ID="url_groups" NavigateUrl="~/action/Group_manager.aspx?action=2">
<asp:Image id="img_groups" runat="server" ImageUrl="~/Images/folders/group_config.png" AlternateText="Group manager"
EnableViewState="false" ImageAlign="Middle" Width="32px" CssClass="navigation" />
</asp:HyperLink>
Could you help me? I tried to search on web but I wasn't able to find any post or suggestion to fix that problem
thanks,
Andrea
It's browser dependent whether the alt text gets shown when you mouse over an image. If you set the ToolTip parameter on a .NET Image or ImageButton it will render a title attribute which shows across pretty much every browser.
Here's an example from a site I'm currently working on:
<asp:HyperLink ID="ReplyLink" runat="server" NavigateUrl='<%# Eval("SrcAddr", "~/msp/send.aspx?to={0}") %>'>
<asp:Image ID="ReplyIcon" runat="server" ImageUrl="~/images/email_go.png" AlternateText="Reply" ToolTip="Reply" />
</asp:HyperLink>
I think you're mixing up the alt attribute with the title attribute. Alt is shown when the user doesn't accept images and for screen readers whereas the title attribute is shown as tool tip when you hover the image.

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