ASP NET 2 0 What is ASP NET

  • Slides: 23
Download presentation
ASP. NET 2. 0

ASP. NET 2. 0

What is ASP. NET n n n n is a server-side scripting language developed

What is ASP. NET n n n n is a server-side scripting language developed by Microsoft is the next generation of ASP (Active Server Pages), completely new technology, not backward compatible to classic ASP is part of Microsoft. Net framework runs inside IIS (Internet Information Server) version history: ASP. NET, ASP. NET 2. 0, ASP. NET 3. 0 an ASP. NET file has the extension “. aspx” as opposed to “. asp” as in classic ASP the execution and role of “. aspx” files is the same as in other server-side languages like JSP, PHP, etc.

Microsoft. Net Framework n n Microsoft. Net framework is an environment for building, deploying

Microsoft. Net Framework n n Microsoft. Net framework is an environment for building, deploying and running web-based and standalone enterprise applications it contains the following components: n n n programming languages: C#, Visual Basic. Net, J# server technologies and client technologies: ASP. NET, Windows Forms (Windows desktop solutions), Compact Framework (PDA/Mobile solutions) development environments: Visual Studio. Net, Visual Web Developer

Differences between ASP. NET and ASP n n n ASP. NET has a new

Differences between ASP. NET and ASP n n n ASP. NET has a new set of programmable controls, XML-based components, increased performance by running compiled code, event-driven programming ASP. NET uses ADO. NET for accessing databases, supports full Visual Basic, not VBScript, supports C#, C++ and JScript ASP. NET has a large set of HTML controls which can be controlled by scripts ASP. NET contains a new set of programmable objectoriented input controls all ASP. NET objects on a web page can expose events that can be processed by ASP. NET code

Running ASP. NET n for running ASP. NET code only: . NET Framework n

Running ASP. NET n for running ASP. NET code only: . NET Framework n and IIS (Microsoft Internet Information Server) is required n n the principle of running ASP. NET code is the same as in running other server-side scripts like PHP and JSP

First. aspx example <html> <body> <p><%Response. Write(“Hello. Net World!”)%></p> </body> </html> n in classic

First. aspx example <html> <body> <p><%Response. Write(“Hello. Net World!”)%></p> </body> </html> n in classic ASP (and also available in ASP. NET), ASP code is inserted in html code inside the tags “<%…%>”

ASP. NET Server Controls n n n in ASP. NET, ASP code is not

ASP. NET Server Controls n n n in ASP. NET, ASP code is not placed entirely inside the html code, but instead is written in separate executable files server controls are tags understood and run by the server there are three kinds of server controls: n n n HTML Server Controls - Traditional HTML tags Web Server Controls - New ASP. NET tags Validation Server Controls - For input validation

HTML Server Controls n n html elements in ASP. NET files are, by default,

HTML Server Controls n n html elements in ASP. NET files are, by default, treated as text. To make these elements programmable a runat="server" attribute to the html element is required. This attribute indicates that the element should be treated as a server control. The id attribute is added to identify the server control. all html server controls must be within a <form> tag with the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts.

HTML Server Control example <script runat="server"> Sub Page_Load link 1. HRef="http: //www. google. com"

HTML Server Control example <script runat="server"> Sub Page_Load link 1. HRef="http: //www. google. com" End Sub </script> <html> <body> <form runat="server"> <a id="link 1" runat="server">Visit Google!</a> </form> </body> </html>

Web Server Controls n n are special tags understood by the server, but they

Web Server Controls n n are special tags understood by the server, but they do not map to any existing HTML element they require a runat=“server” attribute to work <asp: control_name id="some_id" runat="server" /> ex. : <script runat="server"> Sub submit(Source As Object, e As Event. Args) button 1. Text="You clicked me!" End Sub </script> <html> <body> <form runat="server"> <asp: Button id="button 1" Text="Click me!" runat="server" On. Click="submit"/> </form> </body> </html>

Validation Server Controls n n are used to validate user input; if the user

