我有一个绑定到 ListView 控件的 SQLDataSource,但我想将部分绑定记录放入 HTML TITLE 属性中。这是我想要更改的代码隐藏文件,以便它可以使用 Eval 根据数据内容构建动态 TITLE:
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
这是相应的 .aspx 文件:
<%@ 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>
您可以通过将以下内容放在 ListView 的 ItemTemplate 内的某个位置来调用页面代码的方法 (Sub):
<%# SetPageTitle(Eval("SomeProperty")) %>
然后在你的代码后面(抱歉,它是用 C# 编写的):
protected void SetPageTitle(object title)
{
this.Title = title.ToString();
}
或者,您也可以传递完整的数据项,而不是仅传递一个属性:
<%# SetPageTitle(Container.DataItem) %>
更新(回答您的评论):
<%# ... %>
是所谓的数据绑定表达式。它仅在数据绑定控件(示例中的 ListView)内部工作,并且始终与当前记录一起工作(通常您在数据绑定控件(如 ListView)中显示多个记录)。
所以当你使用<%# Eval("Price") %>
,您正在显示当前记录的“价格”列的值。如果您的查询将返回多个记录,则将为每条记录执行此操作,并且在设置页面标题(如上所示)时,页面的标题将是最后一条记录的值。
另一方面<%= ... %>
,只是一个普通的服务器端代码片段(不知道是否有特定的名称),它不知道数据绑定上下文(例如哪一个是当前记录)。
请参阅以下问题了解更多详细信息:什么时候应该在 ASP.NET 控件中使用 # 和 =? https://stackoverflow.com/questions/115159/when-should-i-use-and-in-asp-net-controls
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)