Add OnClick button event dynamically

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
 
M

Munsifali Rashid

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
 
C

Carl Prothman [MVP]

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top