0

In my application i have array of buttons created dynamically.I am trying to raise an onclick event for those buttons and change the text of the button which i click.I tried the below code for this but its not working.How can i do this?.Any suggesions?

Code:

   for (int i = 0; i < 5; i++)
    {
        lbl = new Button[5];
        lbl[i] = new Button();
        lbl[i].Text = "hi";
        lbl[i].Width = 30;
        lbl[i].Click += new EventHandler(lbl_click);
        //lbl[i].CssClass = "label";
        div1.Controls.Add(lbl[i]);
    }

Click Event:

   protected void lbl_click(object sender, EventArgs e)
   {
    Button[] lbl = sender as button[];
    lbl[i].Text = "clicked";

   }
4
  • Is this your actual code? You're recreating the button array from scratch both in your Click handler and in every iteration of the loop in your first code snippet. (Your edit only fixed the first problem.) Commented Mar 29, 2013 at 8:23
  • You'll have to define your for loop on OnInit event of page life cycle Commented Mar 29, 2013 at 8:24
  • Ah, and your edit also introduced another problem: sender is a Button, not a Button[]. Commented Mar 29, 2013 at 8:30
  • @FrédéricHamidi i tried steve's answer it works in pageload but not inside a method. Commented Mar 29, 2013 at 9:32

1 Answer 1

4

You are recreating the array of buttons in your event handler, but this array is not populated with the buttons created before. It is empty and will give you a null reference exception if you try to use an element of this array (null.Text, it will never work).
The sender object instead, represent the button that the user has clicked.

protected void lbl_click(object sender, EventArgs e)
{
     Button lbl = sender as Button;
     lbl.Text = "clicked";
}

Also, if you need to know which specific button has been clicked then I suggest you to add something to differentiate between them at creation time:

For example use the name property:

Button[] lbl = new Button[5];
for(int i = 0; i< 5; i++)
{
    ....
    lbl[i].Name = "Button_" + i.ToString();
    ....
}

Notice that I have moved the array declaration and initialization outside the loop that create every single element of the array (the actual button).

Sign up to request clarification or add additional context in comments.

5 Comments

if i use this in pageload its working.but if i use it inside a method then the event is not firing.
Take a look at the Related column on the right. There are numerous questions on this problem.
I saw that.It tells controls should be created only on page load.Is that true.We cannot create it anywhere else?
Sorry, I can't really help on this because I am not an expert on ASP.NET. Perhaps a new question on the specific matter could catch the interest of new people on this
check this i have asked another question on this stackoverflow.com/questions/15738693/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.