我动态创建的链接按钮事件不会触发. [英] My dynamically created link buttons event won't fire.

查看:73
本文介绍了我动态创建的链接按钮事件不会触发.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据应用程序先前创建的文件数量动态创建链接按钮.链接已成功创建,但是,当我尝试单击它们时,它只是进行回发而没有其他任何事情.

有人可以用我的代码为我指出正确的方向吗?谢谢!

I am dynamically creating link buttons based on how many files that were created earlier on in the application. The links are being created successfully, however, when I try to click them, it just does a postback and nothing else happens.

Can someone point me in the right direction with my code? Thank you!

LinkButton btnFile = new LinkButton();
           for (int i = 0; i < convertedFiles.Count; i++)
           {
               foreach (var filess in convertedFiles)
               {
                   btnFile.Text = "Download file for agency " + Agencynumber + "<br>";
                   btnFile.ID = filess;
                   btnFile.Visible = true;
                   btnFile.Click += new System.EventHandler(this.button_Click);
                   Form1.Controls.Add(btnFile);
               }

           }
protected void button_Click(object sender, EventArgs e)
     {
       //some code here
     }

推荐答案

一旦您看到此代码创建的按钮,便会触发该事件.我宁愿假设您没有遵守它.我可以建议检查两件事:

首先,有时您创建两个看起来几乎相同的按钮.一个按钮可以隐藏另一个按钮或类似的东西.您可能单击了错误的按钮.我记得现实生活中的这种情况.

其次,您可以单击一个右按钮,事件将触发,但是事件处理程序的代码并不能帮助您查看它,因为它由于某种错误没有执行您可以观察到的任何操作.为了确保在调试器中运行它,请首先在处理程序方法的最开始处设置一个断点.



等一下!有一个问题.我只是注意到您在循环之外创建了一个按钮.假设内部循环重复N次.这意味着您将相同按钮 N的实例创建到同一父控件(即您的窗体Form1​​)中.此外,您可以将相同的事件处理程序添加到相同按钮实例的调用列表中,并执行N次!

这没有任何合理的意义. (叹气)

—SA
As soon as you can see the button created by this code, the event should fire. I would rather assume that you fail to observe it. I can suggest to check up two things:

First, sometimes you create two buttons looking nearly the same way. One button could hide another one or something like that. You might click a wrong button. I remember such cases in real life.

Second, you might click a right button, and the event fires, but the code of even handler does no help you to see it, as it does not do anything you could observe, by some mistake. To make sure, run it under debugger, set a breakpoint at the very beginning of the handler method, at first.



Wait a minute! There is a problem. I just noticed that you create a button outside the loops. Let''s say, the inner loop is repeated N times. It means that you create an instance of the same very button N times into the same parent control (which is your form Form1). Moreover, you add the same very event handler to the invocation list of the same very button instance, and you do it N times!

It cannot make any reasonable sense. (Sigh…)

—SA


我将更进一步.我认为您的问题比Sergey指出的要严重得多.让我们来分解一下你在做什么:
I am going to take this a step further. I think that your problem is much worse than what Sergey indicates. Let''s break down what it is that you are doing:
LinkButton btnFile = new LinkButton();
           for (int i = 0; i < convertedFiles.Count; i++)
           {
               foreach (var filess in convertedFiles)
               {
                   //removed extraneous
                   Form1.Controls.Add(btnFile);
               }

           }
protected void button_Click(object sender, EventArgs e)
     {
       //some code here
     }



现在,您正在动态创建一个按钮,但是您要一遍又一遍地添加相同的按钮.但这比那更糟.看一下你的循环.您似乎正在迭代的字符串列表(也许).然后,我们进入foreach,在其中再次迭代相同的集合!你真的想要那个吗?如果您的convertFiles集合包含10个字符串,则for循环运行10次,然后foreach在每次迭代中添加10个按钮!这是很多按钮.

我认为您的整个例程可以归结为以下内容,并且我相信这就是您要执行的操作:



Now you are creating a button dynamically but you are adding the same button over and over. But it''s worse than that. Take a look at your loops. You have what appears to be a list of strings(maybe) that you are iterating over. Then we get to the foreach where we iterate over the same collection again! Do you really want that? If your convertedFiles collection contained, say, 10 strings, your for loop runs 10 times and then your foreach adds 10 buttons on each iteration! That''s a lot of buttons.

I think that your entire routine could be boiled down to the following and I believe that this is what you intended to do:

foreach (var filess in convertedFiles)
               {
                   LinkButton btnFile = new LinkButton();
                   btnFile.Text = "Download file for agency " + Agencynumber + "<br>";
                   btnFile.ID = filess;
                   btnFile.Visible = true;
                   btnFile.Click += new System.EventHandler(button_Click);
                   Form1.Controls.Add(btnFile);
               }
protected void button_Click(object sender, EventArgs e)
     {
       //some code here
     }



将按钮创建移至foreach循环并完全摆脱for循环.



Move the button creation to the foreach loop and get rid of the for loop altogether.


这篇关于我动态创建的链接按钮事件不会触发.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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