0
votes

If I have the following (generic) ASP code:

<asp:ObjectDataSource runat="server" ID="myODS">...</asp:ObjectDataSource>

<asp:GridView runat="server" ID="myGV" DataSourceID="myODS">
   <Columns>

      <asp:TemplateField>
         <ContentTemplate>
            <asp:Label runat="server" ID="myLabel" Text='<%# [What goes here?] %>' />
         </ContentTemplate>
      </asp:TemplateField>

      <asp:BoundField ... />
      <%-- Other columns... --%>

   </Columns>
</asp:GridView>

What can I call within an ASP expression, inside my Template Field, that will allow me to have access to my ODS results? Basically, inside the expression, I want to do something like row("ID") to get a specific value out of a column from my ODS.

1

1 Answers

1
votes

There are a number of different syntaxes you can use to specify a particular property. The simplest is to use the Eval method:

    <asp:Label runat="server" ID="myLabel" Text='<%# Eval("ID") %>' />

This is a short-hand syntax for the more verbose syntax using DataBinder.Eval static method, so you can use this more verbose syntax if you want:

    <asp:Label runat="server" ID="myLabel" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>' />

See here for the MSDN documentation on DataBinder.Eval

Edit: One thing I forgot to mention what that ASP.Net 2.0 and higher support the "Bind" method:

    <asp:Label runat="server" ID="myLabel" Text='<%# Bind("ID") %>' />

which nominally supports 2-way data binding. Thus if you use the Bind method, you don't have to manually screen-scrape your value from your inserted or deleted row, and the ASP.Net infrastructure will handle generating the new or modified object. However, in practice, there are a lot of caveats for using this. Your domain object must have a default constructor, must only be composed of simply properties, and I think there are a few more. In practice, this is so restrictive that I don't think it is a very useful thing to use. But I just wanted to point it out.