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

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>

Related

How to loop through data in WebForms like in MVC

How do I loop through data in WebForms like I do in ASP.NET MVC? For instance, in MVC, this is as simple as:
<table>
#foreach (var myItem in g)
{
#<tr><td>#MyItem.title<td></tr>
}
</table>
What would the code behind look like?
Or, can I add an MVC project to a WebForms application so that I can use MVC functionality, instead?
Rather than use a repeater, you can just loop through the list in a similar MVC type way using the <% %> and <%= %> tags.
<table>
<% foreach (var myItem in g) { %>
<tr><td><%= myItem.title %></td></tr>
<% } %>
</table>
As long as the property you're looping through is acessible from the aspx/ascx page (e.g. declared as protected or public) you can loop through it. There is no other code in the code behind necessary.
<% %> will evaluate the code and <%= %> will output the result.
Here is the most basic example:
Declare this list at your class level in your code behind:
public List<string> Sites = new List<string> { "StackOverflow", "Super User", "Meta SO" };
That's just a simple list of strings, so then in your aspx file
<% foreach (var site in Sites) { %> <!-- loop through the list -->
<div>
<%= site %> <!-- write out the name of the site -->
</div>
<% } %> <!--End the for loop -->
In WebForm you can use Repeater control:
<asp:Repeater id="cdcatalog" runat="server">
<ItemTemplate>
<td><%# Eval("title")%></td>
</ItemTemplate>
</asp:Repeater>
In code behind:
cdcatalog.DataSource = yourData;
cdcatalog.DataBind();
You can use a Repeater with any sort of valid DataSource (SqlDataSource, EntityDataSource, ObjectDataSource) object:
Define the DataSource
Reference the DataSource in your Reperater
....
<asp:Repeater id="someRep" runat="server" DataSourceID="YourDataSource">
<ItemTemplate>
<tr>
<td><%# Eval("PropertyName") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
...

How to get the value inside a repeaters itemtemplate?

<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>

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?

Using Eval inside a IF statement and 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>

Is it possible to use Data-Binding Expressions directly in markup to show/hide content?

I would like to do something like:
<%# if((bool)Eval("IsDisabled")){ %><span>Disabled</span><% }
else { %><span>Active</span><% } %>
but I don't think its possible.
There is a way to create method in codebehind which returns appropriate string and call it, but thats not an option.
You can use placeholders to hold the two versions of your markup and then use the Visible property to show the relevant one. Something like this... Note the use of ! before the call to IsDisabled in the second Visible property.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# IsDisabled((bool) Eval("IsDisabled")) %>'>
<span>Disabled</span>
</asp:PlaceHolder>
<asp:PlaceHolder ID="PlaceHolder2" runat="server" Visible='<%# !IsDisabled((bool) Eval("IsDisabled")) %>'>
<span>Active</span>
</asp:PlaceHolder>
The code behind IsDisabled method looks like this...
public bool IsDisabled (bool isDisabled)
{
return isDisabled;
}
Its not possible to use # eval in if statement,
You have some options to solve that:
You can put the condition of the if in a previous line then check on this variable in the if
example:
in code behind:
protected bool isDisabled;
in aspx:
<%# isDisabled=(bool)Eval("IsDisabled") %>
<% if(isDisabled) %>
Other way is to call a code behind method which return bool and check on it in the if.

Resources