Dynamic generation of Button
hi everyone
i have tried generating Button control dynamically... but faced two problems:
1) if i want to add this button to any container control...then its fine( i can user control.add(objButton)...but i couldnt work out how to add it to a form and place it at desired location;
2)how do we add event handler for this dynamically generated button...i mean where do we write coe to handle Click event for this button.
thnx in advance.
Re: Dynamic generation of Button
1) You can either use a placeholder control or add the button to your form's control collection. As every control in the form has a index number you can use the AddAt(Int32, Control) method rather than the Add(Control) to position it where you want.
2) Try:
Code:
// Handle Click Event
void btnName_Click(object sender, EventArgs e) {
// Do whatever
}
// Create Button
Button btnName = new Button();
btnName.ID = "newButton";
btnName.Text = "Click here";
btnName.Click += new EventHandler(btnName_Click);
PlaceHolderName.Controls.Add(btnName);
Hope that makes sense.
DJ