ASP .NET按钮事件处理程序不开火,第一次点击,但在回发后第二次点击 [英] ASP .NET Button event handlers do not fire on the first click, but on the second click after a PostBack

查看:122
本文介绍了ASP .NET按钮事件处理程序不开火,第一次点击,但在回发后第二次点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我自定义现有的ASP.NET / C#应用程序。它有它自己的小框架,并约定延长/自定义其功能时,开发人员跟随。我目前正在扩展它的一些管理功能,以该框架提供了一个合同的执行落实 GetAdministrationInterface()方法,它返回系统。 Web.UI.Control 。这种方法托管GUI界面页面的Page_Load()方法中被调用。

问题:我在我的GUI三个按钮,每个已分配事件处理程序。我的管理GUI负载可达完美的罚款,但点击任何按钮没有做什么,我希望他们做的事。然而,当我点击他们第二次的时候,按钮的工作。

我把断点在每个事件处理程序方法的开头,并通过我的code踩。在第一次点击,没有事件处理程序被触发。在第二次点击,他们解雇了。

任何想法?

按钮定义的示例(在 GetAdministrationInterface

 公众覆盖控制GetAdministrationInterface()
{
    //更多code ...    按钮BTN =新按钮();
    btn.Text =点击我!;
    btn.Click + =新的EventHandler(Btn_Click);    //更多code ...
}

事件处理方法定义的示例

 无效Btn_Click(对象发件人,EventArgs的发送)
{
    // 做一点事
}

的Page_Load 方法调用 GetAdministrationInterface

 保护无效的Page_Load(对象发件人,发送System.EventArgs)
{
    如果(!Page.IsAsync)
    {
        清单< AdministrationInterface>接口= LT;数据库调用取代;
        的foreach(AdministrationInteface在接口AI)
        {
            placeholderDiv.Controls.Add(ai.GetAdministrationInterface());
        }
    }
}


解决方案

天哪!我知道这将是什么这个愚蠢的。纯属我当然错,我在ASP .NET知识的缺乏。

做的谷歌搜索众多,最终被阻止谷歌对正在运行的自动化脚本一个机器人的怀疑后,我好不容易在最后一个搜索挤压并在此的article 。早在放弃的时候,我尽我所能阅读的文章没有跳过一次10行或寻找pretty图片。在标题为 ID分配给动态创建的控件的,我读了这些神奇的和最快乐的话:


  

如果您单击不工作的按钮,您点击它后,你会发现一个小的差别之前查看HTML源。按钮具有前后后回后不同的HTML标识。我在回后后后回和ctl02和ctl03之前得到ctl04和ctl05。


  
  

ASP.NET按钮,通过检查其Request.Form集合中ID的价值认识的事件。 (事实上​​它发生不同和控件本身不检查Request.Form集合。页由它们的ID和注册到被通知后的数据传送控制数据后向控制)。 ASP.NET不火的Click事件,因为按钮的ID已后背上之间变化。你点击该按钮,你看到后,按钮是不同的按钮ASP.NET。


果然,当我第一次看到的HTML,我的按钮有ID ctl04 $ ctl36 。点击该按钮后,我的按钮有ID ctl04 $ ctl33

所以你有它!我所要做的就是设置ID按钮和preSTO!现在我的事件处理程序被调用!

解决方案示例:

 公众覆盖控制GetAdministrationInterface()
{
    //更多code ...    按钮BTN =新按钮();
    btn.Text =点击我!;
    //我!!存在的祸根!
    btn.ID =The_Bane_of_My_Existence;
    //我!!存在的祸根!
    btn.Click + =新的EventHandler(Btn_Click);    //更多code ...
}

真是一个伟大的方式来花两天时间...

Background: I am customizing an existing ASP .NET / C# application. It has it's own little "framework" and conventions for developers to follow when extending/customizing its functionality. I am currently extending some of it's administrative functionality, to which the framework provides a contract to enforce implementation of the GetAdministrationInterface() method, which returns System.Web.UI.Control. This method is called during the Page_Load() method of the page hosting the GUI interface.

Problem: I have three buttons in my GUI, each of which have been assigned an Event Handler. My administration GUI loads up perfectly fine, but clicking any of the buttons doesn't do what I expect them to do. However, when I click them a second time, the buttons work.

I placed breakpoints at the beginning of each event handler method and stepped through my code. On the first click, none of the event handlers were triggered. On the second click, they fired.

Any ideas?

Example of Button Definition (within GetAdministrationInterface)

public override Control GetAdministrationInterface()
{
    // more code...

    Button btn = new Button();
    btn.Text = "Click Me!";
    btn.Click += new EventHandler(Btn_Click);

    // more code...
}

Example of Event Handler Method Definition

void Btn_Click(object sender, EventArgs e)
{
    // Do Something
}

Page_Load Method that calls GetAdministrationInterface

protected void Page_Load(object sender, System.EventArgs e)
{
    if (!Page.IsAsync)
    {
        List<AdministrationInterface> interfaces = <DATABASE CALL>;
        foreach(AdministrationInteface ai in interfaces)
        {
            placeholderDiv.Controls.Add(ai.GetAdministrationInterface());
        }
    }
}

解决方案

Good grief! I knew it was going to be something this stupid. Purely my fault of course and my lack of knowledge in ASP .NET.

After doing a multitude of Google searches and eventually being blocked by Google on suspicion of being a bot running automated scripts, I managed to squeeze in one last search in and stumbled across this article. Already at the point of giving up, I tried my best to read the article without skipping 10 lines at a time or looking for pretty pictures. In the section titled Assigning IDs to Dynamically Created Controls, I read these magical and most joyful words:

If you view the source HTML before you click the not-working button and after you have clicked it, you will notice a small difference. The buttons have different HTML IDs before and after the post-back. I got ctl04 and ctl05 before the post-back and ctl02 and ctl03 after the post-back.

ASP.NET button recognizes events by checking for a value for its ID in the Request.Form collection. (In truth it happens differently and controls do not check Request.Form collection by themselves. Page passes post data to controls by their IDs and to controls that are registered to be notified about post data). ASP.NET does not fire the Click event, because the button's ID has changed between the post-backs. The button you have clicked and the button you see after are different buttons for ASP.NET.

Sure enough, when I viewed the HTML the first time, my button had the ID ctl04$ctl36. After clicking the button, my button had the ID ctl04$ctl33.

So there you have it! All I had to do was set the ID on the buttons and presto! My event handlers are now being called!

Sample Solution:

public override Control GetAdministrationInterface()
{
    // more code...

    Button btn = new Button();
    btn.Text = "Click Me!";
    // !!THE BANE OF MY EXISTENCE!!
    btn.ID = "The_Bane_of_My_Existence";
    // !!THE BANE OF MY EXISTENCE!!
    btn.Click += new EventHandler(Btn_Click);

    // more code...
}

What a great way to spend two days...

这篇关于ASP .NET按钮事件处理程序不开火,第一次点击,但在回发后第二次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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