如何使用eval在codebehind设置Page.Title [英] How to use Eval in codebehind to set Page.Title

查看:195
本文介绍了如何使用eval在codebehind设置Page.Title的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到ListView控件一个SqlDataSource,但我想将绑定记录的部分到HTML TITLE属性。下面是我想改变,因此它可以使用eval根据数据内容,构建一个动态的TITLE我的codebehind文件:

 公共部分类zShowAd
继承System.Web.UI.Page    保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
        Me.Page.Title =动态的ASPX页面设置
        如何在这里使用eval来代替上述不变的?
    结束小组
末级

下面是对应的.aspx文件:

 <%@页面语言=VBAutoEventWireup =假的MasterPageFile =〜/ zSEO.master
  codeBehind =zShowAd.aspx.vb继承=Zipeee.zShowAd%GT;
< ASP:内容ID =内容1ContentPlaceHolderID =ContentPlaceHolder1=服务器>
< D​​IV>
  < ASP:ListView控件ID =ShowAd=服务器的DataSourceID =aPosting>
    <&LayoutTemplate模板GT;
      < ASP:占位符=服务器ID =itemPlaceholder>< / ASP:占位符>
    < / LayoutTemplate模板>
   <&ItemTemplate中GT;
   < D​​IV>
    < D​​IV ID =包装>
        < D​​IV ID =头>< / DIV>
        < D​​IV ID =主>
            < D​​IV ID =导航>即AdID:LT;%#的eval(即AdID)%>< / DIV>
            < D​​IV ID =额外>价格:其中,%#的eval(价格)%>< / DIV>
            < D​​IV ID =内容> <%#的eval(AdDesc)%>< / DIV>
        < / DIV>
        < D​​IV ID =页脚>< / DIV>
    < / DIV>
   < / DIV>
  < / ItemTemplate中>
 < / ASP:的ListView> < ASP:SqlDataSource的=服务器ID =aPosting
        的ConnectionString =下;%$的ConnectionStrings:ZIPeeeConnectionString2%>中
        的SelectCommand =spGetAdByIDSelectCommandType =StoredProcedure的>
        < SelectParameters>
            < ASP:QueryStringParameter名称=的AdIDQueryStringField =类型=字符串/>
        < / SelectParameters>
    < / ASP:SqlDataSource的>
< / DIV>
< / ASP:内容>


解决方案

您可以通过将地方下面你的ListView的ItemTemplate模板里面调用页面的code的方法(子)的后面:

 <%#SetPageTitle(EVAL(SomeProperty))%GT;

然后在你的背后code(抱歉,这是在C#):​​

 保护无效SetPageTitle(对象名称)
{
  this.Title = title.ToString();
}

另外,您也可以通过完整的数据项,而不是仅仅一个属性:

 <%#SetPageTitle(的Container.DataItem)%GT;


更新(回答你的评论):

<%#......%> 是一个所谓的数据绑定前pression。它只能一个数据绑定控件(在您的示例中的ListView)内,它总是与当前记录(一般为您显示像ListView中数据绑定控件的多个记录)。

工作

所以,当你使用<%#的eval(价格)%> ,您显示当前记录的价格列的值。如果您的查询,将返回多个记录,那么这将是为每个记录执行,并设置页面标题时(如上图所示),该页面的标题是从最后记录的值。

在另一方面<%= ...%> ,只是一个普通的服务器端code段(不知道是否有是它一个特定的名字),它不知道数据绑定上下文(例如这是当前记录)。

请详细信息请参阅以下问题:<一href=\"http://stackoverflow.com/questions/115159/when-should-i-use-and-in-asp-net-controls\">http://stackoverflow.com/questions/115159/when-should-i-use-and-in-asp-net-controls

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: http://stackoverflow.com/questions/115159/when-should-i-use-and-in-asp-net-controls

这篇关于如何使用eval在codebehind设置Page.Title的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