Add OnClick button event dynamically

  • Thread starter Thread starter anony
  • Start date Start date
A

anony

Hi,

I'm trying to dynamically add a button to my ASP.NET / VB page. I can't
seem to figure out how to add a routine to the OnClick event. Any insight
would be greatly appreciated!

Thanks,
Brian
 
To add a server-side event handler, use the following code:

MyButton.Click += new System.EventHandler(MyButton_Click)

where MyButton_Click is as follows:

private void MyButton_Click (object sender, System.EventArgs e)
{
// Your button click code here.
}

If you want to do something client-side in the onClick event, use:

myButton.Attributes.Add("onClick", "fnClientSideThing()");

This code will fire the client-side javascript function called
fnClientSideThing() when the button is clicked.

Hope this helps,

Mun
 
Back
Top