如何从内容页访问母版页控件

时间:2022-10-19 23:24:29

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?

我有一个包含状态消息标签的主页。我需要设置来自不同.aspx页面的状态文本。如何从内容页面实现这一点?

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!");
                }                                       
            }
        }
    }                   
}

6 个解决方案

#1


64  

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

MasterPage。cs文件添加标签的属性如下:

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

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

在您的aspx页面上,就在页面指示下面添加以下内容:

<%@ 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:

在您的codebehind(aspx.cs)页面中,您可以轻松访问Label属性,并根据需要设置其文本。是这样的:

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

#2


23  

In Content page you can access the label and set the text such as

在Content页面中,您可以访问标签并设置文本,例如

Here 'lblStatus' is the your master page label ID

这里的“lblStatus”是您的主页标签ID

Label lblMasterStatus = (Label)Master.FindControl("lblStatus");

lblMasterStatus.Text  = "Meaasage from content page";

#3


6  

It Works

它的工作原理

To find master page controls on Child page

查找子页上的母版页控件

Label lbl_UserName = this.Master.FindControl("lbl_UserName") as Label;                    
lbl_UserName.Text = txtUsr.Text;

#4


3  

You cannot use var in a field, only on local variables.

不能在字段中使用var,只能在局部变量上使用。

But even this won't work:

但即使这样也行不通:

Site master = Master as Site;

Because you cannot use this in a field and Master as Site is the same as this.Master as Site. So just initialize the field from Page_Init when the page is fully initialized and you can use this:

因为你不能在字段中使用这个,而Master as Site和这个是一样的。主网站。因此,当页面完全初始化时,只需从Page_Init初始化字段,您可以使用以下方法:

Site master = null;

protected void Page_Init(object sender, EventArgs e)
{            
    master = this.Master as Site;
}

#5


3  

I have a helper method for this in my System.Web.UI.Page class

我在System.Web.UI中有一个帮助方法。页面类

protected T FindControlFromMaster<T>(string name) where T : Control
{
     MasterPage master = this.Master;
     while (master != null)
     {
         T control = master.FindControl(name) as T;
         if (control != null)
             return control;

         master = master.Master;
     }
     return null;
}

then you can access using below code.

然后您可以使用下面的代码访问。

Label lblStatus = FindControlFromMaster<Label>("lblStatus");
if(lblStatus!=null) 
    lblStatus.Text = "something";

#6


1  

This is more complicated if you have a nested MasterPage. You need to first find the content control that contains the nested MasterPage, and then find the control on your nested MasterPage from that.

如果您有一个嵌套的母版,这将更加复杂。您需要首先找到包含嵌套母版的内容控件,然后再从中找到嵌套母版上的控件。

Crucial bit: Master.Master.

关键的一点:Master.Master。

See here: http://forums.asp.net/t/1059255.aspx?Nested+master+pages+and+Master+FindControl

看到:http://forums.asp.net/t/1059255.aspx?Nested +主+ +和+主+ FindControl页面

Example:

例子:

'Find the content control

“查找内容控制

Dim ct As ContentPlaceHolder = Me.Master.Master.FindControl("cphMain")

Dim ct作为ContentPlaceHolder = me . master . findcontrol(“cphMain”)

'now find controls inside that content

现在找到内容里面的控件

Dim lbtnSave As LinkButton = ct.FindControl("lbtnSave")

Dim lbtnSave As LinkButton = ct.FindControl(“lbtnSave”)

#1


64  

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

MasterPage。cs文件添加标签的属性如下:

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

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

在您的aspx页面上,就在页面指示下面添加以下内容:

<%@ 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:

在您的codebehind(aspx.cs)页面中,您可以轻松访问Label属性,并根据需要设置其文本。是这样的:

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

#2


23  

In Content page you can access the label and set the text such as

在Content页面中,您可以访问标签并设置文本,例如

Here 'lblStatus' is the your master page label ID

这里的“lblStatus”是您的主页标签ID

Label lblMasterStatus = (Label)Master.FindControl("lblStatus");

lblMasterStatus.Text  = "Meaasage from content page";

#3


6  

It Works

它的工作原理

To find master page controls on Child page

查找子页上的母版页控件

Label lbl_UserName = this.Master.FindControl("lbl_UserName") as Label;                    
lbl_UserName.Text = txtUsr.Text;

#4


3  

You cannot use var in a field, only on local variables.

不能在字段中使用var,只能在局部变量上使用。

But even this won't work:

但即使这样也行不通:

Site master = Master as Site;

Because you cannot use this in a field and Master as Site is the same as this.Master as Site. So just initialize the field from Page_Init when the page is fully initialized and you can use this:

因为你不能在字段中使用这个,而Master as Site和这个是一样的。主网站。因此,当页面完全初始化时,只需从Page_Init初始化字段,您可以使用以下方法:

Site master = null;

protected void Page_Init(object sender, EventArgs e)
{            
    master = this.Master as Site;
}

#5


3  

I have a helper method for this in my System.Web.UI.Page class

我在System.Web.UI中有一个帮助方法。页面类

protected T FindControlFromMaster<T>(string name) where T : Control
{
     MasterPage master = this.Master;
     while (master != null)
     {
         T control = master.FindControl(name) as T;
         if (control != null)
             return control;

         master = master.Master;
     }
     return null;
}

then you can access using below code.

然后您可以使用下面的代码访问。

Label lblStatus = FindControlFromMaster<Label>("lblStatus");
if(lblStatus!=null) 
    lblStatus.Text = "something";

#6


1  

This is more complicated if you have a nested MasterPage. You need to first find the content control that contains the nested MasterPage, and then find the control on your nested MasterPage from that.

如果您有一个嵌套的母版,这将更加复杂。您需要首先找到包含嵌套母版的内容控件,然后再从中找到嵌套母版上的控件。

Crucial bit: Master.Master.

关键的一点:Master.Master。

See here: http://forums.asp.net/t/1059255.aspx?Nested+master+pages+and+Master+FindControl

看到:http://forums.asp.net/t/1059255.aspx?Nested +主+ +和+主+ FindControl页面

Example:

例子:

'Find the content control

“查找内容控制

Dim ct As ContentPlaceHolder = Me.Master.Master.FindControl("cphMain")

Dim ct作为ContentPlaceHolder = me . master . findcontrol(“cphMain”)

'now find controls inside that content

现在找到内容里面的控件

Dim lbtnSave As LinkButton = ct.FindControl("lbtnSave")

Dim lbtnSave As LinkButton = ct.FindControl(“lbtnSave”)