I have a repeater that evaluates the content field from a page. This content may have inline widgets in it:
Now, normally I would wrap Eval("Content") in a placeholder with EnableViewState="false" and then resolve it dynamically in code behind but the problem is that the repeater repeats the ID for the placeholder rendering it invalid.
Is there anyway around this?
<cms:CMSUniView ID="MainNavMenu" runat="server" >
<HeaderTemplate>
<ul>
<li>
</HeaderTemplate>
<ItemTemplate>
<a href="<%# Eval("Link") %>"
title="<%# HTMLHelper.HTMLEncode( Convert.ToString(Eval("DocumentName"))) %>" >
<%# HTMLHelper.HTMLEncode( Convert.ToString(Eval("DocumentName"))) %>
</a>
<%# Eval("Content") %>
</ItemTemplate>
<SeparatorTemplate>
</li>
<li>
</SeparatorTemplate>
<FooterTemplate>
</li>
</ul>
</FooterTemplate>
</cms:CMSUniView>
You can try to resolve the content by using CMS.MacroEngine.MacroResolver.Resolve() method. So your code will look like:
<%# MacroResolver.Resolve(Eval("Content").ToString()) %>
And if you want to find any control inside a repeater template, I guess you should do it on ItemDataBound event. Example
Related
I have a listview which contain an tag inside it, also it contain an href, but the href is not working. Here is the code
<asp:ListView ID="listsearch" runat="server" ItemPlaceholderID="itemsearch">
<LayoutTemplate>
<ul class="ada"><asp:PlaceHolder ID="itemsearch" runat="server"/></ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%# String.Format("https://www.google.co.in") %> ' Text="Ssdsd"/>
</li>
</ItemTemplate>
</asp:ListView>
HTML output:
<a id="ctl00_ContentPlaceHolder1_listsearch_ctrl0_LinkButton1" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$listsearch$ctrl0$LinkButton1", "", false, "", "google.co.in";, false, true))' style="border-width: 1px;">Ssdsd</a>
<asp:LinkButton> control is intended for simulate the behaviour of an <asp:button> but in hyperlink taste. If you do not need server processing on click event better use <asp:hyperlink> or simply non-server html <a> tag:
I guess you've a property like Url in the DataSource passed to the ListView, so you replace your LinkButton for something like that:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("myUrl") %>'
Target="_blank"><%# Eval("myUrl") %></asp:HyperLink>
Or better:
<%# Eval("MyUrl") %>
And also opens the url in a new tab!
I'm using ASP.NET Web Forms, and I've got a Repeater control to display some data. Currently it looks like this:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<strong>Details:</strong> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<strong>Status:</strong> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
This list has hover behaviour, etc. and I'd like to reuse the same style of list across my project, just with different data (really just replacing the three lines with <strong> tags in). However, I obviously don't want to copypaste and change it every time it's used, so I thought I'd try and make it into a User Control:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<%= DataString %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And in my corresponding class in CodeBehind:
public string DataString { get; set; }
So my main aim was to do this:
<uc:MyUserControl runat="server" ID="blah" DataString="<strong>Date/Time:</strong><%# DataBinder.Eval(Container.DataItem, "date") %> ETC..." />
But it seemed that ASP.NET wanted to evaluate the <% %> in the string there and then, and I tried different ways to get around it such as this:
<% string theDataString = "<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %>"; %>
<uc:MyUserControl runat="server" ID="blah" DataString='<%= theDataString %>' />
But I ran into trouble again with the closing %> in theDataString, and even after replacing it with something like {0: %} (to escape the % sign: can't remember the exact syntax), which did escape it correctly, I discovered that you can't actually use the <%= %> inside an attribute, which was annoying.
My last-ditch attempt was to set DataString in my page's CodeBehind, and the HTML <strong> rendered correctly, but ASP.NET didn't evaluate the <%# %> and just spat it out literally. So I'm kind of stuck as I can't think of anything else... (by the way I'm still relatively new to ASP.NET)
I'm not even sure this is a good way to go about it - previously I thought about making it into a templated user control but this method seemed simpler and it was something I already knew how to do, or so I thought. So any help with this would be greatly appreciated, thanks!
EDIT: Just to be clear, my emphasis here is on the re-usability of this particular control - so I will be able to display different types of data records in this style of list (clickable, highlight behaviour etc).
If your goal is to have three lines appear as bold instead of normal text, then your best bet is to use something more geared towards website-display than something programmatic and heavy-handed like a dynamic server-side user control. For example:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<span class="displayMode">Date/Time:</span> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<span class="displayMode">Details:</span> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<span class="displayMode">Status:</span> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And then, programmtically show:
<style type="text/css">
.displayMode { font-weight: bold; }
/* and/or */
.displayMode { text-decoration: underline; }
</style>
Use the right tool for the job, in this case, I don't feel as though server-side code is the right way to accomplish this task. If, on the other hand, you need to change the content of the insides of your <li> tags, then yes you would have to do it in C#. However, if that were the case you could easily bind it to a different object or use a different control or inherit the control or any number of other methods. For your specific case, just use standard HTML manipulation techniques.
I am trying to add a conditional statement to my repeater, but I am receiving the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
The code I am trying to use is as follows:
<asp:Repeater ID="repLeftmenu" DataSourceID="xmlMenu" runat="server" EnableViewState="false">
<HeaderTemplate>
<ul class="leftMenu">
</HeaderTemplate>
<ItemTemplate>
<li>
<% If XPath("#url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString Then%>
<a href="<%# XPath("#url") %>">
<%# XPath("#title")%>
</a>
<% Else %>
<a href="<%# XPath("#url") %>">
<%# XPath("#title")%>
</a>
<% End If%>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
Is there something wrong with my syntax?
Thanks,
crjunk
You are using databinding expresions in regular code blocks, which is not supported. Databinding expressions should appear inside <%# ... %> blocks. Regular code blocks look like this: <% ... %>. (The only difference is the # at the beginning of the block.)
If you need a conditional inside a databinding block, you need to use an expression (simply said: a one-liner). The only way to accomplish this is by using the following code:
<%# If(XPath("Url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString, "Display this when true", "Display this when false") %>
This can quickly become unwieldy when whole blocks of HTML are conditionally displayed. What you could do then is to create both blocks and set the visibility of each based on the conditional expression:
<span runat="server" visible='<%# XPath("Url").ToString = <%# If(XPath("Url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString %>'>
HTML that should be displayed when condition is true
</span>
<span runat="server" visible='<%# XPath("Url").ToString <> <%# If(XPath("Url").ToString = HttpContext.Current.Request.Url.AbsolutePath.ToString %>'>
HTML that should be displayed when condition is false
</span>
I have the following ListView code:
<asp:ListView ID="lvOrders" runat="server">
<LayoutTemplate>
<ul id="orderList">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<% if (Mode == AdminSingle) { %>
<%# Eval("Offertype")%>
<% } %>
<%# Eval("Something")%>
</li>
</ItemTemplate>
</asp:ListView>
Now, my problem is that Eval("Offertype") is being evaluated in any case. But the sql query is pretty heavy for that part and I would like to only run that part of the query if I really need too. Is there a clean way around this?
I know I could do this:
<%# GetStatusInfo(Container.DataItem)%>
But then I get the display logic into my code....
i have a repeater that will output a series of items:
<asp:repeater ... runat="Server">
<itemtemplate>
<%# GetItemText %>
<itemtemplate>
<asp:repeater>
But some items will not have an associated link, so i don't want them to be clickable. i tried making it a runat=server HtmlAnchor, and set the htmlAnchor.Disabled = true for the items are should not actually have a link - but they can still be clicked (it just makes the text gray)
i know how i'd do it in the olden days:
<% If IsLink Then %>
<A href="<% =GetItemLink%">
<% End If %>
<% =GetItemText %>
<% If IsLink Then %>
</A>
<% End If %>
But that's the messy mixing code and html ASP way. What's the ASP.NET way?
Use an <asp:HyperLink > control, which displays the text normally if no link is supplied.
Edited to include example:
<asp:repeater ... runat="Server">
<itemtemplate>
<asp:HyperLink ... runat="server" NavigateUrl="<%# GetItemLink(...) %>"> <%# GetItemText %></asp:HyperLink>
<itemtemplate>
<asp:repeater>
In the above example, the anchor tag will be rendered to html regardless, but if the NavigateUrl attribute is an empty string, there will be no href at all and every browser I've ever used renders the text in a manner similar to spans (so look out for custom styles on <a >'s).