2016-05-04 6 views
0

私は、リフレクションを使用してページからmasterpageパブリックメソッドを呼び出しますが、メソッドが呼び出されると、masterpageのコントロールはnullを返します。C#Reflection:asp.netコントロールは、マスターページでnullです

コードマスターページファイルに:

public void Add_PageTitle(string title, string titleIcon = null, string titleSmallText = null, string title2 = null) 
{ 

    if (!string.IsNullOrEmpty(titleIcon)) 
     this.litPageTitleIcon.Text = "<i class=\"" + titleIcon + " position-left\"></i>"; 

    if (!string.IsNullOrEmpty(title)) 
     this.litPageTitleText.Text = " <span class=\"text-semibold\">" + title + "</span>" + (!string.IsNullOrEmpty(title2) ? " - " + title2 : ""); 

    if (!string.IsNullOrEmpty(titleSmallText)) 
     this.litPageTitleSmallText.Text = " <small class=\"display-block\">" + titleSmallText + "</small> "; 
} 

コードページ・ファイル内:

Type masterType = this.Page.Master.GetType(); 
object classInstance = Activator.CreateInstance(masterType, null); 
MethodInfo miAddPageTitle = classInstance.GetType().GetMethod("Add_PageTitle", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); 
MethodInfo miHeadingElements = classInstance.GetType().GetMethod("Add_Heading_Elements", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); 

if (this.WorkingServiceRequest != null) 
    miAddPageTitle.Invoke(classInstance, new object[] { this.WorkingServiceRequest.QuestionPack.QuestionPackName, ((this.WorkingServiceRequest.ServiceType != null) ? this.WorkingServiceRequest.ServiceType.IconClass : string.Empty), ((this.WorkingServiceRequest.ServiceType != null) ? this.WorkingServiceRequest.ServiceType.Description : this.WorkingServiceRequest.QuestionPack.QuestionPackName), this.WorkingServiceRequest.Name }); 
else 
    miAddPageTitle.Invoke(classInstance, new object[] { questionPack.Name, questionPack.IconClass, questionPack.Description, string.Empty }); 

私は、例えばマスターページに微細な方法が、コントロール を呼び出すことができます

<asp:Literal ID="litPageTitleIcon" runat="server"></asp:Literal> 
<asp:Literal ID="litPageTitleText" runat="server"></asp:Literal> 
<asp:Literal ID="litPageTitleSmallText" runat="server"></asp:Literal> 

マスターページの "litPageTitleIcon、litPageTitleText" 例外ofcauseを引き起こす、nullを返します。

私は間違っていますか?

答えて

0

あなたはcreate instanceで新しいマスターページを作成しています。そのため、使用可能なコントロールはありません。

は、私は ...あなたは直前demostratedこのアプローチを使用していた。しかし、私は4つの異なるmasterpagesのように持っている...だから私は、私があれば取​​り除くためのより良い方法をやっていたと思った、[OK]を

var x = (MyMasterPageType)this.Page.Master; 
x.Add_PageTitle(questionPack.Name, questionPack.IconClass, questionPack.Description, string.Empty) 
+0

を試してみてください... elseステートメント... as ... masterpagesには同じメソッドがあります。 –

+0

インターフェイスの作成、masterpagesのインターフェイスの実装、インターフェイスへのキャスト、ダイナミックの使用を行います。 –

+0

インターフェイスを実装する上記のマスターページのasp.netコントロールが表示されますか? 私はこれを取り除きたいです if (this.Page.Master is PageTemplate){ var master = (PageTemplate)this.Page.Master; } else if (this.Page.Master is PageTemplate_v2) { PageTemplate_v2 masterv2 = (PageTemplate_v2)this.Page.Master; }

関連する問題