views:

61

answers:

2

Hi all,

We are creating dynamic text boxes and buttons inside a grid for each row. Now we want to create click event for each button. Tocreate 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 showing error massage : imbtnAdd_Click does not exist

+1  A: 
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
is this single event is enough for all the buttons inside the grid?
Geetha
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
A: 

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