事件处理程序为空

发布于 2024-08-04 07:31:40 字数 874 浏览 5 评论 0原文

我正在尝试从用户控件引发单击事件并在包含页面上处理它。我遇到的问题是,当我单击用户控件上的按钮“imgstep1”时,imgstep1_click 事件后面的代码会触发,但“btnHandler”事件始终为空。因此它不会调用父事件。

对此的任何帮助将不胜感激。

我的用户控制代码是:

.ascx 代码:

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

.ascx.cs 代码:

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

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

.aspx 页面代码:

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

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

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');
}

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

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

发布评论

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

评论(3

说谎友 2024-08-11 07:31:40

该代码看起来很简单,可以正常工作。 btnHandler 为 null 的唯一原因可能是因为未调用 aspx 页面中的事件注册代码。

有回帖吗?您确定每次加载页面时都添加事件吗???

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

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);
赏烟花じ飞满天 2024-08-11 07:31:40

如果您删除 OnClick="imgstep1_Click" 并将其放入 ascx.cs 中,

protected ImageButton imgstep1;

protected override void OnInit(EventArgs e)
{
    this.imgstep1.Click += new ImageClickEventHandler(imgstep1_Click);
}

这种连接事件的方法是否有效?

If you remove OnClick="imgstep1_Click" and you put this in your ascx.cs

protected ImageButton imgstep1;

protected override void OnInit(EventArgs e)
{
    this.imgstep1.Click += new ImageClickEventHandler(imgstep1_Click);
}

Does this method of wiring up your event work?

游魂 2024-08-11 07:31:40

看起来它应该可以工作...您可以在调试器中单步执行代码,并在 Page_Load 中设置 ucStepHdr.btnHandler 后立即查看它的值是什么吗? (顺便说一句,传统上这些是在 init 中设置的,而不是在 load 中设置的,但这不是你的问题。)

It looks like it should work... can you step through the code in the debugger, and see what the value of ucStepHdr.btnHandler is as soon as you set it in Page_Load? (Just an aside, traditionally these are set in init rather than load, but this isn't your issue.)

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