Monday, 16 June 2014

ASP.NET - Client Side

Filled under:

Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

ASP.NET - Client Side

ASP.Net client side coding has two aspects:
  • Client side scripts: that would run on the browser and in turn would speed up the execution of page. For example, client side data validation which can catch invalid data and warn the user accordingly without making a round trip to the server.
  • Client side source code: that the ASP.NET pages generate. For example, the HTML source code of an ASP.NET page contains a number of hidden fields and automatically injected blocks of JavaScript code, which keeps information like view state or does other jobs to make the page work.

Client Side Scripts:

All ASP.Net server controls allow calling client side code written using JavaScript or VBScript. Some ASP.Net server controls use client side scripting to provide responses to the users without posting back to the server, for example, the validation controls, which we will discuss in due time.
Apart from these scripts the Button control has a property OnClientClick, which allows executing client-side script, when the button is clicked.
The traditional and server HTML controls has the following events that can execute a script when they are raised:
EventDescription
onblurWhen the control loses focus
onfocusWhen the control receives focus
onclickWhen the control is clicked
onchangeWhen the value of the control changes
onkeydownWhen the user presses a key
onkeypressWhen the user presses an alphanumeric key
onkeyupWhen the user releases a key
onmouseoverWhen the user moves the mouse pointer over the control
onserverclickIt raises the ServerClick event of the control, when the control is clicked

Client Side Source Code

We have already discussed that, ASP.NET pages are generally written in two files:
  • The content file or the markup file ( .aspx)
  • The code-behind file
The content file contains the HTML or ASP.Net controls tags and literals to form the structure of the page and the code behind file contains the class definition. At run time, the content file is parsed and transformed into a page class.
This class along with the class definition in the code file and some other system generated code make the executable code (assembly) that processes all posted data and generates the response and sends it back to the client.
Consider the simple page:
<%@ Page Language="C#" AutoEventWireup="true" 
                       CodeBehind="Default.aspx.cs" 
                       Inherits="clientside._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
      <asp:Button ID="Button1" runat="server" 
        OnClick="Button1_Click" Text="Click" />
    </div>
    <hr />
    <h3><asp:Label ID="Msg" runat="server" Text=""></asp:Label>
    </h3>
    </form>
</body>
</html>
When this page is run on the browser, the View Source option shows the HTML page sent to the browser by the ASP.Net runtime:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
 Untitled Page
</title></head>
<body>
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
value="/wEPDwUKMTU5MTA2ODYwOWRk31NudGDgvhhA7joJum9Qn5RxU2M=" />
</div>
 
<div>
<input type="hidden" name="__EVENTVALIDATION" 
id="__EVENTVALIDATION" 
value="/wEWAwKpjZj0DALs0bLrBgKM54rGBhHsyM61rraxE+KnBTCS8cd1QDJ/"/>
</div>

<div>
<input name="TextBox1" type="text" id="TextBox1" />  
<input type="submit" name="Button1" value="Click" id="Button1" />
</div>

<hr />
<h3><span id="Msg"></span></h3>
</form>
</body>
</html>
Looking closely at the code would reveal that first two <div> tags contain the hidden fields which store the view state and validation information.

0 comments: