How to Set Events and Event Handlers using C# Code Behind

Part of the You Can Learn ASP.Net series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated:
September 4, 2004

This is a part 2 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.

This is part two on Setting Events and EventHandlers in ASP.Net, you have written all the code and created an OnClick event along with the C# scripting that goes along with it. You click the start button in Visual Studio and if we did it right you will see the image below.

Hello World label after Click Me event fires with ASP.Net Event Handlers

9. You see the button has turned to a green background and the words "Hello World" are above the button.
That shows the solution to writing ASP.Net Eventhandlers and firing events in ASP.Net. But you can do the same thing with C# code behind, and in my opinion it is even easier to accomplish the same task and with less typing. Plus you get to use intellisense to guide you in your typing. We are going to use the same project you currently have opened.

Events and EventHandlers using C# code behind

  1. Drag and drop another Button control onto your Design form
  2. Right click the Button control and choose Properties on the menu.
  3. Change the Text property to Event
  4. Change the ID property to btnEvent
  5. Now in the Design window double click on the button.
    This will open up WebForm1.aspx.cs or the C# code behind page.
    Plus it will take you straight to the button click event.
    You should see something similar to what's below.
    private void btnHello_Click(object sender, System.EventArgs e)
    {
    }
  6. Between the curly braces add the following code.
  7. btnHello.BackColor = Color.Blue;
    For reference Color.Blue is a member of System.Drawing.Color
  8. lblHello.Text = "Hello from Chicago.";
    these two statements will "fire" when you click on the button.
    First the Hello button will turn Blue and second the hello label will have the text "Hello from Chicago."
  9. As you were typing, btnHello. Intellisense should have popped up and provided you with potential correct choices.
    Again making it easier to choose the right section of code, which you don't get if you are trying to do the same
    thing in ASP.Net scripting.
  10. So Run the project and choose the Hello button and see what happens. Hopefully you will see this image.

    Using C# Events and Event Handlers.
  11. Another point to bring up is the event handler within the C# code.
    Look for the box that says "Web Form Designer Generated Code."
    Click on the plus box next to it.
    You should see all the code automatically generated by Visual Studio for you.
    Look for the method "private void InitializeComponent()"
    In that method you should see something like
    this.btnHello.Click += new System.EventHandler(this.btnHello_Click);
    which is your actual event handler already created for you by Visual Studio.
    If you change the name of your event, you also have to change this event handler.
Now we are finished with this example of Setting Events and event handlers in ASP.Net and with C# code behind.
You should have learned there are two ways to set events.
  1. When you add them to ASP.Net you add C# script
    language between the Html Head tags. Then you add javascript like method calls to the control's OnClick event.
  2. Using C# code behind, you double click on the Button control and Visual Studio will take you to the specific spot
    for you to begin adding the code for what you want to occur when that event takes place.
When you break it down like this, event handling in C# is relatively easy and you can decide whether to add your event handlers to your ASP.Net or C# code behind. It works either way.
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