0

We are creating dynamic text boxes and buttons inside a grid for each row. Now we want to create click event for each button. To create button inside the grid in using ITemplate.

Code:

ImageButton imbtnAdd = new ImageButton();
imbtnAdd.ID = "imbtn" + columnName;
imbtnAdd.ImageUrl = "btn_add_icon.gif";
imbtnAdd.Width = 20;                    
container.Controls.Add(imbtnAdd);

Error:

I have used imbtnAdd.Click += new ImageClickEventHandler(imbtnAdd_Click); but it shows an error message

imbtnAdd_Click does not exist

Falko
  • 17,076
  • 13
  • 60
  • 105
Geetha
  • 199
  • 1
  • 8
  • 19

2 Answers2

2
ImageButton imbtnAdd = new ImageButton();
imbtnAdd.ID = "imbtn" + columnName;
imbtnAdd.ImageUrl = "btn_add_icon.gif";
imbtnAdd.Width = 20;             

imbtnAdd.Click += imbtnAdd_Click;

container.Controls.Add(imbtnAdd);

// ...

private void imbtnAdd_Click(object sender, EventArgs e)
{
    // handle event
}
jrista
  • 32,447
  • 15
  • 90
  • 130
  • is this single event is enough for all the buttons inside the grid? – Geetha Nov 14 '09 at 05:27
  • If they all do the same thing when clicked, yes. If you need them to do different things, you may be able to handle that in the event, but you are also free to attach any of a number of handlers to any given button. – jrista Nov 14 '09 at 05:28
1

Jrista's answer is correct.

Although, if you want to implement different handlers for all the buttons and you are using .Net 3.0 or above, you can use lambdas:

imbtnAdd.Click += (object sender, EventArgs e) =>
{
    // Code handling code goes here...
};
Yogesh
  • 14,498
  • 6
  • 44
  • 69