more asp net
play

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#"


  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 1

  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> 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

  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> 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

  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> 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

  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 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

  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? 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

  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 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend