Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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!

C#
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
     }
Posted
Comments
Zoltán Zörgő 29-Jun-12 17:17pm    
//some code here should happen, if this is really empty, what you expect?
Sergey Alexandrovich Kryukov 29-Jun-12 23:15pm    
That's one of my points. It could be even non-empty, still doing nothing observable -- please see my answer.
--SA
Richard C Bishop 29-Jun-12 17:18pm    
It is not empty, I only posted relevent code.
Sergey Alexandrovich Kryukov 29-Jun-12 23:16pm    
You are right, but if you see something which looks like a miracle, you can check up more possibilities for a problem.
--SA
Jackie00100 29-Jun-12 17:18pm    
Dosen't event normally suppose to be public?

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.

[EDIT]

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
 
Share this answer
 
v3
Comments
Richard C Bishop 2-Jul-12 9:55am    
I tried debugging it with breakpoints and it never stopped on the button click event.

Maybe you can suggest a different method regarding what I am trying to do.

My application converts excel files to .txt and .ach files. One of the files contains info to create multiple files. I get those files created just fine, I just cannot figure out to prompt the user a save as box for each file. Can you think of another way to accomplish this? Thank you!
Sergey Alexandrovich Kryukov 2-Jul-12 15:39pm    
How can I know what it could be if you show only a part of code, and there is nothing fundamentally wrong with it?
Well, put a break point on this line and make sure the += operator was actually executed:
btnFile.Click += new System.EventHandler(this.button_Click);

You need some debugging skills...
--SA
Sergey Alexandrovich Kryukov 2-Jul-12 15:42pm    
What are you doing?! Please look at my updated answer above, after [EDIT].
--SA
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:
C#
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
     }


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:
C#
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
     }


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900