Use the 'If condition' in Item Template [ASP] - asp-classic

I have two elements; <img> and <video> in an ItemTemplate, I want to test the value <% # Eval ("Url")%> to choose an element to display.
<itemtemplate>
<li>
<video data-cycle-cmd="pause" id='my-video' class='video-js' controls preload='auto' width='980' height='452' poster='MY_VIDEO_POSTER.jpg' data-setup='{}'>
<source src='<%# Eval("Url") %>' type='video/mp4'>
</video>
</li>
<li>
<img src="<%# Eval("url") %>">
</li>
</itemtemplate>

You can use a ternary operator in the template and based on that outcome show/hide the correct element.
<ItemTemplate>
<li>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Eval("SomeValue").ToString() == "video" ? true : false %>'>
<video data-cycle-cmd="pause" id='my-video' class='video-js' controls preload='auto' width='980' height='452' poster='MY_VIDEO_POSTER.jpg' data-setup='{}'>
<source src='<%# Eval("Url") %>' type='video/mp4'>
</video>
</asp:PlaceHolder>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Url") %>' Visible='<%# Eval("SomeValue").ToString() == "video" ? false : true %>' />
</li>
</ItemTemplate>

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() %>' />

How to find control bounded in item template on master page from content page?

I am having a nested repeater on the master page. And a hiddenfield in its item template.
i want the value of hiddenfield on content page.Like this
<ul class="categories">
<li>
<div id='cssmenu'>
<h4>Categories</h4>
<asp:Repeater ID="repcategory" runat="server" OnItemDataBound="repcategory_ItemDataBound">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hf1" Value='<%# Eval("CategoryID") %>' runat="server" />
<li class="active has-sub">
<a href='#'><span>
<%#Eval("CategoryName") %></span></a>
<asp:Repeater ID="repsubcategory" OnItemDataBound="repsubcategory_ItemDataBound" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hf2" Value='<%# Eval("SubCategoryID") %>' runat="server" />
<li class="has-sub">
<a href='#'><span>
<%#Eval("SubCategoryName") %></span></a>
<asp:Repeater ID="repsubcategory2" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hf3" Value='<%# Eval("SubCategory2ID") %>' runat="server" />
<li>
<a href="ClientProductSubCategory2.aspx"><span>
<%#Eval("SubCategory2Name") %></span></a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</div>
I want the value of subcategory2id on the content page and codded like this.
HiddenField hiddensubcategory2id = (HiddenField)Master.FindControl("hf3");
DataSet ds = new ClientProductView().GetAllProductSubCategory2(hiddensubcategory2id.Value);
repContent.DataSource = ds;
repContent.DataBind();
But this is returning a null value. Please help me to solve this problem
Try finding the control within the repeater object itself, like this:
var hiddensubcategory2id = repsubcategory2.FindControl("hf3") as HiddenField;
Note: You should always check to see if the result of a FindControl() is null or not, like this:
if(hiddensubcategory2id != null)
{
// Do something with the control you found
}

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>

Anchor Link in DataGrid ItemTemplate

I have the following markup in a DataGrid:
<itemtemplate>
<a href='~/File.aspx?item=<%# DataBinder.Eval(Container.DataItem, "ItemID").ToString() %>'
runat='server'><%# DataBinder.Eval(Container.DataItem, "Title").ToString() %>
</a>
</itemtemplate>
But it's rendering the following HTML:
<a href="../File.aspx?item=<%# DataBinder.Eval(Container.DataItem, "ItemID").ToString() %>">
My Link
</a>
What am I doing wrong?
Try this.
<asp:TemplateColumn>
<ItemTemplate>
<a id="A1" runat='server' href='<%# "~/File.aspx?item=" + Eval("ID")%>'>
<%# Eval("Job") %>
</a>
</ItemTemplate>
</asp:TemplateColumn>
You have to append it
<a href='~/File.aspx?item=' + <%# DataBinder.Eval(Container.DataItem, "ItemID").ToString() %>
Otherwise if you use hyperlink, It will be very easy. e.g.
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%#Eval("ItemID", "~/File.aspx?item={0}")%>'
Text='<%# DataBinder.Eval(Container.DataItem, "Title").ToString() %>'></asp:HyperLink>
</ItemTemplate>

Accessing SiteMapNode Container.DataItem from outside of Parent Repeater

I am trying to access the current 'active' top level node from a sitemap repeater from outside of the ASP.NET repeater used to render it, this is for CSS styling purposes as I want to place the child nodes on the subsequent row with different styling horizontally. At present I have the following code which I can't get to display correctly using CSS.
<asp:SiteMapDataSource ID="topNavLevel" runat="server" ShowStartingNode="false" />
<asp:Repeater runat="server" ID="rptParent" DataSourceID="topNavLevel">
<HeaderTemplate><ul id="lawMenu" class="topMenu"></HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" ID="parentLink" NavigateUrl='<%# Eval("Url") %>'><span class="t"><%# Eval("Title") %></span><span class="l"></span><span class="r"></span></asp:HyperLink>
<asp:Repeater ID="rptChild" runat="server" DataSource='<%# ((SiteMapNode) Container.DataItem).ChildNodes %>'>
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="childLink" runat="server" NavigateUrl='<%# Eval("Url") %>'><span class="t"><%# Eval("Title") %></span><span class="l"></span><span class="r"></span></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</li>
</ItemTemplate>
<FooterTemplate>
</ul></FooterTemplate>
</asp:Repeater>
I would like to display the child nodes on the next light blue element which I can do perfectly well from a seperate div if this was not rendered using a child repeater. In the image below Blog and Services are top level nodes and their subseqent nodes (2 for each) should be displayed on the light-blue row below. Is this possible?
To get the parent repeaters DataItem as if you weren't inside your child repeater:
<%# DataBinder.Eval(((System.Web.UI.WebControls.RepeaterItem)Container.Parent.Parent).DataItem, "Property") %>
I have solved this now. For anyone else coming across this post here's the solution:
<asp:SiteMapDataSource ID="topNavLevel" runat="server" ShowStartingNode="false" />
<asp:Repeater runat="server" ID="rptParent" DataSourceID="topNavLevel">
<HeaderTemplate>
<ul id="lawmenu" class="law-menu">
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink runat="server" ID="parentLink" NavigateUrl='<%# Eval("Url") %>'><%# Eval("Title") %></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
<div class="law-nav_nav2">
<asp:SiteMapDataSource ID="secondNavLevel" runat="server" ShowStartingNode="false" StartingNodeOffset="1" />
<asp:Repeater ID="rptChild" runat="server" DataSourceID="secondNavLevel">
<HeaderTemplate>
<ul class="law-menu_nav2"style="z-index:100">
</HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="childLink" runat="server" NavigateUrl='<%# Eval("Url") %>'><%# Eval("Title") %></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
</div>
The HeaderTemplate takes care of the list container styling then the repeater items are listed out one at a time with an offset of 1 for the current node. This looks easy based on what I have seen around the net, I'm just fairly new to some elements of ASP.NET :) Thanks.

Resources