Part of the
You Can Learn ASP.Net series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated: September 4, 2004
This is part 1 of a two part
article on Setting events and eventhandling in Visual Studio with ASP.Net and
C# codebehind.
How to Set Events and EventHandlers using ASP.Net
How to Set Events and EventHandlers with C# code behind.
There are a couple different ways to create event handlers in ASP.Net, setting
events with the C# scripting language all within ASP.Net HTML and the second is
to use C# code behind to let Visual Studio.Net to automatically create events
and EventHandlers for us.
First we will look at creating the event handlers in the ASP portion of .Net.
Let's get started ...
The first task is to create a project. I have
written other articles on creating projects and solutions that will help you
with that easy task. For this example, I am going to lead you into the famous
"Hello World" of computer programming, just because I think it will show this
process the best and easiest way. In my sample, I have a solution named MyFirstSolution
and a project within that solution named YCLSProj1.
1. Double click on WebForm1.aspx in the Solution Explorer tab.
2. Go to the Toolbox and drag and drop a label control and a Button control
onto the the WebForm1.aspx Design page.
(See the tabs at the bottom of the page for clarification.)
You should have something that resembles the following image.

3. Right Click the button control on the Design form and choose properties
on the popup menu.
The properties tab should be visible.
Set the text of the button control to "Click Me".
Change the ID of the button control to "btnClick".
4. Right Click on the Label control on the Design form and choose properties
on the popup menu.
Set the ID of the label control to "lblHello".
5. Now comes the part where we create the ASP.Net event instance.
Click on the HTML tab of the WebForm page. You should see some HMTL script that
looks like this for the Button control.
<asp:Button id="btnClick" style="Z-INDEX: 102; LEFT: 86px;
POSITION: absolute; TOP: 143px" runat="server" Text="Click
Me"></asp:Button>
You need to add an OnClick event to this code in the asp:Button control.
Add the following code OnClick="Button_Click" before the runat="server"
<asp:Button id="btnClick" style="Z-INDEX: 102; LEFT: 86px;
POSITION: absolute; TOP: 143px"
OnClick="Button_Click" runat="server"
Text="Click Me"></asp:Button>
This will cause something to happen when you click on the Button on your
WebPage. But what will happen? Actually right now nothing will occur, because
we don't have any code or scripting to run when we fire the click event.
6. So the next step is to add some C# scripting.
Between the HTML <Head> statements add the following script.
<script language=C# runat="server">
private void Button_Clicked(object sender, EventArgs e)
{
btnClick.BackColor=System.Drawing.Color.Green;
lblHello.Text = "Hello World";
}
</script>
Some points to be made at this time. The first line <script language=C#
runat="server"> defines the script as C# and it is to be run on the
server. Then there is a method call within the script similar to what you would
see for javascript. When you go to run this script and click the Click me
button, it goes to the button's OnClick event which calls the Button_Clicked
event. Within the method there are two lines
-
btnClick.BackColor=System.Drawing.Color.Green;
Causes the "Click Me" button to turn green.
-
lblHello.Text = "Hello World";
Causes the lblHello label to change it's text value to "Hello World."
7. You have enough information on the form to now Run the project.
Click the Start button (blue arrow). If you typed everything in okay it should
build and run. You should see something similar to the image below.

8. Click the button and see what happens.
The next page shows the page as it will look after you have fired the ASP.Net
OnClick Event. The BtnClick EventHandler will capture the event and cause
changes to occur within the page. Changes that you have specifically coded for
in your C# scripting language. Coming up in the next page is how to handle the
same events using C# code behind to fire the eventhandler.
Go To Page 1 /
2
|