Introduction to XML



ISDV 165

Lecture #3: Syntax and Structure

files are identified by the extension .aspx. The following elements can be used with :

• Directives:

• Code Blocks: …

The code inside the script tag must be declaration, which means it must be placed either in a function or a subroutine, so we can execute this code later.

• Server Controls: or

• Data binding expressions:

• Render code (inline): and

• Server Side Comments:

supports the event driven programming model. It relies on event to trigger certain code. Whenever an event is triggered, uses postback to send the information to the server for processing. When an page is viewed, the following events are generated on the server:

Page_Init

Page_Load

Page_Unload

Postback instruct the browser to post information back to the server for handling the event.

isPostBack test is used to determine whether the form has been visited before or not. It help remember that information the user has input the first time.

The .NET framework provides us with a rich set of classes. One of the important classes used in is the Page class. This class is inherited by any page. Any file with extension .aspx is compiled at run time as Page object and cached in the server memory. The Page class provides us with a wide range of properties and methods. It also provides us with access to objects created from the classes in the namespace Syste.Web such as HttpApplication, HttpSession, HttpRequest and HttpResponse. The methods and properties of these classes are exposed through the following properties (objects) of the Page Class:

• Request object: Contains Information about the current HTTP request.

• Response object: Allows sending the HTTP response data to the client.

• Server object: Provides helper methods to process an HTTP request.

• Application object: Enables sharing of global information across multiple sessions and requests within an application.

• Session object: Maintains information for each user’s session. Variables stored in the Session object are not discarded when the user moves from page to page in the application

The above objects are known as the core objects (intrinsic objects) in . Many other classes are exposed through the Page class such as: HttpCookie, HttpPostFile ,HttpWrite and more.

Examples:

Ex1: inlineCodeEx.aspx

The following example uses inline code:

inline code example

Placing the code inline, make it harder to maintain. Therefore using the script tag and the server control are considered the best way to embed you code.

Ex2: scriptTagEx.aspx

The following example uses the script tag:

script tag example

protected void Page_Load()

{

string strName = "muthana";

Response.Write (string.Format( "Hello {0} ", strName));

Response.Write (string.Format(" The date is is {0}",DateTime.Now.ToLongDateString()));

Response.Write(string.Format(" The time is {0}:{1}:{2}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second));

}

Ex3:serverControlEx.aspx

The following example uses a Server controls:

server conrtol example

protected void Page_Load()

{

string strName = "muthana";

time.Text=string.Format( "Hello {0}, the date and time is is {1} ", strName,DateTime.Now.ToString());

}

Ex4:CodeBehindEx.aspx

When we use the script tag and or the server controls, the code does not have to be in the same page. We can separate the code from the user interface. For example., following is the user interface code in the file codeBehindEx.aspx:

code behind example

The code behind the scene is stored in a file called codeBehindEx.aspx.cs :

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class codeBehindEx : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

string strName = "muthana";

time.Text = string.Format("Hello {0}, the date and time is is {1} ", strName, DateTime.Now.ToString());

}

}

Note:

• Class Page provides objects and event handlers to create web applications.

• The partial keyword in the previous example indicates that the code behind is a partial class which defines the page logic. The graphical representation of the page is generated by the engine the first time the ASPX file is requested. The generated code is represented as another partial class and is stored under the folder :

Following is the code generated by for the example above:

#pragma checksum "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "B2F5407EDAB128D6E23294E1AE60021B"

//------------------------------------------------------------------------------

//

// This code was generated by a tool.

// Runtime Version:2.0.50727.42

//

// Changes to this file may cause incorrect behavior and will be lost if

// the code is regenerated.

//

//------------------------------------------------------------------------------

public partial class codeBehindEx : System.Web.SessionState.IRequiresSessionState {

#line 11 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

protected global::System.Web.UI.WebControls.Label time;

#line default

#line hidden

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

protected global::System.Web.UI.HtmlControls.HtmlForm Form1;

#line default

#line hidden

protected System.Web.Profile.DefaultProfile Profile {

get {

return ((System.Web.Profile.DefaultProfile)(this.Context.Profile));

}

}

protected System.Web.HttpApplication ApplicationInstance {

get {

return ((System.Web.HttpApplication)(this.Context.ApplicationInstance));

}

}

}

