Hyperlink display image in ASP.NET Datalist - c#

I am trying to get an image to display on a page using a Hyperlink inside a datalist.However, the image will not display, I just get the correct number of boxes with what I call the no image picture. I know that it is retrieving the filepath correctly. The tooltip shows the correct path, it is the exact path going into a repeater that works, but does not display the image. Any help would be greatly appreciated.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="HomePagePhotosList.ascx.cs" Inherits="UserControls_HomePagePhotosList" %>
<asp:DataList ID="list" runat="server" Width="1000px" CssClass="EventsList"
RepeatDirection="Horizontal">
<HeaderStyle CssClass="DepartmentsListHead" />
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:HyperLink
ID="HyperLink1"
Runat="server"
ImageUrl='<%#System.String.Format("Images/{0}", DataBinder.Eval(Container.DataItem, "Filepath")) %>'
NavigateUrl='<%# Link.ToEditMinutesAdmin(Eval("ImageID").ToString())%>'
ToolTip='<%# System.String.Format("Images/{0}", DataBinder.Eval(Container.DataItem, "Filepath")) %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:DataList>

In order to fix this problem I had to change the
ImageUrl='<%#System.String.Format("Images/{0}", DataBinder.Eval(Container.DataItem, "Filepath")) %>'
To
ImageUrl='<%#System.String.Format("~/Images/{0}", DataBinder.Eval(Container.DataItem, "Filepath")) %>'

Related

Manage Hyperlink base on the condition in Repeater ASP.NET C#

How to manage Hyperlinks base on AttachmentID, In Inside link button there is two hyperlink to manage, If AttachmentID is "NA" Then Hyperlink ID one should visible else Hyperlink ID two should visible. I tried lots into google like this code not able to find. I tried using ItemCommand and ItemDataBound but did not understand this concept. The main concept to do this manage target="_blank".
Below is my Repeater Code.
<asp:Repeater ID="Repeater_News1" runat="server" OnItemDataBound="Repeater_News1_ItemDataBound">
<ItemTemplate>
<asp:Image ID="Image2" runat="server" class="pull-left img-responsive" ImageUrl='<%# Bind("ImageName", "~/images/news_images/{0}") %>' />
<asp:LinkButton ID="lnkbtn_check" runat="server" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Attachmentid") %>'>
<a href='<%# DataBinder.Eval(Container.DataItem, "Attachment")%>' id="one"
target="_blank">
<%# DataBinder.Eval(Container, "DataItem.Heading")%></a>
<a href='<%# DataBinder.Eval(Container.DataItem, "Attachment")%>' id="two">
<%# DataBinder.Eval(Container, "DataItem.Heading")%></a>
</asp:LinkButton>
</h4>
<p>
<%# DataBinder.Eval(Container, "DataItem.SmallDescription")%></p>
</ItemTemplate>
</asp:Repeater>
Use this in your repeater:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# Eval("AttachmentID").ToString() != "NA" %>' Text='<%# Eval("DataItem.Heading") %>' Target="_blank" />
You can set the Visibility with an if statement in the HyperLink itself: Visible='<%# Eval("AttachmentID").ToString() != "NA" %>'
UPDATE
You can also check the AttachmentID for IsNullOrEmpty and show the correct hyperlink.
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# string.IsNullOrEmpty(Eval("AttachmentID").ToString()) %>' Text='<%# Eval("DataItem.Heading") %>' Target="_blank" />
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# Eval("Attachment") %>' Visible='<%# !string.IsNullOrEmpty(Eval("AttachmentID").ToString()) %>' Text='<%# Eval("DataItem.Heading") %>' Target="_self" />

Concatenate Host of a URL with other items in ItemTemplate as Anchor

