簡體   English   中英

Null 參考文字控制

[英]Null Reference Literal Control

我正在嘗試將文本傳遞給文字,parent.master 具有 ContentTemplateID,而 Child.master 具有 contentID,其中之一是表單的文字

<asp:Literal runat="server" ID="myLiteral"></Literal>

我正在嘗試從這樣的 UserControl.cs 文件中傳遞它

 gooleTrackin = track.GetTrack(track.OrderType.YAHOO); 
 Literal mpLiteral = (Literal)this.Parent.Page.Master.FindControl("myLiteral");
mpLiteral.Text = gooleTrackin.ToString(); //nullReference here 

但它在最后一行給了我 NulLReference 。

順便說一句,我無法訪問母版頁的 .cs 文件,它必須通過 UserControl 來完成。

謝謝

附加代碼(位於 CHILD.MASTER 中)

<%@ Master Language="C#" AutoEventWireup="true" MasterPageFile="../common/child.master" %>
<%@ Register Src="UserControl.ascx" TagName="UserControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
   <div class="inner"><uc1:UserControl ID="theRceipt" runat="server" Visible="true"/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BottomContentTemplate" Runat="Server">
<div style="margin-bottom:30px;">
   <asp:Literal ID="myLiteral" runat="server"></asp:Literal>
<a href="~/" id="23" runat="server" class="BackLink">Back to Home Page</a>
</div>
</asp:Content>

使用母版頁時,母版頁中的控件會合並到頁面的控件層次結構中,因此在查找控件時可能會出現問題。 我建議您嘗試以下操作,看看它是否有效:

Literal mpLiteral = (Literal)this.Page.FindControl("myLiteral");

或者

Literal mpLiteral = (Literal)this.Parent.FindControl("myLiteral");

否則,您可能不得不嘗試遞歸查找 - 請參閱http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

但是,我寧願推薦一種替代方法 - 假設您已經在 Child.Master 中定義了文字控件,並且Child是 class 后面的代碼名稱,您可以在其中添加一個輔助方法,例如

public void UpdateMyLiteral(string text)
{
   myLiteral.Text = text;
}

並在用戶控制代碼中,調用輔助方法,例如

((Child)this.Page.Master).UpdateMyLiteral("xyz");

請注意,我們將 master 轉換為其代碼隱藏 class。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM