Using Eval inside a IF statement and Repeater - asp.net

I am trying to use Eval inside a IF Statement and Repeater.
I want to do something like this:
<asp:Repeater runat="server" ID="rpRepeater">
<ItemTemplate>
<% if ((bool)Eval("A_Boolean"))
{ %>
blah...
<% } %>
</ItemTemplate>
</asp:Repeater>
This code gives me the following error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Eval can be only used inside "binding" tag.
<%# Eval("A_Boolean") %>
http://support.microsoft.com/kb/307860#1a

It is possible to simulate if statements like this (code goes within ItemTemplate).
<asp:Panel runat="server" Visible='<%# Eval("A_Boolean") %>'>
blah...
</asp:Panel>

Related

How to get parent datasource Eval() inside a nested Repeater? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Accessing parent data in nested repeater, in the HeaderTemplate
I have a nested repeater being databound... let's say the top level repeater is an OrderRow and the inner repeaters are bound to LineItem rows from my DB. ASPX is something like this:
<asp:Repeater ID="rptOrder" runat="server">
<ItemTemplate>
<%# Eval("OrderID") %>:<br/>
<asp:Repeater ID="rptLineItems" runat="server">
<ItemTemplate>
<%# Eval("SomeColumn1"); %>
<%# Eval("SomeColumn2"); %>
<%# Eval("SomeColumn3"); %>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddLine" runat="server" CommandArgument=<%# ???? %> />
</FooterTemplate>
</asp:Repeater>
</ItemTemplate
</asp:Repeater>
Now the button on the inner footer will be used to add a new line item... but the command argument needs to be the OrderID from the outer repeater, so we know which order to add to. Obviously a regular Eval() call won't work here, because it will have the inner repeater's DataRowView as a source [actually, it won't, since it's in the footer]. How do I get this value? Am I going to have to set this dynamically in the ItemDataBound event of the outside repeater?
Since the call are serial you can use the code behind to save the last order id and use it later.
Here is the idea.
<asp:Repeater ID="rptOrder" runat="server">
<ItemTemplate>
<%#GetOrderID(Container.DataItem)%><br />
<asp:Repeater ID="rptLineItems" runat="server">
<ItemTemplate>
<%# Eval("SomeColumn1"); %>
<%# Eval("SomeColumn2"); %>
<%# Eval("SomeColumn3"); %>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAddLine" runat="server" CommandArgument=<%=cLastOrderID%> />
</FooterTemplate>
</asp:Repeater>
</ItemTemplate
</asp:Repeater>
and on call behind
public int cLastOrderID = -1;
protected string GetOrderID(object oItem)
{
cLastOrderID = (int)DataBinder.Eval(oItem, "OrderID");
return cLastOrderID.ToString();
}
What I do here is that I call the GetOrderID to return the OrderID and I save it in a global value, and then later in the footer I use this global value. Hope this helps you.

using an if/then in a repeater in an asp.net page

I haven't used asp.net webforms in what seems like forever, and I am having the toughest time figuring out how to get a simple if/then statement to work. This is what I want to do:
<asp:Repeater ID="rpt" runat="server" DataSourceID="lds">
<ItemTemplate>
<% if(Eval("show")) { %> show something <% } %>
</ItemTemplate>
</asp:repeater>
But obviously that gives me an error - how do I do this? Thank you - I have completely gone to MVC now and I can't seem to remember this stuff.
You are missing an ItemTemplate
<asp:Repeater ID="rpt" runat="server" DataSourceID="lds">
<ItemTemplate>
<%# Eval("show") ? Eval("Whatever") : Eval("Whatever") %>
</ItemTemplate>
</asp:repeater>
Try the conditional operator
<%= Convert.ToBoolean(Eval("show")) ? "something" : "" %>
If you just want to show a simple string or something, you can do this:
<%# bool.Parse(Eval("show")) ? "show something" : null %>
What exactly are you trying to show/not show?

ASP.NET FormView: Including a Member Variable in an XPath Expression

Is something like this possible?
<asp:FormView ID="myFormView" runat="server">
<ItemTemplate>
<p><%# XPath("Root/Path/Item[#id=<%=this.m_myId%>]/HelloWorld")%></p>
</ItemTemplate>
</asp:FormView>
m_myId is a member variable on the page. I can't seem to get it to work without a parsing error.
You can't have nested asp tags. Try something like this:
<%# XPath("Root/Path/Item[#id= " + this.m_myId + "]/HelloWorld") %>

Passing Container.Eval to (Html.ReaderPartial) inside ASP.NET Repeater Control

I am trying to pass Eval to Html.RenderPartial inside ASP.NET Repeater but it does not work can any one help?
<asp:Repeater runat="server">
<ItemTemplate>
<% Html.RenderPartial("UserControl1",Eval("Title")); %>
</ItemTemplate>
</asp:Repeater>
by the way I know that I can do it in other ways but I want to know if it is doable or not.
is the same as in that it expects an expression that returns a string, so to get this compiling you have to call a method that calls Html.RenderPartial(), then returns an empty string:
<%
protected string RenderControl(object dataItem)
{
Html.RenderPartial("UserControl1", ((MyType) dataItem).Title);
return "";
}
%>
... <%# RenderControl(Container.DataItem) %> ...
I would just use foreach though - mixing WebForms data-binding and MVC partial rendering is unpredictable, at best:
<% foreach (MyObject o in data) { Html.RenderPartial("UserControl1", o.Title); } %>
Don't make life any harder than it needs to be...
Try putting your RenderPartial inside <%# %> statement like:
<asp:Repeater runat="server">
<ItemTemplate>
<%# Html.RenderPartial("UserControl1",Eval("Title")); %>
</ItemTemplate>
</asp:Repeater>

Unable to bind in asp.net grid Template Column

I am having trouble accessing the data field. I receive the error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I can get the value but using <%# getOpenJobs((string)Eval("ParentPart")) %> but I need to use it in the if to display a certian picture if it passes the condition. Is there a better way to do this or am i just missing something simple?
<telerik:GridTemplateColumn UniqueName="hasOpenJobs" HeaderText="">
<ItemTemplate>
<% if (getOpenJobs((string)Eval("ParentPart")) > 1)
{ %>
<img src="../images/job-icon.gif" alt="Open Jobs" />
<%} %>
</ItemTemplate>
</telerik:GridTemplateColumn>
In these cases, I usually create a method in the code-behind to send back the final generated HTML. E.g.
<ItemTemplate>
<%# GetJobImageHtml((string)Eval("ParentPart")) %>
</ItemTemplate>
Then do whatever logic you need in the GetJobImageHtml() method and return a HTML string.

Resources