ASP.Net forms button qouestion

  • Thread starter Thread starter Mateo
  • Start date Start date
M

Mateo

Hi!

Let's say You generate button from code. Web forms control, not HTML button.
How can I catch it's OnClick event? Where to put code which is exedcuted on
buttons OnClick event?

Thanx!

Example:
Dim ctrCommandButton As New Button

ctrCommandButton.Text = "click me"

......

'let's add it for example in table cell

......

cell.Controls.Add(ctrCommandButton)

......

how to catch OnClick?
 
Hello,

I know you are using VB.NET however, I am able to show you an example
in C# that might help.

If you are using MS Visual Studio 2005 the easiest way to add an
OnClick event is to select the Button and then view the properties
panel. On the properties panel you will see a small picture of a
lightening bolt. This is the events menu. When you click on the Events
menu you will see an OnClick event. All you have to do is to double
click the OnClick event and the code with standard parameters will be
added to the code behind page.

// ---- This is the code from the Default.aspx file ---------------
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>


// --- This is some of the code from the code behind file for
Default.aspx.cs w/o all of the using statements -------
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
// ----- your do something code goes here.
}
}


The most important part is to make sure the Event signature
"Button1_Click" in the control and the event handler are exactly the
same or else it will not work.

Hope this helps,
Charles
 
Back
Top