通过母版的ImageButton事件内容页 [英] Pass MasterPage ImageButton event to content Page

查看:98
本文介绍了通过母版的ImageButton事件内容页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ImageButton的一个母版。我希望onclick事件触发和母版?

里面承载的.aspx页面中被捕获

母版:

 < ASP:ImageButton的ID =btnClear的OnClick =Clear_Click
 的ImageUrl =图片/ Back_Icon_06.png=服务器还有AlternateText =清除
 WIDTH =38HEIGHT =39/>


解决方案

母版实际上是页面的子(事实上,这是一个用户控件)。我们不希望页面必须知道它的子控件(这就是为什么我们委托那些方面的控制摆在首位)的贴心细节,所以正确的做法是处理主服务器上的单击事件页面,并从那里开火,该页面处理母版另一个事件:

站长:

 公共事件的EventHandler SomethingHappened;保护无效Button_Click(对象发件人,EventArgs的发送)
{
    OnSomethingHappened(EventArgs.Empty);
}保护无效OnSomethingHappened(EventArgs的发送)
{
    如果(this.SomethingHappened!= NULL)
    {
        this.SomethingHappened(这一点,E);
    }
}

页:

 保护覆盖无效的OnInit(EventArgs的发送)
{
    base.OnInit(E);
    //允许更改母版页
    如果(this.Master是MyMaster)
    {
        ((MyMaster)this.Master).SomethingHappened + =新的EventHandler(HandleSomethingHappened);
    }
}私人无效HandleSomethingHappened(对象发件人,EventArgs的发送)
{
    //处理它
}

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"/>

解决方案

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
}

这篇关于通过母版的ImageButton事件内容页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