namespace ASP {

#line 320 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Web.Profile;

#line default

#line hidden

#line 315 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Text.RegularExpressions;

#line default

#line hidden

#line 317 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Web.Caching;

#line default

#line hidden

#line 313 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Configuration;

#line default

#line hidden

#line 312 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Collections.Specialized;

#line default

#line hidden

#line 11 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

using System.Web.UI.WebControls.WebParts;

#line default

#line hidden

#line 324 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Web.UI.HtmlControls;

#line default

#line hidden

#line 11 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

using System.Web.UI.WebControls;

#line default

#line hidden

#line 321 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Web.UI;

#line default

#line hidden

#line 311 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Collections;

#line default

#line hidden

#line 310 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System;

#line default

#line hidden

#line 319 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Web.Security;

#line default

#line hidden

#line 316 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Web;

#line default

#line hidden

#line 318 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Web.SessionState;

#line default

#line hidden

#line 314 "C:\WINDOWS\\Framework\v2.0.50727\Config\web.config"

using System.Text;

#line default

#line hidden

[System.pilerGlobalScopeAttribute()]

public class codebehindex_aspx : global::codeBehindEx, System.Web.IHttpHandler {

private static bool @__initialized;

private static object @__fileDependencies;

public codebehindex_aspx() {

string[] dependencies;

#line 912304 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx.cs"

((System.Web.UI.Page)(this)).AppRelativeVirtualPath = "~/codeBehindEx.aspx";

#line default

#line hidden

if ((global::ASP.codebehindex_aspx.@__initialized == false)) {

dependencies = new string[2];

dependencies[0] = "~/codeBehindEx.aspx";

dependencies[1] = "~/codeBehindEx.aspx.cs";

global::ASP.codebehindex_aspx.@__fileDependencies = this.GetWrappedFileDependencies(dependencies);

global::ASP.codebehindex_aspx.@__initialized = true;

}

this.Server.ScriptTimeout = 30000000;

}

private global::System.Web.UI.HtmlControls.HtmlTitle @__BuildControl__control3() {

global::System.Web.UI.HtmlControls.HtmlTitle @__ctrl;

#line 7 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl = new global::System.Web.UI.HtmlControls.HtmlTitle();

#line default

#line hidden

System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));

#line 7 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("code behind example"));

#line default

#line hidden

return @__ctrl;

}

private global::System.Web.UI.HtmlControls.HtmlHead @__BuildControl__control2() {

global::System.Web.UI.HtmlControls.HtmlHead @__ctrl;

#line 6 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl = new global::System.Web.UI.HtmlControls.HtmlHead("head");

#line default

#line hidden

global::System.Web.UI.HtmlControls.HtmlTitle @__ctrl1;

#line 6 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl1 = this.@__BuildControl__control3();

#line default

#line hidden

System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));

#line 6 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(@__ctrl1);

#line default

#line hidden

return @__ctrl;

}

private global::System.Web.UI.WebControls.Label @__BuildControltime() {

global::System.Web.UI.WebControls.Label @__ctrl;

#line 11 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl = new global::System.Web.UI.WebControls.Label();

#line default

#line hidden

this.time = @__ctrl;

@__ctrl.ApplyStyleSheetSkin(this);

#line 11 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl.ID = "time";

#line default

#line hidden

return @__ctrl;

}

private global::System.Web.UI.HtmlControls.HtmlForm @__BuildControlForm1() {

global::System.Web.UI.HtmlControls.HtmlForm @__ctrl;

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl = new global::System.Web.UI.HtmlControls.HtmlForm();

#line default

#line hidden

this.Form1 = @__ctrl;

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl.ID = "Form1";

#line default

#line hidden

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl.Method = "post";

#line default

#line hidden

System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n"));

#line default

#line hidden

global::System.Web.UI.WebControls.Label @__ctrl1;

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl1 = this.@__BuildControltime();

#line default

#line hidden

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(@__ctrl1);

#line default

#line hidden

#line 10 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n\t\t"));

#line default

#line hidden

return @__ctrl;

}

private void @__BuildControlTree(codebehindex_aspx @__ctrl) {

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

this.InitializeCulture();

#line default

#line hidden

System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n\r\n\r\n\r\n\r\n"));

#line default

#line hidden

global::System.Web.UI.HtmlControls.HtmlHead @__ctrl1;

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl1 = this.@__BuildControl__control2();

#line default

#line hidden

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(@__ctrl1);

#line default

#line hidden

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n\r\n "));

#line default

#line hidden

global::System.Web.UI.HtmlControls.HtmlForm @__ctrl2;

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__ctrl2 = this.@__BuildControlForm1();

#line default

#line hidden

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(@__ctrl2);

#line default

#line hidden

#line 1 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx"

@__parser.AddParsedSubObject(new System.Web.UI.LiteralControl("\r\n\r\n\r\n\r\n"));

#line default

#line hidden

}

#line 912304 "D:\DFolder\myFolder\courses\ISDV165\basicSyntax\codeBehindEx.aspx.cs"

protected override void FrameworkInitialize() {

base.FrameworkInitialize();

this.@__BuildControlTree(this);

this.AddWrappedFileDependencies(global::ASP.codebehindex_aspx.@__fileDependencies);

this.Request.ValidateInput();

}

#line default

#line hidden

public override int GetTypeHashCode() {

return 811019769;

}

public override void ProcessRequest(System.Web.HttpContext context) {

base.ProcessRequest(context);

}

}

}

After you run the application, the server will send the output to the browser. You can view the code for the output in IE by selecting View->Source. Following is the output code for the above example:

code behind example

Hello muthana, the date and time is is 24/09/2006 10:26:04 PM

Notice that there is a hidden field named __VIEWSTATE that us used by to maintain the state of the controls.

Ex5:

In this example, we will see how to check user login using an HTML file. First we create an HTML file called login.html:

Login Form

User ID

user Password:

This login.html form is submitted to an file called login.aspx:

Login result

And the logic for the code behind is listed below:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class login : System.Web.UI.Page

{

string [] arrID= {"user1","user2","user3"};

string[] arrPAssword = { "pass1", "pass2", "pass3" };

bool blnFound = false;

protected void Page_Load(object sender, EventArgs e)

{

string id=Request.Form["id"];

string pass=Request.Form["pass"];

for(int i=0 ;i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download