How does data binding work in a templated item

satya - Tuesday, May 08, 2007 11:27:52 PM

how to use template fields in a grid view article

how to use template fields in a grid view article

satya - Wednesday, May 09, 2007 9:35:31 PM

data binding samples

data binding samples

satya - Wednesday, May 09, 2007 9:36:56 PM

The three articles on data binding

part1
part2
part3

satya - Wednesday, May 09, 2007 9:58:40 PM

Study the rules for databinder.eval method

Especially in relation to naming containers and data items etc.

satya - Wednesday, May 09, 2007 10:07:16 PM

Nice article that delves into the container.dataitem

Nice article that delves into the container.dataitem

satya - Tuesday, May 15, 2007 2:28:46 PM

another sample using a repeater


<form id="Form1" method="post" runat="server">
  <table>         
    <asp:Repeater id="Repeater1" runat="server">
      <ItemTemplate>
        <tr>
          <td><%# DataBinder.Eval(Container.DataItem, "Name") %></td>
          <td><%# DataBinder.Eval(Container.DataItem, "HexValue") %></td>
        </tr>
      </ItemTemplate>
    </asp:Repeater>
  </table>
</form>

satya - Tuesday, May 15, 2007 2:31:51 PM

repeated elements can be bound to a number collection like objects

which include

DataTable
DataView
SqlDataReader

satya - Tuesday, May 15, 2007 2:32:09 PM

In each case the Container.dataitem will have a different type

In each case the Container.dataitem will have a different type

satya - Tuesday, May 15, 2007 2:32:51 PM

This will also include custom objects supporting an ICollection

In this case the dataitem will point to the custom object.

satya - Tuesday, May 15, 2007 2:39:13 PM

following is valid if your repeater is bound to a collection of "Color" objects


<asp:Repeater id="Repeater1" runat="server">
  <ItemTemplate>
    <tr bgcolor="<%# ((Color)Container.DataItem).HexValue %>">	
      <td><%# ((Color)Container.DataItem).Name %></td>
      <td><%# ((Color)Container.DataItem).HexValue %></td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

satya - Tuesday, May 15, 2007 2:39:36 PM

what is special about "Container" though

what is special about "Container" though

satya - Tuesday, May 15, 2007 2:45:05 PM

using a method again


<asp:Repeater id="Repeater1" runat="server">
  <ItemTemplate>
    <tr bgcolor="<%# ((Color)Container.DataItem).HexValue %>">
      <td><%# GetColorName(Container.DataItem)  %></td>
      <td><%# ((Color)Container.DataItem).HexValue %></td>
    </tr>
  </ItemTemplate>
</asp:Repeater>

satya - Tuesday, May 15, 2007 2:46:34 PM

The context for this method...

The code snippets gets executed in the body of a generated function that is inside the code behind class. So any method defined in that the class context could be used for this. This method needs to be protected.

satya - Tuesday, May 15, 2007 4:22:21 PM

take a look at this link

take a look at this link

satya - Tuesday, May 15, 2007 10:09:29 PM

another example


<asp:GridView ID="wellGrid" AutoGenerateColumns="False" runat="server" Width="70%">
   <Columns>
      <asp:HyperLinkField 
         DataTextField="well_nm" 
         HeaderText="well name" 
         DataNavigateUrlFields="well_loc_id" 
         DataNavigateUrlFormatString="/test-website/well.aspx?wellid={0}" 
         />
         <asp:TemplateField HeaderText="test">
            <ItemTemplate>
               <asp:HyperLink ID="WellNameHyperLinkItemLabel" 
               Text='<%# Eval("well_nm") %>'
               NavigateUrl='<%# String.Format("/test-website/well.aspx?wellid={0}"
                           ,Eval("well_loc_id"))%>'
               runat="server" />
            </ItemTemplate>
         </asp:TemplateField>
   </Columns>
</asp:GridView>

satya - Tuesday, May 15, 2007 10:16:40 PM

The Eval function (perhaps!!)

Most likely in this release of asp.net this function is added to the generated code behind class with the following code inside it


Eval(colname)
{
 return DataBinder.Eval(Container.DataItem,colname);
}

It is not clear though how this class level function will get a reference to the row specific container.dataitem

satya - Tuesday, May 15, 2007 10:30:19 PM

Can I put an open text in an itemtemplate?

Can I put an open text in an itemtemplate?

satya - Friday, May 18, 2007 5:04:33 PM

another example


<asp:GridView ID="wellGrid" AutoGenerateColumns="False" runat="server" Width="70%">
   <Columns>
         <asp:TemplateField HeaderText="wellname">
            <ItemTemplate>
               <asp:HyperLink ID="WellNameHyperLinkItemLabel" 
               Text='<%# Eval("well_nm") %>'
               NavigateUrl='<%# String.Format("/test-website/well.aspx?wellid={0}"
			                                     ,Eval("well_loc_id"))%>'
               runat="server" />
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="property number">
            <ItemTemplate>
            <asp:Label Text='<%#Eval("PROPERTY_NUM") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Team Name">
            <ItemTemplate>
            <asp:Label ID="Label1" Text='<%#Eval("TEAM_NM") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Area">
            <ItemTemplate>
            <asp:Label ID="Label1" Text='<%#Eval("AREA") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Section">
            <ItemTemplate>
            <asp:Label ID="Label1" Text='<%#Eval("SECTION") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Town Ship">
            <ItemTemplate>
            <asp:Label ID="Label1" Text='<%#Eval("TOWNSHIP") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="County">
            <ItemTemplate>
            <asp:Label ID="Label1" Text='<%#Eval("COUNTY") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Lat">
            <ItemTemplate>
            <asp:Label ID="Label1" Text='<%# Eval("LATITUDE") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="LONG">
            <ItemTemplate>
            <asp:Label ID="Label1" Text='<%# Eval("LONGITUDE") %>' runat="server"/>
            </ItemTemplate>
         </asp:TemplateField>
         
   </Columns>

</asp:GridView>

satya - Friday, May 18, 2007 5:12:58 PM

example using a function


protected String getValue(Object item, String colname)
    {
        try
        {
            DataRowView row = (DataRowView)item;
            String value = (String)row[colname];
            return value;
        }
        catch (Exception x)
        {
            return "No data";
        }
    }