How to Set Events and Event Handlers using ASP.Net and C#


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.

Drag and Drop a Button control and a Label control onto the Visual Studio Design page.

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.

As the Webform appears before clicking on the Click Me button with the ASP.Net script for Events.

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

Home| About Us | NewsLetters | Contact Us |

Copyright © 2004-2014 You Can Learn Series

You Can Learn Series Home Page You Can Learn Series
You Can Learn Series Home Page Home            About the creators of YouCanLearnSeriesAbout Us      Contact YouCanLearnSeriesContact Us      Site map of YouCanLearnSeriesSite Map      Privacy Policy of You Can Learn SeriesPrivacy Policy       Terms of Use of You Can Learn Series web siteTerms of Use       YouCanLearnSeries RSS feed for C# tips and tutorials     
C# and ASP.Net Programming TipsC# and ASP.Net      SQL Tutorial, tips and tricks for DBA'sSQL       Landscaping Tips for Home GardenersLandscaping       WeightLifting Tips for the Young and OldWeightLifting       Kennos BlogBlog       Good health TipsGoodHealth       Learn about Great Travel DestinationsTravel     Web Business TipsWeb Business            You Can Learn Series' Online StoreStore



You Can Learn C# and ASP.Net SeriesC# and ASP.Net
  Using Client Side Code with C# and ASP.NetClient Side Code
  Using Client Side State Management with C# and ASP.NetClient Side State Management
  Create a C# Class in Visual StudioCreate A Class
  Use A Class in C#Use A Class
  Create a Solution in Visual StudioCreate a Solution
  Create a Project in Visual StudioCreate a Project
  Create an RSS feed for your Web SiteCreate an RSS Feed
  Create an RSS feed for your Web SiteListItemCollection
  Set Events in ASP.NetSet Events ASP.Net
  Set Events in C#Set Events C#
  What is Session State and How to Use it.Session State
  Creating and Using the C# SortedList ObjectSortedList Object
  Stylesheet Tips for Visual Studio .Net and ASP.NetStyleSheet Tricks
  Using C# MethodsUsing Methods
  Pass Objects to MethodsPass Objects to Methods
  Pass Objects from MethodsPass Objects from Methods
  Using Client side State management like ViewState and Hidden fieldsView State
  LDAP, What is Lightweight Directory Access ProtocolWhat is LDAP
  Build an LDAP ReaderBuild an LDAP Reader
  Convert information from LDAP to a Web ServiceLDAP To Web Service
  Question of the dayQuestion of the Day

You Can Learn SQL SeriesSQL
You Can Learn Landscaping and Gardening SeriesLandscape Tips
You Can Learn WeightLifting SeriesWeightLift Tips
Kenno's BlogBlog
You Can Learn Good Health SeriesGood Health Tips
You Can Learn Good Series NewslettersNewsletters
Learn about Great Travel DestinationsTravel
You Can Learn Series Web Business TipsWeb Business Tips
Merchandise Available from You Can Learn SeriesMerchandise

Bill Welter - The Prepared Mind of a Leader




Americas Public Schools