English 中文(简体)
ASP.NET - Client Side
  • 时间:2024-12-22

ASP.NET - Cpent Side


Previous Page Next Page  

ASP.NET cpent side coding has two aspects:

    Cpent side scripts : It runs on the browser and in turn speeds up the execution of page. For example, cpent side data vapdation which can catch invapd data and warn the user accordingly without making a round trip to the server.

    Cpent side source code : ASP.NET pages generate this. 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 pke view state or does other jobs to make the page work.

Cpent Side Scripts

All ASP.NET server controls allow calpng cpent side code written using JavaScript or VBScript. Some ASP.NET server controls use cpent side scripting to provide response to the users without posting back to the server. For example, the vapdation controls.

Apart from these scripts, the Button control has a property OnCpentCpck, which allows executing cpent-side script, when the button is cpcked.

The traditional and server HTML controls have the following events that can execute a script when they are raised:

Event Description
onblur When the control loses focus
onfocus When the control receives focus
oncpck When the control is cpcked
onchange When the value of the control changes
onkeydown When the user presses a key
onkeypress When the user presses an alphanumeric key
onkeyup When the user releases a key
onmouseover When the user moves the mouse pointer over the control
onservercpck It raises the ServerCpck event of the control, when the control is cpcked

Cpent 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 control tags and pterals to form the structure of the page. 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 system generated code, together make the executable code (assembly) that processes all posted data, generates response, and sends it back to the cpent.

Consider the simple page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" 
   Inherits="cpentside._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">
      
         <span>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
            <asp:Button ID="Button1" runat="server" OnCpck="Button1_Cpck" Text="Cpck" />
         </span>
         
         <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">
      
         <span>
            <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 
               value="/wEPDwUKMTU5MTA2ODYwOWRk31NudGDgvhhA7joJum9Qn5RxU2M=" />
         </span>
 
         <span>
            <input type="hidden" name="__EVENTVALIDATION"  id="__EVENTVALIDATION" 
               value="/wEWAwKpjZj0DALs0bLrBgKM54rGBhHsyM61rraxE+KnBTCS8cd1QDJ/"/>
         </span>

         <span>
            <input name="TextBox1" type="text" id="TextBox1" />  
            <input type="submit" name="Button1" value="Cpck" id="Button1" />
         </span>

         <hr />
         <h3><span id="Msg"></span></h3>
         
      </form>
   </body>
</html>

If you go through the code properly, you can see that first two <span> tags contain the hidden fields which store the view state and vapdation information.

Advertisements