views:

617

answers:

1

I have a SQLDataSource that is bound to a ListView control but I want to place parts of the bound record into the HTML TITLE attribute. Here is my codebehind file that I want to change so it can use Eval to construct a dynamic TITLE based on the data content:

Public Partial Class zShowAd
Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Page.Title = " Dynamically set in ASPX page" 
        'how to use Eval here instead of the above constant ??    
    End Sub
End Class

Here is the corresponding .aspx file:

<%@ Page Language="vb" AutoEventWireup="false" MasterPageFile="~/zSEO.master" 
  CodeBehind="zShowAd.aspx.vb" Inherits="Zipeee.zShowAd" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
  <asp:ListView ID="ShowAd" runat="server" DataSourceID="aPosting">
    <LayoutTemplate>
      <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
    </LayoutTemplate>
   <ItemTemplate>
   <div>
    <div id="wrapper"> 
        <div id="header"></div> 
        <div id="main"> 
            <div id="nav">    AdID: <%#Eval("AdID")%></div> 
            <div id="extras">Price: <%#Eval("Price")%></div> 
            <div id="content">      <%#Eval("AdDesc")%></div> 
        </div> 
        <div id="footer"></div> 
    </div>
   </div>
  </ItemTemplate>
 </asp:ListView>

 <asp:sqldatasource runat="server" id="aPosting"
        ConnectionString="<%$ ConnectionStrings:ZIPeeeConnectionString2 %>" 
        SelectCommand="spGetAdByID" SelectCommandType="StoredProcedure">
        <SelectParameters>
            <asp:QueryStringParameter Name="AdId" QueryStringField="a" Type="String" />
        </SelectParameters>
    </asp:sqldatasource>
</div>
</asp:Content>
+1  A: 

You can call a method (Sub) of the page's code behind by putting the following somewhere inside the ItemTemplate of your ListView:

<%# SetPageTitle(Eval("SomeProperty")) %> 

Then in your code behind (sorry it's in C#):

protected void SetPageTitle(object title)
{
  this.Title = title.ToString();
}

Alternatively, you can also pass the complete data item, instead of just one property:

<%# SetPageTitle(Container.DataItem) %> 

Update (to answer your comment):

<%# ... %> is a so-called data-binding expression. It only works inside of a data-bound control (the ListView in your example) and it always works with the current record (typically you display more than one record in a data-bound control like the ListView).

So when you use <%# Eval("Price") %>, you are displaying the value of the current record's "Price" column. If your query, would return more than one record, then this would be executed for each record, and when setting the page title (as shown above), the page's title would be the value from the last record.

On the other hand <%= ... %>, is just a normal server-side code snippet (don't know if there is a specific name for it), which does not know about the data-binding context (e.g. which is the current record).

Please see the following question for more details: http://stackoverflow.com/questions/115159/when-should-i-use-and-in-asp-net-controls

M4N
Thanks very much. I've searched quite a lot and never found a good explanation of why this syntax: <%# SetPageTitle(Eval("SomeProperty")) %>versus this syntax: <% SetPageTitle(Eval("SomeProperty")) %>Coming from a classic ASP background, the 2nd form would seem OK. Is the presence of the # (is that called the binding symbol?) needed because the statement contains Eval ?
John Galt
@John: I tried to explain the difference in the updated answer.
M4N
What a great explanation! Thank you.
John Galt