从内容页面获取并投射Masterpage UserControl,以访问特定的UC属性 [英] Get and cast Masterpage UserControl from Content Page to access specific UC Property
问题描述
我有一个MasterPage(MyBoxx.Master)引用了2个用户控件:
I have a MasterPage (MyBoxx.Master) referencing 2 usercontrols :
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MyBoxx.master.cs" Inherits="MyBoxxMaster" %>
<%@ Register TagPrefix="uc1" TagName="Header" Src="Header.ascx" %>
<%@ Register TagPrefix="uc1" TagName="Footer" Src="Footer.ascx" %>
我的用户控件页眉"除其他外还包含一个搜索框.我想在访问某些页面时隐藏此搜索框.因此,我在用户控件中添加了一个布尔属性,并在呈现用户控件时确定是否显示搜索框时使用此属性:
My user control "Header" contains among other things a searchbox. I want to hide this searchbox when visiting some pages. Therefore I added a boolean property to my user control and use this property when rendering the usercontrol to determinate whether to display the search box or not :
public partial class uxHeader : System.Web.UI.UserControl
{
bool _showSearch = true;
public bool ShowSearch
{
get { return _showSearch; }
set { _showSearch = value; }
}
[...]
protected void Page_Load(object sender, EventArgs e)
{
[...]
searchBox.Visible = _showSearch;
}
}
然后我尝试从内容页面访问此"ShowSearch"属性:
I then try to access this "ShowSearch" Property from the content page :
((uxHeader)Page.Master.FindControl("Header1")).ShowSearch = false;
问题是尝试编译时出现以下错误:
Problem is I get the following error when trying to compile :
Error 15 The type or namespace name 'uxHeader' could not be found (are you missing a using directive or an assembly reference?)
事情是肯定的,因为它可以在以前发布的生产版本上运行,所以我可以在某个时候使它工作并进行编译.但是现在我正在对同一站点中的其他内容进行更改,并且无法编译了.
The thing is I'm sure I got it to work and compile at some point as it works on the previously released production version. But now I'm doing a change to something else in the same site, and can't compile anymore.
在SO的各种文章中,我尝试将以下行添加到内容页面aspx中:
From various post on SO, I tried adding the following lines to my content page aspx :
<%@ MasterType VirtualPath="~/MyBoxx.master"%>
<%@ Reference VirtualPath="~/MyBoxx.master" %>
没有成功!我也看到了有关页面生命周期的一些答案,但这不是问题,因为我在编译时遇到错误,而不是执行时遇到错误.
Without any success ! I saw also some answers about the page Lifecycle, but this can't be the problem here as I'm getting an error on compilation, not a bug upon execution.
如果有人对我如何永久解决此问题有任何建议,我将不胜感激.
If anyone has any advice on how I can fix this for good, I would grandly appreciate.
谢谢!
推荐答案
好吧,我找到了几个可行的解决方案...而且我想我知道它较早/为什么工作
Well, I found several working solutions... and I think I understood how/why it worked earlier
1)似乎编译可以在其中发挥作用.如果我注释了这一行,编译了站点,然后尝试再次添加该行,则在VS中"uxHeader"类型为"available",并且我可以重新编译该站点,而无需注释该行...
1) it seems that compilation has a role to play in this. If I comment the line, compile the site, and then try to add the line again, the type uxHeader is "available" in VS and I can compile the site back again with the line uncommented...
2)因为第一个解决方案显然不是长期解决方案,所以我发现在内容页面aspx中引用用户控件(当然实际上并没有实际使用它)可以解决问题:
2) As first solution is obviously not a long-term solution, I found that referencing the user-control (without actually using it of course) in the content page aspx would do the trick :
<%@ Register TagPrefix="uc1" TagName="Header" Src="Header.ascx" %>
3)我也尝试过这个,我发现它最干净... 在母版页中,公开一个公共属性:
3) I also tried this one, which I find the cleanest... In the master page, expose a public property :
public uxHeader PageHeader
{
get
{
return Header1;//Header1 is the id of the userControl dropped in masterpage
}
}
然后在内容页面aspx中,输入:
In the content page aspx, I then put :
<%@ MasterType VirtualPath="~/DBoxx.master"%>
然后,仍在内容页面中,但在代码背后,并且在对该站点进行编译之后,我可以使用:
then, still in the content page, but in the codebehind, and after a compilation of the site, I can use :
this.Master.PageHeader.ShowSearch = false;
希望这将对以后寻求有关此主题的帮助的人们有所帮助.我看到这是一个递归问题
Hope this will help the ones searching for help on the subject in the future. I see this is a recurent question
这篇关于从内容页面获取并投射Masterpage UserControl,以访问特定的UC属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!