<%# DataBinder.Eval(Container.DataItem, "FirstName") %> in a fieldof a web server control ?

Discussion in 'ASP .Net' started by abargaddon, Jan 28, 2008.

  1. abargaddon

    abargaddon Guest

    Hello everybody,
    I need to use bound variables in a field of a web server control which
    is inside a template. Many sources in the Web say it works, but for me
    it does not, so I have made a test page :

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">

    public class Bidon
    {
    public string OwnerId { get { return "111"; } }
    public string FirstName { get { return "Alberto"; } }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)
    {
    System.Collections.Generic.List<Bidon> listBidon =
    new System.Collections.Generic.List<Bidon>();
    listBidon.Add(new Bidon());
    listBidon.Add(new Bidon());
    RepeaterCollection.DataSource = listBidon;
    RepeaterCollection.DataBind();
    }
    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Ptitle</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <h2>
    Test Repeater and templates</h2>
    <asp:Repeater ID="RepeaterCollection" runat="server">
    <ItemTemplate>
    ItemTemplate : <%# DataBinder.Eval(Container.DataItem,
    "OwnerId") %>
    - <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
    <asp:HyperLink ID="HyperLink2" runat="server"
    Text='eval (<%# DataBinder.Eval(Container.DataItem,
    "OwnerId") %>)' />
    <asp:Label ID="Label2" runat="server"
    Text='eval (<%# DataBinder.Eval(Container.DataItem,
    "FirstName") %>)' />
    <br />
    </ItemTemplate>
    </asp:Repeater>
    </form>
    </body>
    </html>


    The result I obtain is below : Eval() does not work inside the asp:label
    or the asp:hyperlink Text fields :

    Test Repeater and templates
    ItemTemplate : 111 - Alberto eval () eval ()
    ItemTemplate : 111 - Alberto eval () eval ()

    Can anyone help me please ?

    Sincerely,
    Abargaddon
     
    abargaddon, Jan 28, 2008
    #1
    1. Advertising

  2. RE: <%# DataBinder.Eval(Container.DataItem, "FirstName") %> in a field

    Howdy,

    In AP.NET 2.0 you can use Eval instead of DataBinder.Eval. In addition
    remember data bound expressions for server controls must be defined in entire
    attribute:
    <asp:Label runat="server" Text='<%# "prefix " + Eval("whatever") %>'/>
    NOT
    <asp:Label runat="server" Text='prefix<%# Eval("whatever") %>'/>

    Anyway:
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Ptitle</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <h2>
    Test Repeater and templates</h2>
    <asp:Repeater ID="RepeaterCollection" runat="server">
    <ItemTemplate>
    ItemTemplate : <%# DataBinder.Eval(Container.DataItem,
    "OwnerId") %>
    - <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
    <asp:HyperLink ID="HyperLink2" runat="server" Text='<%#
    DataBinder.Eval(Container.DataItem, "OwnerId") %>' />
    <asp:Label ID="Label2" runat="server" Text='<%#
    String.Format(DataBinder.Eval(Container.DataItem, "FirstName"), "Surrounding
    {0} text" %>' />
    <asp:Label ID="Label3" runat="server" Text='<%# Eval("FirstName") %>'
    <br />
    </ItemTemplate>
    </asp:Repeater>
    </form>
    </body>
    </html>

    Hope it helps
    --
    Milosz


    "abargaddon" wrote:

    > Hello everybody,
    > I need to use bound variables in a field of a web server control which
    > is inside a template. Many sources in the Web say it works, but for me
    > it does not, so I have made a test page :
    >
    > <%@ Page Language="C#" %>
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    > <script runat="server">
    >
    > public class Bidon
    > {
    > public string OwnerId { get { return "111"; } }
    > public string FirstName { get { return "Alberto"; } }
    > }
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > if (!this.IsPostBack)
    > {
    > System.Collections.Generic.List<Bidon> listBidon =
    > new System.Collections.Generic.List<Bidon>();
    > listBidon.Add(new Bidon());
    > listBidon.Add(new Bidon());
    > RepeaterCollection.DataSource = listBidon;
    > RepeaterCollection.DataBind();
    > }
    > }
    >
    > </script>
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title>Ptitle</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <h2>
    > Test Repeater and templates</h2>
    > <asp:Repeater ID="RepeaterCollection" runat="server">
    > <ItemTemplate>
    > ItemTemplate : <%# DataBinder.Eval(Container.DataItem,
    > "OwnerId") %>
    > - <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
    > <asp:HyperLink ID="HyperLink2" runat="server"
    > Text='eval (<%# DataBinder.Eval(Container.DataItem,
    > "OwnerId") %>)' />
    > <asp:Label ID="Label2" runat="server"
    > Text='eval (<%# DataBinder.Eval(Container.DataItem,
    > "FirstName") %>)' />
    > <br />
    > </ItemTemplate>
    > </asp:Repeater>
    > </form>
    > </body>
    > </html>
    >
    >
    > The result I obtain is below : Eval() does not work inside the asp:label
    > or the asp:hyperlink Text fields :
    >
    > Test Repeater and templates
    > ItemTemplate : 111 - Alberto eval () eval ()
    > ItemTemplate : 111 - Alberto eval () eval ()
    >
    > Can anyone help me please ?
    >
    > Sincerely,
    > Abargaddon
    >
     
    Milosz Skalecki [MCAD], Jan 29, 2008
    #2
    1. Advertising

  3. abargaddon

    abargaddon Guest

    Re: <%# DataBinder.Eval(Container.DataItem, "FirstName") %> in afield

    Hi Milosz,

    Thank you a lot, this was indeed my problem.
    However, I did not find it mentioned in the MSDN site, although it is an
    important detail !

    Thank you again, and have a nice sunday.
    Abargaddon



    Milosz Skalecki [MCAD] a écrit :
    > Howdy,
    >
    > In AP.NET 2.0 you can use Eval instead of DataBinder.Eval. In addition
    > remember data bound expressions for server controls must be defined in entire
    > attribute:
    > <asp:Label runat="server" Text='<%# "prefix " + Eval("whatever") %>'/>
    > NOT
    > <asp:Label runat="server" Text='prefix<%# Eval("whatever") %>'/>
    >
    > Anyway:
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title>Ptitle</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <h2>
    > Test Repeater and templates</h2>
    > <asp:Repeater ID="RepeaterCollection" runat="server">
    > <ItemTemplate>
    > ItemTemplate : <%# DataBinder.Eval(Container.DataItem,
    > "OwnerId") %>
    > - <%# DataBinder.Eval(Container.DataItem, "FirstName") %>
    > <asp:HyperLink ID="HyperLink2" runat="server" Text='<%#
    > DataBinder.Eval(Container.DataItem, "OwnerId") %>' />
    > <asp:Label ID="Label2" runat="server" Text='<%#
    > String.Format(DataBinder.Eval(Container.DataItem, "FirstName"), "Surrounding
    > {0} text" %>' />
    > <asp:Label ID="Label3" runat="server" Text='<%# Eval("FirstName") %>'
    > <br />
    > </ItemTemplate>
    > </asp:Repeater>
    > </form>
    > </body>
    > </html>
    >
    > Hope it helps
     
    abargaddon, Feb 2, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lauchlan M
    Replies:
    1
    Views:
    7,255
    Lauchlan M
    Oct 15, 2003
  2. Tom Lee
    Replies:
    2
    Views:
    844
    Chris Carter
    Dec 9, 2003
  3. bg
    Replies:
    5
    Views:
    6,394
    Scott Allen
    Apr 16, 2004
  4. Eric Newton
    Replies:
    3
    Views:
    9,429
    Brock Allen
    Apr 4, 2005
  5. Joe Rattz
    Replies:
    2
    Views:
    608
    Robert Koritnik
    Sep 10, 2004
Loading...

Share This Page