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.
-
How to create a javascript file that is separate from the HTML file.
-
How to fire a javascript event onMouseOver from ASP.Net code behind.
-
How to tie the javascript file to the ASP.Net page.
-
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
|