How can I append text to a databound field? - asp.net

If I have this:
<img ID="imgField" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"Name") %>' />
How can I add "images/" or any other string add on to the url?
I tried
ImageUrl=' "images/" + <%# DataBinder.Eval(Container.DataItem,"Name") %>'
And
ImageUrl= "images/" + '<%# DataBinder.Eval(Container.DataItem,"Name") %>'
And
ImageUrl='images/<%# DataBinder.Eval(Container.DataItem,"Name") %>'
But none of them worked. Anyone know?

<img ID="imgField" runat="server" ImageUrl='<%# string.Format("images/{0}", DataBinder.Eval(Container.DataItem,"Name")) %>' />
Or, if you want to shorten it up a bit...
<img ID="imgField" runat="server" ImageUrl='<%# string.Format("images/{0}", Eval("Name")) %>' />
and I think this should work too - but don't have a project in front of me to test it on...
<img ID="imgField" runat="server" ImageUrl='<%# Eval("Name", "images/{0}") %>' />

<img ID="imgField" runat="server"
ImageUrl='<%# "images/" + DataBinder.Eval(Container.DataItem,"Name") %>' />
Or inside a gridview, you could use an ImageField column:
<asp:GridView>
<Columns>
<asp:ImageField DataImageUrlField="Name"
DataImageUrlFormatString="images/{0}" />
</Columns>
</asp:GridView>

You were using
<%
instead of the correct:
<%#

Related

ASP.Net image set ImageURL to external site

I am struggling to set ImageURL in a asp:image control. I am setting it to a URL from another site, at the moment I have several versions of the image trying to get one to work bellow is my code and the output.
Function used in images bellow
Function BuildLogoPath() As String
Return ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg"
End Function
ASP.net Code in page
<asp:Image ID="imgFav" runat="server" ImageUrl='<%: ConfigurationManager.AppSettings("WebPath") & "img/favicon.ico" %>' /><br />
<asp:Image ID="Image2" runat="server" ImageUrl='<%= ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>' />
<asp:Image ID="Image3" runat="server" ImageUrl='<% ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>' />
<asp:Image ID="Image4" runat="server" ImageUrl='<% Response.write(ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg") %>' />
<asp:Image ID="imgLogo" runat="server" ImageUrl='<%# BuildLogoPath() %>' /><br />
<asp:Image ID="Image5" runat="server" ImageUrl='<%= BuildLogoPath() %>' />
<asp:Image ID="Image6" runat="server" ImageUrl='<% BuildLogoPath() %>' />
<asp:Image ID="Image7" runat="server" ImageUrl='<% response.write(BuildLogoPath()) %>' />
<%= ConfigurationManager.AppSettings("WebPath") & "img/favicon.ico" %><br />
<%= ConfigurationManager.AppSettings("WebPath") & "/img/main/logo.jpg" %>
Output:
<img id="ContentPlaceHolder1_imgFav" src="<%:%20ConfigurationManager.AppSettings("WebPath")%20&%20"img/favicon.ico"%20%>"><br>
<img id="ContentPlaceHolder1_Image2" src="<%=%20ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg"%20%>">
<img id="ContentPlaceHolder1_Image3" src="<%%20ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg"%20%>">
<img id="ContentPlaceHolder1_Image4" src="<%%20Response.write(ConfigurationManager.AppSettings("WebPath")%20&%20"/img/main/logo.jpg")%20%>">
<img id="ContentPlaceHolder1_imgLogo" src=""><br>
<img id="ContentPlaceHolder1_Image5" src="<%=%20BuildLogoPath()%20%>">
<img id="ContentPlaceHolder1_Image6" src="<%%20BuildLogoPath()%20%>">
<img id="ContentPlaceHolder1_Image7" src="<%%20response.write(BuildLogoPath())%20%>">
http://office.logma.biz/onefit.com.jamie/img/favicon.ico<br>
http://office.logma.biz/onefit.com.jamie//img/main/logo.jpg
As you can see the code is working fine when not not in the image control.
The simple answer is that you can't use <% and %> inside <asp: .. /> tags. You have two options:
Set it programmatically from code-behind:
imgFav.ImageUrl = Me.BuildLogoPath()
Render a normal <img> tag in the HTML instead:
<img src='<%= BuildLogoPath() %>' />