I have an ASP.NET gridview control with a custom column that is an anchor tag and I'm having some issues with setting the URL.
Based on what I've constructed below, I would expect the that the HREF would come through as "myhost.local/Orders/FileName.PDF", but what I'm seeing it come through as"myhost.local/current directory/current page/myhost.local/Orders/FileName.pdf". Any assistance would be greatly appreciated!
The Gridview
<asp:GridView ClientID="GV" AllowPaging="true" OnPageIndexChanging="gv_Search_PageIndexChanging" PageSize="10" ID="gv_Search" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="Disciplinary Orders" SortExpression="defendant_name">
<ItemTemplate>
<a href="<%# HttpContext.Current.Request.Url.Host %>/Orders<%# Eval("FileName") %> " target="_blank">
<%# Eval("FullName") %> - <%# Eval("CaseNumber") %> - Get Disciplinary Status </a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I was able to get the correct URL by changing to an ASP HyperLink and using String.Format()
<ItemTemplate>
<asp:HyperLink
runat="server"
id="link"
NavigateUrl='<%#String.Format("{0}", "/Orders/" + Eval("FileName")) %>'
Target="_blank">
<%# Eval("FullName") + " - " + Eval("CaseNumber") %> - Get Disciplinary Status
</asp:HyperLink>
</ItemTemplate>

How can I add .aspx inside HyperLink in DataList?

I have created an DataList with Hyperlinks inside of it. But... It's redirecting me to for example: /itemName, but i want to redirect it to /itemName.aspx. When i add to the Eval .aspx its putting an error.
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" Width="120px">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("itemName") %>' Text='<%# Eval("nazwa") %>'></asp:HyperLink>
</ItemTemplate>
</asp:DataList>
Any ideas?
Problem solved by editing Eval like this:
Eval("itemName", "~/{0}.aspx")

Can't reference Image ID in C# Code. Why?

I would like to know, why I can't reference "asp:Image" (Image1) control in my code, but I can reference datalist control (DataList1) which contains asp:Image. Here is my code:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="imageHolder">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" Width="100%">
<ItemTemplate>
<asp:Image ID="Image1" OnDataBinding="DataList1_DataBinding" Width="80%" Height="100px"
CssClass="datalistImages" runat="server" ImageUrl='<%# "http://mywebsite.com/" + Eval("url") %>' />
</ItemTemplate>
</asp:DataList>
</asp:Content>
And I'm 100% sure I'm working in the correct class. Any suggestions?
Because it's in the item collection.
If you want to reach that image control at real time you have to use ItemCreated or ItemDataBound events.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.items%28v=vs.110%29.aspx
Datalist events.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist_events%28v=vs.110%29.aspx

Operand "+" with Bind/Eval in aspx files

I have this aspx code:
<asp:TemplateField HeaderText="Name" SortExpression="Firmierung">
<ItemTemplate>
<asp:HyperLink ID="HyperLink" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
I want to add an if condition so that I can use operand + with Text, something like this:
<asp:TemplateField HeaderText="Name" SortExpression="Firmierung">
<ItemTemplate>
<% if(Condition is true) { %>
<asp:HyperLink ID="HyperLink" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") + ("Active") %>' />
<% } else { %>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>' Text='<%#Bind("Name") %>' />
<% } %>
</ItemTemplate>
</asp:TemplateField>
I don't know how to use operand + in this case. Any help appreciated. Thanks!
Try this one
Text='<%# String.Format({0}{1},Eval("Name"),Eval("Active")) %>'
OR
Text='<%# String.Format({0}{1},Bind("Name"),Bind("Active")) %>'
For more help visit the link:
HyperLink with NavigateUrl with Eval(). Where is the mistake?
You can't do it with Bind. Although with Eval you can add some C# code with it, with Bind it is not possible. Eval is really a method call but Bind is just a declaration that turned by the framework to some binding code, and its format has to be Bind("FieldName") (You can add formatting though).
The Hyperlink Text property is not editable by the client so Eval should be enough and you can write it in one row.
<asp:HyperLink ID="HyperLink" runat="server"
NavigateUrl='<%#Eval("Id", "DetailInfo.aspx?Id={0}") %>'
Text='<%# Eval("Name") + (Condition ? + Eval("Active") : "") %>' />

Categories