California State University Los Angeles CIS 445 Advanced Visual Basic.NET ASP.NET (Part 1), PhD jwoo5@calstatela.edu California State University, LA Department Computer s Part1 The Fundamentals of ASP.NET Contents Introduction.NET Fundamentals ASP.NET simple syntax ASP.NET page Mulithoming for Hosts on IPv6 1
.NET.NET Microsoft has presented.net solution in June 2000 Want to compete with J2EE Want to occupy the world ASP.NET Better Language Support Programmable Controls Event Driven Programming XML Based Components User Authentication, with Accounts and Roles Higher Scalability Increased Performance - Compiled Code Easier Configuration and Deployment Not Fully ASP Compatible.NET (Cont d).net Mainly web solution Platform Independent Final goal but not now Language Independent Visual Basic.NET, C++.NET, C#.NET, ASP.NET, ADO.NET Component Model Easy to distribute Same as EJB Mulithoming for Hosts on IPv6 2
.NET Fundamentals Fundamentals ASP.NET Controls Contains a large set of HTML controls. Almost all HTML elements on a page can be built Event Aware Controls Load, Click and Change events handled by code makes coding much simpler and much better organized ASP.NET Components are heavily based on XML User Authentication Supports forms-based user authentication, including cookie management and automatic redirecting of unauthorized logins. Can still do custom login page and custom user checking.net (Cont d) Fundamentals User Accounts and Roles Give each user (with a given role) access to different server code and executables. High Scalability Server to server communication has been greatly enhanced, making it possible to scale an application over several servers. Compiled Code The first request for an ASP.NET page on the server will compile the ASP.NET code and keep a cached copy in memory. Increases performance Mulithoming for Hosts on IPv6 3
.NET (Cont d) Fundamentals Easy Configuration Configuration of ASP.NET is done with plain text files. Configuration files can be uploaded or changed while the application is running. No need to restart the server Easy Deployment No more server restart to deploy or replace compiled code Compatibility ASP.NET is not fully compatible with earlier versions of ASP, so most of the old ASP code will need some changes to run under ASP.NET. A new file extension ".aspx" This will make ASP.NET applications able to run side by side with standard ASP applications on the same server..net Framework.NET Framework Need to install.net Framework Frame Class Library (FCL) Infrastructure such as transaction, security EJB container Common Language Runtime (CLR) Compiled code from.net language is Microsoft Intermediate Language (MSIL) Codes in different languages can link each other via MSIL Similar to Java IDL CLR should be on each platfrom Similar to JVM Mulithoming for Hosts on IPv6 4
Web Matrix.NET Framework Tool made for ASP.NET development FREE Download http://www.aspmatrix.com/asp-net/webmatrix/index.aspx http://www.asp.net/webmatrix very easy to use, lightweight, and light on system resources Comparing to Visual Studio.NET Has own server Don t need to connect IIS server Need to install IIS server before installing VS.NET You are in trouble now. Do you want to delete and reinstall VS.NET? ASP.NET ASP.NET ASP.NET Active Server Pages in HTML tags of a ASPX file Its function can be written in many languages Similar to JSP ADO.NET ActiveX Data Object Manage data in DBs Similar to JDBC Mulithoming for Hosts on IPv6 5
Dynamic Page in ASP.NET ASP.NET <% --%> tags The code inside the tags is executed on the server. Reponse.Write Writing something to the HTML output stream. Now() A function returning the servers current date and time To execute ASP.NET Not possible in the lab Need IIS web server (MS Windows OS Enterprise Edition) Or Web Matrix ASP.NET Developing tool (Freeware) Download from http://www.asp.net/webmatrix Dynamic Page in ASP.NET (Cont d) ASP.NET Hello.aspx <html> <body bgcolor="yellow"> <center> <h2>hello W3Schools!</h2> <p><%response.write(now())%></p> </center> </body> </html> Mulithoming for Hosts on IPv6 6
hello.aspx (Lab Exam 1) ASP.NET hello.aspx To test the first ASP.NET code Download hello.aspx from the lecture note site And save it to the hard disk Open it from Web Matrix Run it with F5 Connected to Web Matrix Web server Or copy and paste the code to the Html view of Web Matrix in new ASPX file I will let you know how to do it during the lecture ASP.NET With Classic ASP Impossible to separate executable code from the HTML itself Difficult to read, and difficult to maintain pages ASP.NET has solved it with server controls. Server controls are tags that are understood by the server. HTML Traditional HTML tags Web New ASP.NET tags Validation For input validation Mulithoming for Hosts on IPv6 7
HTML Are HTML tags understood by the server HTML elements in ASP.NET files are, by default, treated as text All HTML server controls must be within a <form> tag with the runat="server" attribute runat="server" By adding the attribute to the HTML element in order to make these elements programmable This attribute indicates to treat the element as a server control the form should be processed on the server the enclosed controls can be accessed by server scripts HTML (Cont d) an HtmlAnchor server control example in an.aspx file. Set the href attribute of the HtmlAnchor control in an event handler an event handler is a subroutine that executes code for a given event The Page_Load event <script runat="server"> Sub Page_Load link1.href="http://www.w3schools.com" End Sub </script> <html> <body> <form runat="server"> <a id="link1" runat="server">visit W3Schools!</a> </form> </body> </html> Mulithoming for Hosts on IPv6 8
htmlcontrol.aspx (Lab Exam 2) To test the Html Server Control Download htmlcontrol.aspx from the lecture note site And save it to the hard disk Open it from Web Matrix Run it with F5 Connected to Web Matrix Web server Or copy and paste the code to the All view of Web Matrix in new ASPX file I will let you know how to do it during the lecture Web Are special ASP.NET tags understood by the server. Require a runat="server" attribute Do not necessarily map to any existing HTML elements May represent more complex elements. The syntax for creating a Web server control is: <asp:control_name id="some_id" runat="server" /> Mulithoming for Hosts on IPv6 9
Web (Cont d) Declare a Button server control in an.aspx file. Create an event handler for the Click event which changes the text on the button: Drag Button from Web Control toolbox And drop it into Design view In html view <html> <body> <form runat="server"> <asp:button id="button1" Text="Click me!" runat="server" /> </form> </body> </html> Web (Cont d) <script runat="server"> Sub submit(source As Object, e As EventArgs) webbutton.text="you clicked me!" End Sub </script> <html> <body> <form runat="server"> <asp:button id= webbutton" Text="Click me!" runat="server" OnClick="submit"/> </form> </body> </html> Mulithoming for Hosts on IPv6 10
webcontrol.aspx (Lab Exam 3) To test the Html Server Control Download webcontrol.aspx from the lecture note site And save it to the hard disk Open it from Web Matrix Run it with F5 Connected to Web Matrix Web server Or copy and paste the code to the All view of Web Matrix in new ASPX file I will let you know how to do it during the lecture Validation Used to validate user-input. If the user-input does not pass validation, it will display an error message to the user. Performs a specific type of validation like validating against a specific value or a range of values By default, page validation is performed when a Button, ImageButton, or LinkButton control is clicked. Prevent validation by setting the CausesValidation property to false. The syntax for creating a Validation server control is: <asp:control_name id="some_id" runat="server" /> Mulithoming for Hosts on IPv6 11
Validation (Cont d) Declare one TextBox, Button, and RangeValidator control If validation fails, the text "The value must be from 1 to 100!" will be displayed in the RangeValidator control: Set the properties of RangeValidator ControlToValidate="tbox1" MinimumValue="1" MaximumValue="100" Type="Integer" EnableClientScript="false" Don t need any client script If true, need a script code Text="The value must be from 1 to 100! Error message If the input is not in the range Validation (Cont d) <html> <body> <form runat="server"> Enter a number from 1 to 100: <asp:textbox id="tbox1" runat="server" /> <br /><br /> <asp:button Text="Submit" runat="server" /> <br /> <asp:rangevalidator ControlToValidate="tbox1" MinimumValue="1" MaximumValue="100" Type="Integer" EnableClientScript="false" Text="The value must be from 1 to 100!" runat="server" /> </form> </body> </html> Mulithoming for Hosts on IPv6 12
validcontrol.aspx (Lab Exam 4) To test the Html Server Control Download validcontrol.aspx from the lecture note site And save it to the hard disk Open it from Web Matrix Run it with F5 Connected to Web Matrix Web server Or copy and paste the code to the All view of Web Matrix in new ASPX file I will let you know how to do it during the lecture Event Event The Page_Load Event Triggered when a page loads, Automatically call the subroutine Page_Load, and execute the code inside it: contains no object references or event arguments <script runat="server"> Sub Page_Load lbl1.text="the date and time is " & now() End Sub </script> <html> <body> <form runat="server"> <h3><asp:label id="lbl1" runat="server" /></h3> </form> </body> </html> Mulithoming for Hosts on IPv6 13
event.aspx (Lab Exam 5) To test the Page_Load event Download event.aspx from the lecture note site And save it to the hard disk Open it from Web Matrix Run it with F5 Connected to Web Matrix Web server Or copy and paste the code to the All view of Web Matrix in new ASPX file I will let you know how to do it during the lecture The Page.IsPostBack Property Event Use it if you want to execute the code in the Page_Load subroutine only the FIRST time the page is loaded If the Page.IsPostBack property is false, the page is loaded for the first time, if it is true, the page is posted back to the server (i.e. from a button click on a form): The example next will write the "The date and time is..." message only the first time the page is loaded. When a user clicks on the Submit button, the submit subroutine will write "Hello World!" to the second label, but the date and time in the first label will not change. Mulithoming for Hosts on IPv6 14
The Page.IsPostBack Property Event <script runat="server"> Sub Page_Load if Not Page.IsPostBack then lbl1.text="the date and time is " & now() end if End Sub Sub submit(s As Object, e As EventArgs) lbl2.text="hello World!" End Sub </script> <html> <body> <form runat="server"> <h3><asp:label id="lbl1" runat="server" /></h3> <h3><asp:label id="lbl2" runat="server" /></h3> <asp:button text="submit" onclick="submit" runat="server" /> </form> </body> </html> postback.aspx (Lab Exam 6) To test the Page.IsPostBack property Download postback.aspx from the lecture note site And save it to the hard disk Open it from Web Matrix Run it with F5 Connected to Web Matrix Web server Or copy and paste the code to the All view of Web Matrix in new ASPX file I will let you know how to do it during the lecture Mulithoming for Hosts on IPv6 15
postback.aspx (Lab Exam 6) Modify the part of the code as follows: Test it after removing Not Sub Page_Load if Page.IsPostBack then lbl1.text="the date and time is " & now() end if End Sub Add Shared Variable Shared by Web browsers (users) Dim Shared count As Integer Sub Submit(s As Object, e As EventArgs) visitlabel.text = "current count is:" & count count = count + 1 End Sub Mulithoming for Hosts on IPv6 16