代码之家  ›  专栏  ›  技术社区  ›  Victor

在内容页上定位标签

  •  0
  • Victor  · 技术社区  · 14 年前

     Page p = this.Page;
     //this line causes application to unload with no exception
     ContentPlaceHolder cp = (ContentPlaceHolder)p.Master.FindControl("Content2");
     Label label = (Label)cp.FindControl("SomeLabel");
    

    它只是卸载自己没有例外。为什么会这样?

    1 回复  |  直到 14 年前
        1
  •  0
  •   CAbbott    14 年前

    我能让它工作,这样它就能找到一个 Label ContentPlaceHolder

    站点1.主节点:

    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        </asp:ContentPlaceHolder>
    </div>
    </form>
    

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" MasterPageFile="~/Site1.Master" %>
    <%@ Register Src="WebUserControl1.ascx" TagName="stuff" TagPrefix="uc" %>
    
    <asp:Content ID="indexContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:Label ID="Label1" Text="Label" runat="server" />
        <uc:stuff ID="test" runat="server" />
    </asp:Content>
    

    WebUserControl1.ascx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.Master != null)
        {
            Control c = Page.Master.FindControl("ContentPlaceHolder1");
            if (c != null)
            {
                Label l = (Label)c.FindControl("Label1");
            }
        }
    }