views:

522

answers:

2

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.

A: 

Try putting your RenderPartial inside <%# %> statement like:

<asp:Repeater runat="server">
    <ItemTemplate>
        <%# Html.RenderPartial("UserControl1",Eval("Title")); %>
    </ItemTemplate>
</asp:Repeater>
Robert Koritnik
Thanks for your help but I got this compilation exception:CS1026: ) expectedI test it before it wont work thanks again...
Khaled Musaied
+4  A: 

<%# %> 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...

Sam
+1 for "don't make life any harder than it needs to be".
Brad Wilson
Every thing is easy but <code><% Html.RenderPartial("UserControl1", o.Title);%></code> is making it hard it is different from other html extentions Thanks anyway
Khaled Musaied