将 MasterPage ImageButton 事件传递给内容页面

发布于 2024-08-08 06:34:13 字数 301 浏览 4 评论 0原文

我在 MasterPage 中有 ImageButton。我希望触发 OnClick 事件并由 MasterPage 内托管的 .ASPX 页面捕获?

母版页:

<asp:ImageButton ID="btnClear" OnClick="Clear_Click" 
 ImageUrl="images/Back_Icon_06.png" runat="server" AlternateText="Clear" 
 width="38"   height="39"/>

I have ImageButton in a MasterPage. I want the OnClick event to fire and be captured by the .ASPX page hosted inside the MasterPage?

MasterPage:

<asp:ImageButton ID="btnClear" OnClick="Clear_Click" 
 ImageUrl="images/Back_Icon_06.png" runat="server" AlternateText="Clear" 
 width="38"   height="39"/>

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

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

发布评论

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

评论(2

檐上三寸雪 2024-08-15 06:34:13

母版页实际上是页面的子级(事实上,它是一个 UserControl)。我们不希望页面必须了解其子控件的私密细节(这就是为什么我们首先将这些方面委托给这些控件),因此正确的方法是处理主控件上的单击事件页面并从那里触发该页面处理的母版页上的另一个事件:

Master:

public event EventHandler SomethingHappened;

protected void Button_Click(object sender, EventArgs e)
{
    OnSomethingHappened(EventArgs.Empty);
}

protected void OnSomethingHappened(EventArgs e)
{
    if(this.SomethingHappened != null)
    {
        this.SomethingHappened(this, e);
    }
}

Page:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    //allows us to change master pages
    if(this.Master is MyMaster)
    {
        ((MyMaster)this.Master).SomethingHappened += new EventHandler(HandleSomethingHappened);
    }
}

private void HandleSomethingHappened(object sender, EventArgs e)
{
    //deal with it
}

The masterpage is actually a child of the page (in fact, it's a UserControl). We don't want the page to have to be aware of the intimate details of its child controls (thats why we delegate those aspects to those controls in the first place), so the correct approach would be to handle the click event on the master page and from there fire another event on the masterpage which the page handles:

Master:

public event EventHandler SomethingHappened;

protected void Button_Click(object sender, EventArgs e)
{
    OnSomethingHappened(EventArgs.Empty);
}

protected void OnSomethingHappened(EventArgs e)
{
    if(this.SomethingHappened != null)
    {
        this.SomethingHappened(this, e);
    }
}

Page:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    //allows us to change master pages
    if(this.Master is MyMaster)
    {
        ((MyMaster)this.Master).SomethingHappened += new EventHandler(HandleSomethingHappened);
    }
}

private void HandleSomethingHappened(object sender, EventArgs e)
{
    //deal with it
}
岛徒 2024-08-15 06:34:13

我建议在内容页面中指定强类型母版页,并在母版页端公开事件

这是一个很好的MSDN 参考

这类似于 Rex M 指定的内容,但只是稍微简化了对母版页的访问。

// Master Page Code
public event EventHandler ClearClick
{
   add
   {
      this.btnClear.Click += value;
   }
   remove
   {
      this.btnClear.Click -= value;
   }
}

// Content Page markup
<%@ Page  masterPageFile="~/MasterPage.master"%>

<%@ MasterType  virtualPath="~/MasterPage.master"%> 

// Content Page Code
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.Master.ClearClick += new EventHandler(OnClearClick);
}

private void OnClearClick(object sender, EventArgs e)
{
   // Handle click here
}

I would recommend specifying a strongly typed master page in your content page and exposing the event on the master page side

Here's a good MSDN reference

This is similar to what Rex M specified but just simplifies accessing the Master Page a little bit.

// Master Page Code
public event EventHandler ClearClick
{
   add
   {
      this.btnClear.Click += value;
   }
   remove
   {
      this.btnClear.Click -= value;
   }
}

// Content Page markup
<%@ Page  masterPageFile="~/MasterPage.master"%>

<%@ MasterType  virtualPath="~/MasterPage.master"%> 

// Content Page Code
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    this.Master.ClearClick += new EventHandler(OnClearClick);
}

private void OnClearClick(object sender, EventArgs e)
{
   // Handle click here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文