Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

asp.net - how to access master page control from content page

I have a master page which contains a label for status messages. I need to set the status text from different .aspx pages. How can this be done from the content page?

public partial class Site : System.Web.UI.MasterPage
{
    public string StatusNachricht
    {
        get
        {
            return lblStatus.Text;
        }
        set
        {
            lblStatus.Text = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {            

    }
}

I have tried this, but was unsuccessful in making it work:

public partial class DatenAendern : System.Web.UI.Page
{
    var master = Master as Site;

    protected void Page_Load(object sender, EventArgs e)
    {               
        if (master != null)
        {
            master.setStatusLabel("");
        }
    }        

    protected void grdBenutzer_RowCommand(object sender, GridViewCommandEventArgs e)
    {           
            try
            {
                //some code

                if (master != null)
                {
                    master.setStatusLabel("Passwort erfolgreich ge?ndert.");
                }
            }
            catch (Exception ex)
            {
                if (master != null)
                {
                    master.setStatusLabel("Passwort konnte nicht ge?ndert werden!");
                }                                       
            }
        }
    }                   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the MasterPage.cs file add the property of Label like this:

public string ErrorMessage
{
    get
    {
        return lblMessage.Text;
    }
    set
    {
        lblMessage.Text = value;
    }
}

On your aspx page, just below the Page Directive add this:

<%@ Page Title="" Language="C#" MasterPageFile="Master Path Name"..... %>
<%@ MasterType VirtualPath="Master Path Name" %>   // Add this

And in your codebehind(aspx.cs) page you can then easily access the Label Property and set its text as required. Like this:

this.Master.ErrorMessage = "Your Error Message here";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...