Inline code block within asp:Image - asp.net

I'm trying to do a String.Replace inside my asp:image tag (nested in a Repeater) in order to change an apostrophe to an html friendly apostrophe
<asp:Image ID="Image1"
runat="server"
ImageUrl='<%# String.Format("~/images/products/{0}", XPath("image"))%>'
Visible='<%# CheckEmpty(XPath("image")) %>'
AlternateText='<%# XPath("#name")%>'
ToolTip='<%# XPath("#name").Replace("'", "&apos;")%>' />
The "ToolTip" is giving me difficulties. The error says "not well formed" but I know it's something to do with the format but.

You should probably use HtmlEncode since it's already included in the framework:
ToolTip='<%# HttpUtility.HtmlEncode(XPath("#name")) %>'

Protected Function ReplaceApostrophe(ByVal input As String) As String
Return input.Replace("'", "'")
End Function
<asp:Image ID="Image1"
runat="server"
ImageUrl='<%# String.Format("~/images/products/{0}", XPath("image"))%>'
Visible='<%# CheckEmpty(XPath("image")) %>'
AlternateText='<%# XPath("#name")%>'
ToolTip='<%# ReplaceApostrophe(XPath("#name"))%>' />

Related

<asp:TemplateField>, with an <asp:Image> item, how do I insert a directory into the ImageUrl='<%# Eval("StatusImage") %>'?

I have a GridView with an <asp:TemplateField ... > column. Inside that TemplateField I have an <asp:Image ... > whos image is dynamically set by a DataSource column. For example:
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("StatusImage").ToString() %>' />
</ItemTemplate>
</asp:TemplateField>
The ImageUrl='<%# Eval("StatusImage").ToString() %>' needs to have a directory ("Images/") attached to it somehow but how? I've tried everything I can think of... Any suggestions?
Thanks much for your help!
Steve
try this but I am not sure if you need to string be used there
ImageUrl='<%# Eval("StatusImage", "~/Images/{0}").ToString() %>'
You can do this in many format like
ImageUrl='<%# "~/Images/" & Eval("StatusImage").ToString() %>'
or
ImageUrl='<%# getImagePath(Eval("StatusImage").ToString()) %>'
in the code behind
function getImagePath(ByVal ImageName as string) as string
return "~/Images/" & ImageName
end function
You can best insert the "images/" in the database along with the file name . this will work fine.

eval and anchor tag href in a text label property ASP.Net

I have been trying to get these running looking at lots of different samples but with no luck.
To me it seems it's ok but what am I missing here?
<asp:Label ID="Label1" runat="server" Text="<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>" Width="100px" visible="true"></asp:Label>
Thanks
"With no luck" is not a descriptive error.
<%# is for databinding expression only. So have you called Page.DataBind() or at least Label1.DataBind() in codebehind?
You could also try
Text='<%= String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>'
Apart from that, why not doing such things in codebehind only, so you don't have issues like this?
<asp:Label ID="Label1" runat="server" Text='<%# String.Format("<a href=http://localhost/reportserver/Pages/ReportViewer.aspx?/temp&rs:Command=Render&id={0}>link</a>", Eval("ID")) %>' Width="100px" visible="true"></asp:Label>
you shouldn't use Text="something". you should use Text='something'

Combining Strings in ASP.NET Attributes

I am trying to concat a string inside the attribute. I get an error. I think that it is related to my Eval. Is there a proper way to concat strings, or is this just not possible. The problem I believe is where I set the NavigateUrl.
<asp:HyperLink ID="lb"
runat="server"
Text='<%#Eval("Key.Id") %>'
NavigateUrl='ViewItem.aspx?id=' + '<%# Eval("Key.Id") %>'/>
Short answer: NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'
Longer explanation:
The problem in your code is that you are use data binding expression only for part of your web control attribute. You need to move everything inside the data binding expression.
First of all, a data binding expression is this:
<%# EXPRESSION %>
Basically the rule for using a data binding expression for a web control attribute is that the expression must be the only thing in the attribute:
<asp:HyperLink ID="lb" runat="server"
Text='<%# EXPRESSION %>'
NavigateUrl='<%# EXPRESSION %>' />
So your first attribute, Text, is correct. But your second attribute, NavigateUrl is not correct. Because you put ViewItem.aspx?id= as the value for the attribute, leaving + '<%# Eval("Key.Id") %>' outside any attribute but inside the control tag.
Here is the correct syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# Eval("Key.Id", "ViewItem.aspx?id={0}") %>'/>
Notice that we are using a format string as the second parameter for Eval(). This is equivalent to the following, more explicit, syntax:
<asp:HyperLink ID="lb" runat="server"
Text='<%# Eval("Key.Id") %>'
NavigateUrl='<%# String.Format("ViewItem.aspx?id={0}", Eval("Key.Id")) %>'/>
Here's what I do when I have something in a gridview like this:
<img src='<%# GetDisImageLink(Eval("Disabilities").ToString()) %>'
alt="Disabilities" />
[CS code-behind]
public string GetDisImageLink(string dis)
{
return "../../Content/Images/CardContactInfo/" +
(dis.Trim() == "Y" ? "DIS.png" : "Blank.png");
}
Try this instead:
<asp:HyperLink ID="lb" runat="server" Text='<%#Eval("Key.Id") %>' NavigateUrl='ViewItem.aspx?id=<%# Eval("Key.Id") %>'/>
You don't need to concatenate

imagebutton not displaying image while image link without extension stored in database

i am using this code
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("photo", "~/gallery/thumbs\\{0}") %>' PostBackUrl='<%# Eval("ProductID", "gamedetails.aspx?ProductID={0}") %>
within a datalist.
it displaying other information but not the image
Change your markup to:
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl='<%# Eval("photo", "~/gallery/thumbs/{0}") %>' PostBackUrl='<%# Eval("ProductID", "gamedetails.aspx?ProductID={0}") %>
Notice I changed \\ for /
This is because you've initialized all attributes other then ImageSrc, probably )

How do you concatenate text when using Bind expression in asp.net

What is the syntax to concatenate text into a binding expression for an asp.net webpage (aspx).
For example if I had a hyperlink that was being bound like this:
<asp:HyperLink id="lnkID" NavigateUrl='<%# Bind("Link") %>' Target="_blank"
Text="View" runat="server"/>
How do you change, say, the Text to concatenate a bound value with a string? Variations like this aren't quite right.
Text='<%# Bind("ID") + " View" %>'
neither does
Text='<%# String.Concat(Bind("ID"), " View") %>'
Use Eval instead.
Text='<%# Eval("ID", "{0} View") %>'
Eval is also better if the value is not going to be updated, where Bind allows two way data binding.
You can also place the "concatenation" in the text portion of a tag if using a template field:
<asp:TemplateField HeaderText="Name" SortExpression="sortName">
<ItemTemplate>
<asp:LinkButton ID="lbName" runat="server" OnClick="lbName_Click" CommandArgument='<%# Eval("ID") %>'>
<%--Enter any text / eval bindind you want between the tags--%>
<%# Eval("Name") %> (<%# Eval("ID") %>)
</asp:LinkButton>
</ItemTemplate>
This results in output like:
Name (ID)
inside of the template column.
I have used String.Format("{0}{1}"... before to good effect.
You could use the following:
CommandArgument='<%#String.Format("{0}|{1}", Eval("ArgZero"), Eval("ArgOn"))%>'

Resources