Adding an Alert event for your Client Side Code

Part of the You Can Learn C# series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated:
January 10, 2005

So that's pretty cool at this point. Finally let's add a little javascript alert commands and see what affect that has on the form.

Comment out the line you just added and add the following code to the ClientSideCode function.

alertConfirm = "Did you enjoy the holidays?\n" 
	+ " OK for yes\n" 
	+ " Cancel for no"; 
if(confirm(alertConfirm)) 
{ 
	alert("That's nice!"); 
} 
else 
{ 
	alert("I'm Sorry."); 
}
When you save, compile and run this code, a form will popup asking you if you enjoyed the holidays. Then when you choose OK or Cancel another popup will display a message.

Hopefully all this worked okay for you. Listed below is the entire javascript function ClientSideCode with all three commands listed.

function ClientSideCode() 
{ 
	//document.bgColor = "#123456"; 
	//document.forms[0].btnSubmit.style.backgroundColor = "#123499"; 
	
	alertConfirm = "Did you enjoy the holidays?\n" 
	+ " OK for yes\n" 
	+ " Cancel for no"; 
	
	if(confirm(alertConfirm)) 
	{ 
		alert("That's nice!"); 
	} 
	else 
	{ 
		alert("I'm Sorry."); 
	} 
}
Then I also included the Page_Load event for your perusal.

private void Page_Load(object sender, System.EventArgs e) 
{ 
	btnSubmit.Attributes.Add("onMouseOver","ClientSideCode();"); 
}
			
So with one line of code in the code behind you are able to easily fire off any javascript function you need to speed up client side performance. Why is it important to have client side code? For a standard ASP.Net form, control's behavior are listed as runat="server". The server that is being requested is the server where that web page is hosted. It could be in the same room as you or on the other side of the world.

So to decrease the amount of time it takes to send a message to the server and back, we like to do some functionality on the client side. Meaning that transactions are occurring on the user's machine. Much faster response times.

This article covered 4 important skills.
  1. How to create a javascript file that is separate from the HTML file.
  2. How to fire a javascript event onMouseOver from ASP.Net code behind.
  3. How to tie the javascript file to the ASP.Net page.
  4. How to access the button properties with javascript.
In conclusion, when writing javascript for your projects, create a separate javascript file. This allows for greater reuse within projects. Object oriented programming means you code once for multiple pages. Then tie that javascript file to your web page in the HTML HEAD section. And finally, with one line of code, in the C# code behind, each control can fire a javascript event.

We hope you enjoyed this article and welcome any feedback.

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