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 1
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> File: fortune.aspx • Literal content • Output exactly as it appears 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> File: fortune.aspx • Instructions for IIS • Don’t generate any output 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> File: fortune.aspx • Old ASP style • Generate output programmatically 2
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> File: fortune.aspx • Define a page element, can set properties in C# • Translated to HTML on output HTML Controls <%@ Page language="c#" %> <%@ Register TagPrefix="My" TagName="Login" Src="login.ascx" %> <html> <h1>Fortune Teller</h1> It is now <% =DateTime.Now.ToString() %> All other controls go inside a form, which is itself a control! <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 • Almost HTML, except for ‘runat=“server”’ • Allows you to set properties in C# (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> File: fortune.aspx • More powerful than HTML controls 3
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> File: fortune.aspx • You create My:Login control • Modular development of interactive pages 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() %> … File: fortune.aspx • What about your C# code? • It could go in a script tag, but we’ll prefer… 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> File: fortune.aspx • Put C# code in “Code-Behind” file, foo.aspx.cs • Inherits=“FortuneApp.fortune”. What? 4
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 Declare variables for Controls you want to access. { public class fortune : System.Web.UI.Page Type and name must match the type { and id from the web form. protected Label greeting; … File: fortune.aspx.cs • FortuneApp: namespace of the application • fortune: class that derives from Page 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 Memory: HTML Render() Page Object Client: GET /fortune.aspx HTTP/1.1 Files: fortune.aspx fortune.aspx.cs 5
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? Your Web Form ASP.MyPage_aspx Lecture4Demos.MyPage Your Code-Behind Class System.Web.UI.Page System.Web.UI.TemplateControl System.Web.UI.Control System.Object • Note: a Page is a Control Other Relevant Objects • A Page has references to other objects: – Session (type HttpSessionState) – Application (type HttpApplicationState) Time Application Session Session Page Page Page Page Page Session Page Page 6
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 object 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 Literal Controls are Session created for any MyPage static HTML in your Web Form Literal Control Label Control Panel Control Literal Control Literal Control TextBox Control TextBox Control Button Control 7
Recommend
More recommend