从内容页面获取并转换 Masterpage UserControl 以访问特定的 UC 属性

Get and cast Masterpage UserControl from Content Page to access specific UC Property(从内容页面获取并转换 Masterpage UserControl 以访问特定的 UC 属性)

本文介绍了从内容页面获取并转换 Masterpage UserControl 以访问特定的 UC 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个引用 2 个用户控件的 MasterPage (MyBoxx.Master):

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) 编译似乎在其中发挥了作用.如果我注释该行,编译站点,然后尝试再次添加该行,则 uxHeader 类型在 VS 中为可用",我可以在未注释该行的情况下再次编译该站点...

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 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从内容页面获取并转换 Masterpage UserControl 以访问特定的 UC 属性

基础教程推荐