如何使 MasterPage 中的 UserControl 对所有子页面公开

发布于 2024-07-16 20:42:49 字数 2510 浏览 7 评论 0原文

我有一个 UserControl,已将其添加到 web.config

<add tagPrefix="BCF" src="~/controls/MyMessageBox.ascx" tagName="error"/>

并添加到我的母版页。

<BCF:error ID="BCError" runat="server" Visible="false" />

现在我需要能够从使用该母版页的所有子页面引用此控件及其公共属性。 我这样做是我的 BasePage OnLoad 事件

public UserControl BCError;
BCError = (UserControl)Master.FindControl("BCError");

问题是,虽然我可以在 .aspx 页面中执行此操作,

BCError.Visible = true;

但我无法引用我放入的任何控件属性? 例如 ShowError .. 如果我这样做,

BCError.ShowError = "Error Message";

我只会收到一条错误消息,指出

'System.Web.UI.UserControl' 不包含 'ShowInfo' 的定义,并且没有扩展方法 'ShowInfo'

你能指出吗我的方向是正确的!

这是用户控件的代码...我可以使用后面的母版页代码中的属性(如果我将控件直接放入其中,则可以在页面中使用),但不能在后面的子页面代码中使用它们? 它甚至没有显示智能感知中的属性或包装方法?

    using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class MyMessageBox : System.Web.UI.UserControl
{
    #region Properties
    public bool ShowCloseButton { get; set; }

    #endregion

    #region Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ShowCloseButton)
            CloseButton.Attributes.Add("onclick", "document.getElementById('" + MessageBox.ClientID + "').style.display = 'none'");
    }
    #endregion

    #region Wrapper methods
    public void ShowError(string message)
    {
        Show(MessageType.Error, message);
    }

    public void ShowInfo(string message)
    {
        Show(MessageType.Info, message);
    }

    public void ShowSuccess(string message)
    {
        Show(MessageType.Success, message);
    }

    public void ShowWarning(string message)
    {
        Show(MessageType.Warning, message);
    } 
    #endregion

    #region Show control
    public void Show(MessageType messageType, string message)
    {
        CloseButton.Visible = ShowCloseButton;
        litMessage.Text = message;

        MessageBox.CssClass = messageType.ToString().ToLower();
        this.Visible = true;
    } 
    #endregion

    #region Enum
    public enum MessageType
    {
        Error = 1,
        Info = 2,
        Success = 3,
        Warning = 4
    } 
    #endregion
}

I have a UserControl which I have added to my web.config

<add tagPrefix="BCF" src="~/controls/MyMessageBox.ascx" tagName="error"/>

and added to my master page

<BCF:error ID="BCError" runat="server" Visible="false" />

Now I need to be able to reference this control AND its public properties from all child pages that use that masterpage. I did this is my BasePage OnLoad event

public UserControl BCError;
BCError = (UserControl)Master.FindControl("BCError");

Problem is, although I can do this in the .aspx page

BCError.Visible = true;

I cannot reference any of the Controls properties I have put in? Such as ShowError .. If I do

BCError.ShowError = "Error Message";

I just get an error saying

'System.Web.UI.UserControl' does not contain a definition for 'ShowInfo' and no extension method 'ShowInfo'

Can you please point me in the right direction!

This is the code for the user control... I can use the properties in the masterpage code behind (And in a page if I put the control directly into it) but cannot use them in the child page code behind?? It doesn't even show the properties or wrapper methods in the intellisense?

    using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class MyMessageBox : System.Web.UI.UserControl
{
    #region Properties
    public bool ShowCloseButton { get; set; }

    #endregion

    #region Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ShowCloseButton)
            CloseButton.Attributes.Add("onclick", "document.getElementById('" + MessageBox.ClientID + "').style.display = 'none'");
    }
    #endregion

    #region Wrapper methods
    public void ShowError(string message)
    {
        Show(MessageType.Error, message);
    }

    public void ShowInfo(string message)
    {
        Show(MessageType.Info, message);
    }

    public void ShowSuccess(string message)
    {
        Show(MessageType.Success, message);
    }

    public void ShowWarning(string message)
    {
        Show(MessageType.Warning, message);
    } 
    #endregion

    #region Show control
    public void Show(MessageType messageType, string message)
    {
        CloseButton.Visible = ShowCloseButton;
        litMessage.Text = message;

        MessageBox.CssClass = messageType.ToString().ToLower();
        this.Visible = true;
    } 
    #endregion

    #region Enum
    public enum MessageType
    {
        Error = 1,
        Info = 2,
        Success = 3,
        Warning = 4
    } 
    #endregion
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无需解释 2024-07-23 20:42:49

好吧,我想我大致复制了您的描述,并删除了我原来的答案,因为它太离谱了。

我发现,当您希望内容页引用母版页中使用的用户控件并且该控件可访问时,您将收到一条错误,指示您需要引用特定的程序集,然后您会得到错误表明不存在此类类型的方法。

通过将子页面上的注册页面指令添加到用户控件解决了此问题。 即使使用 web.config 或页面上定义的控件,我也重现了这一点。 在这两种情况下,我仍然必须在内容页面上显式添加注册。

这对我来说没有意义,但它允许我的代码编译和工作。 试一试让我知道。

执行此操作后,您可以引用类似的控件。

this.Master.MessageBox.ShowInfo();

这假定您在母版页上有一个名为 MessageBox 的公共属性。

编辑

我还发现,如果您在主页面和内容页面上注册控件并且不使用 web.config,效果会更好。

编辑

如果您不希望子页面引用用户控件,您的另一个选择是在母版页上公开方法,例如 ShowInfo() ,它将委托给用户控件。

Ok I think I reproduced roughly what your describing and I deleted my original answer cause it was way off.

What I found is that when you want a content page to reference a user control being used in a master page and the control is accesible and what not, you will get an error indicating that you need to reference a specific assembly, and then you get errors indicating that no Method exists of type such and such.

By adding the Register page directive on the child page to the user control resolved this issue. I reproduced this even with the control defined in the web.config or on the page. In both cases I still had to explicitly add a Register on the content page.

This doesn't make sense to me but it allowed my code to compile and work. Give it a shot let me know.

Once you do this you can reference the control like

this.Master.MessageBox.ShowInfo();

This assumes that you have a public property called MessageBox on the Master Page.

Edit

I've also found that this works much better if you register the control on both the master and the content page and not use the web.config.

Edit

If you don't want your child page to reference the user control your other option is expose methods on the master page like ShowInfo() which would delegate to the user control.

生寂 2024-07-23 20:42:49

您需要将其声明为控件类型才能访问其属性。

public MyMessageBox BCError;
BCError = (MyMessageBox )Master.FindControl("BCError");

You need to declare it as your control type to access it's properties.

public MyMessageBox BCError;
BCError = (MyMessageBox )Master.FindControl("BCError");
失与倦" 2024-07-23 20:42:49

尝试在从母版页继承的页面中使用它:

<%@ MasterType VirtualPath="~/MasterPageName.Master" %>  

Try using this in the pages that inherit from your master page:

<%@ MasterType VirtualPath="~/MasterPageName.Master" %>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文