Validation Server Controls n n are used to validate user input; if the user input does not pass validation, it will display an error message each validation control performs a specific type of validation <asp: control_name id="some_id" runat="server" /> ex. : <html> <body> <form runat="server"> <p>Enter a number from 1 to 100: <asp: Text. Box id="tbox 1" runat="server" /> <asp: Button Text="Submit" runat="server" /> </p> <asp: Range. Validator Control. To. Validate="tbox 1“ Minimum. Value="1" Maximum. Value="100“ Type="Integer“ Text="The value must be from 1 to 100!" runat="server" /> </p> </form> </body></html>

Event handlers n ex. Page_Load event handler: <script runat="server"> Sub Page_Load lbl 1. Text="The

Event handlers n ex. Page_Load event handler: <script runat="server"> Sub Page_Load lbl 1. Text="The date and time is " & now() End Sub </script> <html> <body> <form runat="server"> <h 3><asp: label id="lbl 1" runat="server" /></h 3> </form> </body> </html>

ASP. NET Web Forms n n n all server controls must be defined within

ASP. NET Web Forms n n n all server controls must be defined within a <form> tag and this should have the attribute runat=“server” there can be only one <form runat=“server”> control on a web page the form is always submitted to the page itself regardless of the action attribute the method is set to POST by default if not specified, name and id attribute are automatically assigned by ASP. NET

