开发者

DataRepeater without subcontrols

开发者 https://www.devze.com 2022-12-10 23:33 出处:网络
What is the right way to do this in a d开发者_如何转开发atarepeater control? <asp:Repeater ID=\"Repeater1\" runat=\"server\">

What is the right way to do this in a d开发者_如何转开发atarepeater control?

                <asp:Repeater ID="Repeater1" runat="server">
                <ItemTemplate>
                <strong><%= Eval("FullName") %></strong><br />
                <p>
                <%= Eval("Summary") %>
                </p>
                </ItemTemplate>
                </asp:Repeater>

Getting error Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

I'd like to just write out the FullName and Summary. But I don't want to nest subcontrols. Is Repsonse.Write the best way?

UPDATE: Not sure if this is necessary, but the only way I was able to solve it was with controls


The repeater requires a datasource, assigned like so:

public class Foo
{
public string FullName { get;set; } 
public string Summary {get;set; }

public Foo(fullName,summary)
{
  FullName=fullName;
  Summary=summary;
}
}

/// elsewhere...
List<Foo> myFoos = new List<Foo>();
myFoos.Add(new Foo("Alice","Some chick"));
myFoos.Add(new Foo("Bob","Some guy"));
myFoos.Add(new Foo("Charlie","Indeterminate"));
Repeater1.DataSource = myFoos;
Repeater1.DataBind();

As this example shows, your datasource can be anything that implements IEnumerable - lists are my favorites, but most collections in C# fall into this category. Your datasource does not have to come from a database or anywhere particular.

You don't have to use response.write, or subcontrols. (server controls aren't valid inside of a repeater, anyway). You might try replacing

<%=Eval("...

with

<%#Eval("...

I'm unsure of the difference, but the second form is used in most examples.


You can always try the follow:

 <%# DataBinder.Eval(Container.DataItem, "FullName") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消