<asp:Repeater ..>
<ItemTemplate>
<% string age = Eval("a").ToString() %>
<%
age = a.ToLower(); // real stuff here
%>
<p>Hello <%# Eval("name") %> you are <%= age %> old</p>
</ItemTemplate>
</asp:Repeater>
I'm getting an error saying:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Use <%# Eval("<propertyName>") %>
Of course you will have to assign a DataSource to your repeater, and call DataBind()
And, without using those inline coding, you can wrap the whole logic to a custom property for your data item. For example, as in the above code, you can create a custom property say, Age like:
partial class YourDataItemClass // use partial if it is auto-generated
{
public string Age
{
var ageStr = a.ToString(); // assuming YourDataItemClass has an `a` var/property
// Do real stuff here
...
...
var lowered = ageStr.ToLower();
...
...
return lowered;
}
}
and you can expose that property inside the repeater control like:
<asp:Repeater id="myRepeater" ..>
<ItemTemplate>
<p>Hello <%# Eval("Name") %> you are <%# Eval("Age") %> old</p>
</ItemTemplate>
</asp:Repeater>
Assign datasource and databind the repeater somewhere in the code-behind like:
...
// Call the method which provides you the data
// IEnumerable<YourDataItemClass> myData = ... ;
myRepeater.DataSource = myData;
myRepeater.DataBind();
...
<asp:Repeater>
<ItemTemplate>
<p>Hello <%# Eval("name") %> you are <%# Convert.ToString(Eval("a")).ToLower() %> old</p>
</ItemTemplate>
</asp:Repeater>
Related
In Repeater control I can access the dataitem property in item template using <%# Eval("IsAvailable") %>,I want to apply if condition on IsAvailable and toggle showing div using <% %> syntax,how do I achieve that?
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<% if([IsAvailable]){ %>
<div>test1</div>
<% }else{ %>
<div>test2</div>
<% }%>
</ItemTemplate>
</asp:Repeater>
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.
I am binding a List<string> to a Repeater control. Now I want to use the Eval function
to display the contents in ItemTemplate like
<%# Eval("NAME") %>.
But I am not sure what I should use instead of NAME.
Just use <%# Container.DataItem.ToString() %>
If you are worried about null values you may want to refactor to this (.NET 6+)
<asp:Repeater ID="repeater" runat="server">
<ItemTemplate>
<%# Container.DataItem?.ToString() ?? string.Empty%>
</ItemTemplate>
</asp:Repeater>
Note if you are using less than .NET 6 you cannot use the null-conditional operator Container.DataItem?.ToString()
Set the ItemType to System.String
<asp:Repeater ItemType="System.String" runat="server">
<ItemTemplate>
<%# Item %>
</ItemTemplate>
</asp:Repeater>
rptSample.DataSource = from c in lstSample select new { NAME = c };
in the repeater you put
<%# Eval("NAME") %>
This should work just fine:
<ItemTemplate>
<%=this.GetDataItem().ToString() %>
</ItemTemplate>
A more complete example based on the LINQ provided by #RobertoBr:
In code behind:
List<string> notes = new List<string>();
notes.Add("Value1")
notes.Add("Value2")
repeaterControl1.DataSource = from c in notes select new {NAME = c};
repeaterControl1.DataBind();
On page:
<asp:Repeater ID="repeaterControl1" runat="server" >
<ItemTemplate>
<li><%# Eval("NAME") %></li>
</ItemTemplate>
</asp:Repeater>
you have to use the databind syntax here or it will not work.
<%# this.GetDataItem().ToString() %>
Inside Item Template
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>'></asp:Label>
<ItemTemplate>
or Simply Add inside Item Template
<%# Eval("YourEntityName").ToString() ==""? "NA" : Eval("YourEntityName").ToString()%>
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>
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>