ASP. NET View. State n ASP. NET maintains a View. State (containing all the

ASP. NET View. State n ASP. NET maintains a View. State (containing all the values of the fields in the form) as a hidden field in the page like this: <input type="hidden" name="__VIEWSTATE" value="d. Dwt. NTI 0 ODU 5 MDE 1 Ozs+ZBCF 2 ryj. Mpe. Vg. Ur. Y 2 e. Tj 79 HNl 4 Q=" /> n n this is useful when the frame is submitted to the server with an error and you have to go back and correct the input data on the form maintaining the View. State is the default setting for Web forms; this can be changed with the directive: <%@ Page Enable. View. State="false" %>

asp: Text. Box <script runat="server"> Sub change(sender As Object, e As Event. Args) lbl

asp: Text. Box <script runat="server"> Sub change(sender As Object, e As Event. Args) lbl 1. Text="You changed text to " & txt 1. Text End Sub </script> <html> <body> <form runat="server"> Enter your name: <asp: Text. Box id="txt 1" runat="server“ text="Hello World!" ontextchanged="change" autopostback="true"/> <p><asp: Label id="lbl 1" runat="server" /></p> </form> </body> </html>

asp: Button <script runat="server"> Sub submit(sender As Object, e As Event. Args) lbl 1.

asp: Button <script runat="server"> Sub submit(sender As Object, e As Event. Args) lbl 1. Text="Your name is " & txt 1. Text End Sub </script> <html> <body> <form runat="server"> Enter your name: <asp: Text. Box id="txt 1" runat="server" /> <asp: Button On. Click="submit" Text="Submit" runat="server" /> <p><asp: Label id="lbl 1" runat="server" /></p> </form> </body> </html>

Data binding controls n n some controls can be bind to a datasource like

Data binding controls n n some controls can be bind to a datasource like a database, an xml file or a script the data binding list controls are: n n n asp: Radio. Button. List asp: Check. Box. List asp: Drop. Down. List asp: List. Box these controls can contain <asp: List. Item> or they can use the items from a data source

Binding an Array. List to a asp: Radio. Button. List <script runat="server"> Sub Page_Load

Binding an Array. List to a asp: Radio. Button. List <script runat="server"> Sub Page_Load if Not Page. Is. Post. Back then dim mycountries=New Array. List mycountries. Add("Norway") mycountries. Add("Sweden") mycountries. Add("France") mycountries. Add("Italy") mycountries. Trim. To. Size() mycountries. Sort() rb. Data. Source=mycountries rb. Data. Bind() end if end sub </script> <html> <body> <form runat="server"> <asp: Radio. Button. List id="rb" runat="server" /> </form> </body></html>

Binding an xml file to a asp: Radio. Button. List <%@ Import Namespace="System. Data"

Binding an xml file to a asp: Radio. Button. List <%@ Import Namespace="System. Data" %> <script runat="server"> sub Page_Load if Not Page. Is. Post. Back then dim mycountries=New Data. Set mycountries. Read. Xml(Map. Path("countries. xml")) rb. Data. Source=mycountries rb. Data. Value. Field="value" rb. Data. Text. Field="text" rb. Data. Bind() end if end sub display. Message(s as Object, e As Event. Args) lbl 1. text="Your favorite country is: " & rb. Selected. Item. Text end sub </script> <html> <body> <form runat="server"> <asp: Radio. Button. List id="rb" runat="server" Auto. Post. Back="True" on. Selected. Index. Changed="display. Message" /> <p><asp: label id="lbl 1" runat="server" /></p> </form> </body></html>

Creating a Database Connection using ADO. NET <%@ Import Namespace="System. Data. Ole. Db" %>

Creating a Database Connection using ADO. NET <%@ Import Namespace="System. Data. Ole. Db" %> <script runat="server"> sub Page_Load dim dbconn=New Ole. Db. Connection("Provider=Microsoft. Jet. OLEDB. 4. 0; data source=" & server. mappath("northwind. mdb")) dbconn. Open() end sub </script>

Creating a Database Command <%@ Import Namespace="System. Data. Ole. Db" %> <script runat="server"> sub

Creating a Database Command <%@ Import Namespace="System. Data. Ole. Db" %> <script runat="server"> sub Page_Load dim dbconn, sql, dbcomm dbconn=New Ole. Db. Connection("Provider=Microsoft. Jet. OLEDB. 4. 0; data source=" & server. mappath("northwind. mdb")) dbconn. Open() sql="SELECT * FROM customers" dbcomm=New Ole. Db. Command(sql, dbconn) end sub </script>

Creating a Data. Reader <%@ Import Namespace="System. Data. Ole. Db" %> <script runat="server"> sub

Creating a Data. Reader <%@ Import Namespace="System. Data. Ole. Db" %> <script runat="server"> sub Page_Load dim dbconn, sql, dbcomm, dbread dbconn=New Ole. Db. Connection("Provider=Microsoft. Jet. OLEDB. 4. 0; data source=" & server. mappath("northwind. mdb")) dbconn. Open() sql="SELECT * FROM customers" dbcomm=New Ole. Db. Command(sql, dbconn) dbread=dbcomm. Execute. Reader() end sub </script>

Binding a Datasource to a control <%@ Import Namespace="System. Data. Ole. Db" %> <script

Binding a Datasource to a control <%@ Import Namespace="System. Data. Ole. Db" %> <script runat="server"> sub Page_Load dim dbconn, sql, dbcomm, dbread dbconn=New Ole. Db. Connection("Provider=Microsoft. Jet. OLEDB. 4. 0; data source=" & server. mappath("northwind. mdb")) dbconn. Open() sql="SELECT * FROM customers" dbcomm=New Ole. Db. Command(sql, dbconn) dbread=dbcomm. Execute. Reader() customers. Data. Source=dbread customers. Data. Bind() dbread. Close() dbconn. Close() end sub </script> <html> <body> </body> <form runat="server"> <asp: Repeater id="customers" runat="server"> <Header. Template> <table border="1" width="100%"> <tr> <th>Companyname</th> <th>Contactname</th> <th>Address</th> <th>City</th> </tr> </Header. Template> <Item. Template> <tr> <td><%#Container. Data. Item("companyname")%></td> <td><%#Container. Data. Item("contactname")%></td> <td><%#Container. Data. Item("address")%></td> <td><%#Container. Data. Item("city")%></td> </tr> </Item. Template> <Footer. Template> </table> </Footer. Template> </form> </asp: Repeater>