Why ImageButton Display Text SubmitQuery Runtime

i use imagebutton in datalist and set codeexpression :Eval("PictureUrl", "ProductImages\thumb_{0}") and runtime display submitquery.
<asp:DataList ID="DataList1" runat="server" DataKeyField="pid"
DataSourceID="SqlDataSource1">
<ItemTemplate>
pname:
<asp:Label ID="pnameLabel" runat="server" Text='<%# Eval("pname") %>' />
<br />
pprice:
<asp:Label ID="ppriceLabel" runat="server" Text='<%# Eval("pprice") %>' />
<br />
publisher:
<asp:Label ID="publisherLabel" runat="server" Text='<%# Eval("publisher") %>' />
<br />
writer:
<asp:Label ID="writerLabel" runat="server" Text='<%# Eval("writer") %>' />
<br />
<asp:ImageButton ID="ImageButton7" runat="server"
ImageUrl='<%# Eval("PictureUrl", "ProductImages\thumb_{0}") %>' />
<br />
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbpath %>"
SelectCommand="SELECT [pname], [pprice], [PictureUrl], [gid], [storeid], [publisher], [writer], [pid] FROM [tblproduct]">
</asp:SqlDataSource>
output one record:
pname:
<span id="ContentPlaceHolder1_DataList1_pnameLabel_0">تکنیک عکاسی</span>
<br />
pprice:
<span id="ContentPlaceHolder1_DataList1_ppriceLabel_0">125000 </span>
<br />
publisher:
<span id="ContentPlaceHolder1_DataList1_publisherLabel_0">اسرار دانش</span>
<br />
writer:
<span id="ContentPlaceHolder1_DataList1_writerLabel_0">نصرالله کسرائیان</span>
<br />
<input type="image" name="ctl00$ContentPlaceHolder1$DataList1$ctl00$ImageButton7" id="ContentPlaceHolder1_DataList1_ImageButton7_0" src="ProductImages%09humb_~/image/tecnikakasi.jpg" />
You got the correct HTML rendered
<asp:ImageButton ID="ImageButton7" runat="server"
ImageUrl='<%# Eval("PictureUrl", "ProductImages\thumb_{0}") %>' />
will produce input tag with type=image
<input type="image" name="ctl00$ContentPlaceHolder1$DataList1$ctl00$ImageButton7" id="ContentPlaceHolder1_DataList1_ImageButton7_0" src="ProductImages%09humb_~/image/tecnikakasi.jpg" />
The problem is ProductImages%09humb_~/image/tecnikakasi.jpg not exists. It may be wrong.
Check the relative path rendered.
Try
ImageUrl='<%# Eval("PictureUrl", "~/ProductImages/thumb_{0}") %>'
It is displaying "SubmitQuery" because your image is not being loaded. If your writer alternate text, then alternate text will be displayed.
You can view source of the generated html and check the path of the image and try to correct it for resolving this problem.
ImageUrl='<%# Eval("PictureUrl", "{0}") %>'

asp.net c# conditional within data repeater

