English 中文(简体)
How to use Eval in codebehind to set Page.Title
原标题:

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>
最佳回答

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: When should I use # and = in ASP.NET controls?

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签