Part of the
You Can Learn ASP.Net series.
By Ken Brown
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.
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
Now we are finished with this example of Setting Events and event handlers in
ASP.Net and with C# code behind.
Drag and drop another Button control onto your Design
Right click the Button control and choose Properties
on the menu.
Change the Text property to Event
Change the ID property to btnEvent
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)
Between the curly braces add the following code.
btnHello.BackColor = Color.Blue;
For reference Color.Blue is a member of System.Drawing.Color
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."
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.
So Run the project and choose the Hello button and see what
happens. Hopefully you will see this image.
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
You should have learned there are two ways to set events.
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.
When you add them to ASP.Net you add C# script
calls to the control's OnClick event.
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
Go To Page 1 /