More ASP.NET Page model, Session State, Data Binding Today - - PDF document

more asp net
SMART_READER_LITE
LIVE PREVIEW

More ASP.NET Page model, Session State, Data Binding Today - - PDF document

More ASP.NET Page model, Session State, Data Binding Today ASP.NET review Page model Session State Data Binding and List-Bound controls Shopping Cart Demo Review: Example Web Form <%@ Page language="c#"


slide-1
SLIDE 1

1

More ASP.NET

Page model, Session State, Data Binding

Today

  • ASP.NET review
  • Page model
  • Session State
  • Data Binding and List-Bound controls
  • Shopping Cart Demo

Review: Example Web Form

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

File: fortune.aspx

slide-2
SLIDE 2

2

Static HTML

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • Literal content
  • Output exactly as it appears

File: fortune.aspx

Directives - <%@ %>

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • Instructions for IIS
  • Don’t generate any output

File: fortune.aspx

Render Blocks - <% %>

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • Old ASP style
  • Generate output programmatically

File: fortune.aspx

slide-3
SLIDE 3

3

Controls – runat=“server”

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • Define a page element, can set properties in C#
  • Translated to HTML on output

File: fortune.aspx

HTML Controls

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • Almost HTML, except for ‘runat=“server”’
  • Allows you to set properties in C#

File: fortune.aspx

All other controls go inside a form, which is itself a control!

(ASP) Server Controls

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • More powerful than HTML controls

File: fortune.aspx

slide-4
SLIDE 4

4

Custom Controls

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • You create My:Login control
  • Modular development of interactive pages

File: fortune.aspx

Script Tag

<%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <script language=“C#”> private void TellFortune(Object sender, System.EventArgs e) { greeting.Text = “Outlook is bleak…”; } </script> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> …

  • What about your C# code?
  • It could go in a script tag, but we’ll prefer…

File: fortune.aspx

Code-Behind File

<%@ Page language="c#" Codebehind=“foo.aspx.cs“ Inherits=“FortuneApp.fortune“ %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> <form id="Form2" method="post" runat="server"> <My:Login runat=“server” /> <asp:Label id=“greeting” text=“Hello.” runat=“server”/> <br> What is your zodiac sign? <input id=“MonthInput” runat=“server”/> <br> <asp:Button onclick=“TellFortune” runat=“server”/> </form> </html>

  • Put C# code in “Code-Behind” file, foo.aspx.cs
  • Inherits=“FortuneApp.fortune”. What?

File: fortune.aspx

slide-5
SLIDE 5

5

Code-Behind File

using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace FortuneApp { public class fortune : System.Web.UI.Page { protected Label greeting; …

  • FortuneApp: namespace of the application
  • fortune: class that derives from Page

File: fortune.aspx.cs

Declare variables for Controls you want to access. Type and name must match the type and id from the web form.

Page Model

  • Let’s reverse engineer:

– Suppose user requests fortune.aspx. – What does IIS need to do? – How does your code get executed?

Page Request in IIS

Page Object

fortune.aspx fortune.aspx.cs Render() HTML

Client: GET /fortune.aspx HTTP/1.1

Files: Memory:

slide-6
SLIDE 6

6

Page Object

  • What is the type of the page object?
  • We can find out!
  • Demo: MyPage.aspx

Page Object

  • What is the type of the page object?

ASP.MyPage_aspx Lecture4Demos.MyPage System.Web.UI.Page System.Web.UI.TemplateControl System.Web.UI.Control System.Object

  • Note: a Page is a Control

Your Web Form Your Code-Behind Class

Other Relevant Objects

  • A Page has references to other objects:

– Session (type HttpSessionState) – Application (type HttpApplicationState)

Application Session Page Page Session Page Page Session Page Page Page Time

slide-7
SLIDE 7

7

Session State

  • The Page object is created for each request and

then destroyed. For persistent data, stash it in the Session

  • Hashtable like syntax
  • Session[“showDate”] = true;
  • ASP.NET uses cookies to associate Session
  • bject with request
  • Questions: are your preferences stored if:

– You open another browser window? – You quit your browser? – You delete cookies?

Controls

  • Remember, a Page is a Control
  • Controls are trees. Each has these

members:

– HasControls(): returns true if have children – Controls: collection of children – Parent: parent Control in tree – FindControl(string): finds a descendant by name

Control Tree

MyPage Literal Control Label Control Panel Control Literal Control TextBox Control Literal Control TextBox Control Button Control Literal Controls are created for any static HTML in your Web Form Session

slide-8
SLIDE 8

8

Control Demo

  • You can manipulate controls

programmatically

  • Demo: Controls.aspx

Debugging

  • Add trace=“true” to page directive

<%@ Page language="c#" trace="true” … %>

  • Outputs debug info on page:

–Control Tree –Trace messages –Request details –Cookies

  • Demo: Debug.aspx

Data Binding

  • Bind control to a data source
  • Simple Data Binding (Web Form)

– <asp:Label Text=“<%# lotteryNumber %” … > – Write any expression inside <%# %> – Expression should evaluate to string – Binding occurs when control receives a DataBind event

  • Called by you!
slide-9
SLIDE 9

9

Data Binding

  • Programmatic Data Binding (C#)

Control c; Array a; … c.DataSource = a; // set data source c.DataBind(); // bind the data to the control

  • Only for certain controls

List Bound Controls

  • Data Binding is most useful for List Bound

controls, which are designed to take their values from a list:

– DropDownList – Repeater: flexible repeated content – DataGrid: display data in table, e.g. SQL results – DataList: similar to DataGrid, but more options

  • Avoid writing loops!

Data Binding demo

  • DataBind.aspx
slide-10
SLIDE 10

10

FobsUnlimited

  • Put it all together

– More advanced DataGrid usage

  • ButtonColumn
  • OnItemCommand

– Shopping Cart Object – Session State