I want to do something very simple, only display an asp:image when I have value (i.e. not NULL) for the current DataItem.
As below, Image1 should only be output when there is a value for Image1 (i.e. Eval("Image1")) which is a field in the current DataItem.
<asp:PlaceHolder ID="NewsPlaceHolder" runat="server">
<asp:Repeater ID="NewsRepeater" runat="server">
<ItemTemplate>
<div class="newsItem">
<h3><%# Eval("Title") %></h3>
<div class="images">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),1) %>' />
<asp:Image ID="Image2" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),2) %>' />
</div>
<div class="content"><%# Eval("Content") %></div>
</div>
<br class="clear" />
</ItemTemplate>
</asp:Repeater>
</asp:PlaceHolder>
Is there some simple conditional statements I can use in the ASPX page? Many thanks!
You can try to check if it is null from codebehind
<asp:Image ID="Image1" runat="server" visible='<%# HasData(Eval("image")) %>' ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),1) %>'
C#
public bool HasData(object img)
{
if(img!=null) {return true;}
return false;
}
# aspx page.
<div class="images">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),1) %>' Visible = '<%#(Container.DataItem != null) ? true: false) %>' />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# UrlHelper.NewsImageUrl((int)Eval("Id"),2) %>' Visible = '<%#(Container.DataItem != null) ? true: false) %>' />
</div>

DataList with button linked to a method (Vb.Net)

This my data list:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
RepeatLayout="Flow">
<ItemTemplate>
Titre:
<asp:Label ID="TitreLabel" runat="server" Text='<%# Eval("Titre") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("ID", "Handler.ashx?ID={0}") %>' Width="200" Height="200"/>
<br />
comments:
<asp:Label ID="commentsLabel" runat="server" Text='<%# Eval("comments") %>' />
<br />
Ajouter commentaire
<asp:button ID="btnAjouter" runat="server" Text="Ajouter" />
<br/>
<br/>
</ItemTemplate>
</asp:DataList>
In the Vb.aspx code I create a method:
public Sub updateComments()
.......
End Sub
And I want to add an event to my DataList button and excute the method.
I don't know how to do it correctly.
This is in Vb.net.
Thanks
Frank
You just need to add a CommandName to your button and handle the DataList's ItemCommand.
For example(in ItemTemplate)
<asp:button ID="btnAjouter" CommandName="Ajouter" runat="server" Text="Ajouter" />
In Codebehind:
Sub Item_Command(sender As Object, e As DataListCommandEventArgs)Handles DataList1.ItemCommand
If e.CommandName = "Ajouter"
' do something '
End If
End Sub

ASP.NET Eval DataBinding C#

You can do easily:
<asp:Repeater ID="rptCityFriends" runat="server" OnItemDataBound="rptFriendsContainer_DataBound">
<ItemTemplate>
<asp:ImageButton ID="imgThumb" runat="server" PostBackUrl='<%# Eval("FBUsuarioID","~/Friends.aspx?friendID={0}") %>' ImageUrl='<%# Eval("FBAvatarUsuario") %>' CssClass="imgThumbs" />
</ItemTemplate>
</asp:Repeater>
But if i want to pass two Querystrings?I.E:
<asp:Repeater ID="rptCityFriends" runat="server" OnItemDataBound="rptFriendsContainer_DataBound">
<ItemTemplate>
<asp:ImageButton ID="imgThumb" runat="server" PostBackUrl='<%# Eval("FBUsuarioID","FBNomeUsuario","~/Friends.aspx?friendID={0}&nUser={1}") %>' ImageUrl='<%# Eval("FBAvatarUsuario") %>' CssClass="imgThumbs" />
</ItemTemplate>
</asp:Repeater>
How can I set two or more querystrings to an URL in "Eval DataBinding"?
Use String.Format(), i.e.
<asp:Repeater ID="rptCityFriends" runat="server" OnItemDataBound="rptFriendsContainer_DataBound">
<ItemTemplate>
<asp:ImageButton ID="imgThumb" runat="server" PostBackUrl='<%# String.Format("~/Friends.aspx?friendID={0}&nUser={1}", Eval("FBUsuarioID"), Eval("FBNomeUsuario")) %>' ImageUrl='<%# Eval("FBAvatarUsuario") %>' CssClass="imgThumbs" />
</ItemTemplate>
</asp:Repeater>

Resources