# onClick function does not work!

**URL:** https://forum.kirupa.com/t/onclick-function-does-not-work/299893
**Category:** Uncategorized
**Created:** [November 12, 2009, 9:04am UTC](https://forum.kirupa.com/t/onclick-function-does-not-work/299893 "2009-11-12T09:04:06Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![stephsh](https://avatars.discourse-cdn.com/v4/letter/s/49beb7/32.png) [@stephsh](https://forum.kirupa.com/u/stephsh)
#### Post date: [November 12, 2009, 9:04am UTC](https://forum.kirupa.com/t/onclick-function-does-not-work/299893/1 "2009-11-12T09:04:06Z")

</div>

I added a simple button to my Default.aspx page as follows:

```php

 
<p><asp:Label ID="LbSubscription" runat="server" Text="To have a chance on winning a year long free subscription to The Financial Times click below:"></asp:Label>
<br /><asp:Button ID="BnWinner" runat="server" Text="I feel like a winner!" OnClick="BnWinner_Click" />
</p>

```

In my Default.aspx.cs I then added the following code:

```php

public void BnWinner_Click(object sender, EventArgs e)
{
// On clicking on button "winner", a msg appears
Label myLabel = new Label();
myLabel.ID = "labelTest";
myLabel.Text = "You are a winner";
myLabel.ForeColor = System.Drawing.Color.DarkBlue;
//myLabel.Location = new Point(10,10); // Why doesnt this work?
myLabel.Visible = true;
 
// First method for adding it to the stage:
this.Controls.Add(myLabel);
 
// Second method for adding it to the stage:
//form1.Controls.Add(myLabel);
 
// Third method for adding it to the stage
//Page.Controls.Add(myLabel);
 
// Fourth method for adding it to the stage
//Controls.Add(myLabel);
}

```

Neither of those methods worked. I added the part “CodeBehind” to the top of the page to ensure it knows where to look for the onClick code:

```php

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/Default.aspx.cs" CodeBehind ="~/Default.aspx.cs" Inherits="_Default" EnableViewState="False" %>

```
