访问MasterPage的公共属性

访问MasterPage的公共属性

本文介绍了如何从外部类访问MasterPage的公共属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能从其他类(错误)访问MasterPage的公共属性(MessagePlaceholder)?

Why i can't access MasterPage's public property (MessagePlaceholder) from other Class (Errors) ?

错误编译器给出的错误消息是错误1找不到类型或名称空间名称'MyMasterPage'(您是否缺少using指令或程序集引用?)"

Error compiler gives me is "Error 1 The type or namespace name 'MyMasterPage' could not be found (are you missing a using directive or an assembly reference?)"

我的母版页代码后面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MyMasterPage : System.Web.UI.MasterPage
{
    public string MessagePlaceholder
    {
        get { return messagePlaceholder.InnerHtml; }
        set { messagePlaceholder.InnerHtml = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            messagePlaceholder.InnerHtml = Errors.getMessage();
        }
    }
}

我的错误类别

 public static string getMessage()
    {
        HttpContext c = HttpContext.Current;
        string messageType = "";
        if (c.Session["errorMessage"] != null)
        {
            messageType = "errorMessage";
        }
        else if (c.Session["successMessage"] != null)
        {
            messageType = "successMessage";
        }

        if (!string.IsNullOrEmpty(messageType))
        {
            StringBuilder userMessageSb = new StringBuilder();
            userMessageSb.Append(string.Format("<div id=\"{0}\" title=\"{1}\">{2}</div>", messageType, messageType.Replace("Message",string.Empty), c.Session[messageType]));

            // fix so message will not re-appear
            c.Session.Remove(messageType);

            messageType = userMessageSb.ToString();
        }
        return messageType;
    }

    public static void setSuccess(string successMessage, bool isRedirect)
    {
        HttpContext.Current.Session["successMessage"] = successMessage;
    }

    public static void setError(string errorMessage, bool isRedirect)
    {
        HttpContext.Current.Session["errorMessage"] = errorMessage;

        if (!isRedirect)
        {
            ((HttpContext.Current.CurrentHandler as System.Web.UI.Page).Master as MyMasterPage).MessagePlaceholder = getMessage();
        }
    }

这是我设置错误的方式

    if (true)
    {
        Errors.setError("this is an error demo", false);
        return;
    }

或在错误后重定向

   if (true)
    {
        Errors.setError("yet another error", true);
        Response.Redirect("~/error.aspx");
    }

推荐答案

尝试类似的方法.

Control FindMe = Page.FindControl("MessagePlaceholder")

if (FindMe != null)
{
    ((Literal)FindMe).Text =  getMessage();
}

放置在页面上,(将HttpContext.Current.CurrentHandler作为System.Web.UI.Page),或仅从调用中发送Page值.

Place on Page, the (HttpContext.Current.CurrentHandler as System.Web.UI.Page), or just send the Page value from your call.

其他更好的代码.

if(FindMe is Literal)
{
  ((Literal)FindMe).Text = getMessage();
}

这篇关于如何从外部类访问MasterPage的公共属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 03:59