如何从动态填充的FormView控件更新Web窗体中的页面标题



FormViewTitle如果ListView或FormView控件(ASP.NET)中有一个标记元素,它将根据您检索的数据而更改,您将如何更新页面标题。例如一本书的标题。控件将在生成页面后检索标题。我的问题是,有没有一种方法可以在页面呈现后更新页面标题,或者以某种方式将这些信息传递给页面。LabelTitle(ID)值的标题?

请帮忙!。

这是我的代码简化

    <asp:FormView ID="FormViewTitle" 
            Runat="server" 
            DataSourceID="XmlDataSourceTitle"
            AllowPaging="False">
         <ItemTemplate>                        
             <asp:Label ID="LabelTitle" runat="server" Text="">
                <%#Eval("name") %>
              </asp:Label>
         </ItemTemplate>
      </asp:FormView>
     <asp:XmlDataSource ID="XmlDataSourceTitle" 
               DataFile="BooksTitles.xml"
               TransformFile ="TransformRSS.xslt">                 
     </asp:XmlDataSource>

到目前为止,我已经这样做了(在后面的代码中)来访问FormView控件,但不起作用:

if (FormViewTitle.CurrentMode == FormViewMode.ReadOnly)        
{
    Label pt = (Label)FormViewTitle.FindControl("LabelTitle");
    this.Page.Title = pt.Text;
}

在代码Behind Page类中的更改事件中,您可以尝试以下操作:

Page.Title = "Change Book Title";

好的,这里有一个基本的例子来完成你想要的,我创建了一个简单的listView,它绑定到Book对象的列表,我为ItemTemplate中的每个项目创建了一个子按钮,我将书籍的标题作为命令名发送,在后面的代码中使用这个标题,我更改页面的标题,试试看。ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestListView.aspx.cs" Inherits="TestListView" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Book Test</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ListView ID="ListView1" runat="server">
            <ItemTemplate>
                <div>
                    <asp:Label ID="lblTitle" Text='<%# Eval("Title")%>' runat="server" />
                    <asp:Button ID="btnChange" runat="server" CommandName='<%# Eval("Title")%>' Text="Select" OnCommand="btnChange_Command" />
                </div>
            </ItemTemplate>
        </asp:ListView>
    </form>
</body>
</html>

代码隐藏

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
public partial class TestListView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs args)
    {
        if (!IsPostBack)
        {
            var books = new List<Book>
            {
                new Book {Title = "Book 1"},
                new Book {Title = "Book 2"},
                new Book {Title = "Book 3"}
            };
            ListView1.DataSource = books;
            ListView1.DataBind();
        }
    }
    protected void btnChange_Command(object sender, CommandEventArgs e)
    {
        Page.Title = e.CommandName;
    }
}
public class Book
{
    public string Title { get; set; }
}

最新更新