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

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

问题描述

背景:我正在自定义现有的ASP .NET / C#应用程序。它具有自己的一些框架和开发人员在扩展/定制其功能时遵循的约定。我目前正在扩展其中的一些管理功能,该框架提供了一个合同来执行 GetAdministrationInterface()方法的实现,该方法返回 System。 Web.UI.Control 。此方法在托管GUI界面的页面的 Page_Load()方法中调用。

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.

问题:我的GUI中有三个按钮,每个按钮分配了一个事件处理程序。我的管理GUI加载完全正常,但点击任何按钮不会做我期望他们做的。但是,当我第二次点击它们时,按钮就可以工作。

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.

任何想法?

按钮定义示例(内部 GetAdministrationInterface

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...
}

事件处理程序方法定义示例

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

Page_Load 调用 GetAdministrationInterface

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


推荐答案

好悲伤!我知道这将是愚蠢的事情。纯粹是我的错,当然我在ASP.NET中缺乏知识。

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.

Google进行了大量的Google搜索,最终被Google拦截,怀疑自己是一个机器人运行自动化脚本,我设法挤压了最后一次搜索,偶然发现这个文章。已经在放弃的时候,我尽力阅读文章,而不是一次跳过10行或寻找漂亮的图片。在为动态创建的控件分配ID一节中,我阅读了这些神奇而最快乐的词语:

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:


如果您在单击不工作按钮之前查看源HTML,并且单击它之后,您会注意到一个小的区别。这些按钮在回发之前和之后都有不同的HTML ID。我在post-back之后得到ctl04和ctl05,post-back之后是ctl02和ctl03。

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按钮通过检查其ID的值来识别事件Request.Form集合。 (事实上​​,它发生不同的情况,控件不会自己检查Request.Form集合。页面将数据发布到控件的ID以及已注册的控件以被通知关于帖子数据)。 ASP.NET不会触发Click事件,因为按钮的ID已经在后端之间发生变化。您点击的按钮和您看到的按钮是ASP.NET的不同按钮。

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.

果然,当我查看HTML第一次,我的按钮的ID为$ code> ctl04 $ ctl36 。点击按钮后,我的按钮的ID为$ code> ctl04 $ ctl33 。

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.

所以你有它!我所要做的只是把按钮上的ID和presto!我的事件处理程序现在被称为!

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!

示例解决方案:

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按钮事件处理程序不会在第一次点击时触发,但在PostBack后第二次单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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