访问母版页中的事件和成员

时间:2022-09-20 15:54:56

I have an event in the Master page that I want to access in the pages that use that master page but it doesn't seem to be working.

我在母版页面中有一个事件,我想在使用母版页面的页面中访问它,但它似乎不工作。

In the Master

在主

public delegate void NotifyRequest(object sender, EventArgs e);
public class MyMaster
{
   public event NotifyRequest NewRequest;
   protected void uiBtnNewTask_Click(object sender, EventArgs e)
   { 
      if(NewRequest!= null)
         NewRequest(this, e)
   }
}

Inherited Page

继承了页面

MyMaster mm = new MyyMaster();
mm.NewRequest += new NotifyRequest(mm_NotifyRequest);
void mm_NotifyRequest(object sender, EventArgs e)
{
    Label1.Text = "Wow";
    this.Label1.Visible = true;
}

My problem is the event is always null. Any ideas?

我的问题是事件总是为空。什么好主意吗?

1 个解决方案

#1


3  

You probably need to use the following syntax to access your event in the Master Page

您可能需要使用以下语法来访问母版页面中的事件

((MyMaster)Master).NewRequest += new NotifyRequest(mm_NotifyRequest);

If you wish to access members of a master page you need to use the page Master attribute and cast it to your master page.

如果您希望访问母版页的成员,则需要使用页主属性并将其转换为母版页。

Alternately

交替

If you wish do not wish to use a cast you can use the @MasterType directive to create a strong reference to your master page, in this case MyMaster. So you would be able to access your event like so: Master.NewRequest.

如果不希望使用类型转换,可以使用@MasterType指令创建对母版页面的强引用,在本例中是MyMaster。这样你就能像这样访问你的事件了:Master.NewRequest。

More Reading about Master Pages

更多关于母版的阅读

#1


3  

You probably need to use the following syntax to access your event in the Master Page

您可能需要使用以下语法来访问母版页面中的事件

((MyMaster)Master).NewRequest += new NotifyRequest(mm_NotifyRequest);

If you wish to access members of a master page you need to use the page Master attribute and cast it to your master page.

如果您希望访问母版页的成员,则需要使用页主属性并将其转换为母版页。

Alternately

交替

If you wish do not wish to use a cast you can use the @MasterType directive to create a strong reference to your master page, in this case MyMaster. So you would be able to access your event like so: Master.NewRequest.

如果不希望使用类型转换,可以使用@MasterType指令创建对母版页面的强引用,在本例中是MyMaster。这样你就能像这样访问你的事件了:Master.NewRequest。

More Reading about Master Pages

更多关于母版的阅读