事件处理程序为null [英] EventHandler is null

查看:502
本文介绍了事件处理程序为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从提高用户控件click事件,并处理它包含页面上。我的问题是,当我点击用户控制按钮'imgstep1,背后imgstep1_click事件code触发器和但是btnHandler事件常是空。因此,它不调用父类的事件。

在任何帮助,这将大大AP preciated。

我的用户控制code是:

的.ascx code:

 < ASP:ImageButton的的ImageUrl =./图像/ step1.gif
        ID =imgstep1=服务器
         的OnClick =imgstep1_Click/>

.ascx.cs code:

 公共委托无效OnImageButtonClick();
    公共事件OnImageButtonClick btnHandler;    保护无效imgstep1_Click(对象发件人,ImageClickEventArgs E)
    {
        如果(btnHandler!= NULL)
            btnHandler();
    }

.aspx页面中code:

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
     ucStepHdr.btnHandler + =新StepsHeader.OnImageButtonClick(ucStepHdr_btnHandler);
}无效ucStepHdr_btnHandler()
{
  的Response.Write(测试);
}


解决方案

在code看起来很简单才能正常工作。这btnHandler是空的唯一原因可能是因为在aspx页面的事件登记code不叫。

有一个回?你确定你添加的事件每次页面加载???

  ucStepHdr.btnHandler + =新StepsHeader.OnImageButtonClick(ucStepHdr_btnHandler);

I am trying to raise a click event from User control and handle it on the containing page. The problem I have is, when I click the button 'imgstep1' on the user control, the code behind imgstep1_click event triggers and but the 'btnHandler' event is alway null. Hence it doesnt call the parent event.

Any help on this will be much appreciated.

My User Control Code is :

.ascx code:

<asp:ImageButton ImageUrl="./images/step1.gif" 
        ID="imgstep1" runat="server" 
         OnClick="imgstep1_Click"/>

.ascx.cs code :

    public delegate void OnImageButtonClick();
    public event OnImageButtonClick btnHandler;

    protected void imgstep1_Click(object sender, ImageClickEventArgs e)
    {
        if (btnHandler != null)
            btnHandler();
    }

.aspx page code:

protected void Page_Load(object sender, EventArgs e)
{
     ucStepHdr.btnHandler += new StepsHeader.OnImageButtonClick(ucStepHdr_btnHandler);
}

void ucStepHdr_btnHandler()
{
  Response.Write ('test');
}

解决方案

The code looks simple enough to work correctly. The only reason that btnHandler is null could be because the event registration code in the aspx page is not called.

Is there a post back ? Are you sure you are adding the event EACH TIME the page loads ???

ucStepHdr.btnHandler += new StepsHeader.OnImageButtonClick(ucStepHdr_btnHandler);

这篇关于事件处理程序为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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