Anda di halaman 1dari 12

Intro to ASP.

net
Components of Dynamic Web Applications An optimal dynamic Web page combines both client-side scripting and server-side scripting. Client-side scripting: Enables to develop Web pages that can dynamically respond to user input without having to interact with a Web server. Helps in reducing network traffic. Speeds up the response time of a Web application. Server-side scripting provides dynamic content to users based on the information stored in a remote location. The general rules for using client side scripting and server-side scripting are: Any access to data that resides on the client computer or is dependent on the client computer should be implemented using client-side scripting. Any access to data that resides on a Web server or a remote computer, such as a back-end database should be implemented using server-side scripting. ASP.NET has better language support, a large set of new controls, XML-based components, and better user authentication. ASP.NET provides increased performance by running compiled code. ASP.NET code is not fully backward compatible with ASP. New in 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 Language Support ASP.NET uses ADO.NET. ASP.NET supports full Visual Basic, not VBScript. ASP.NET supports C# (C sharp) and C++. ASP.NET supports JScript. ASP.NET Controls ASP.NET contains a large set of HTML controls. Almost all HTML elements on a page can be defined as ASP.NET control objects that can be controlled by scripts. ASP.NET also contains a new set of object-oriented input controls, like programmable list-boxes and validation controls. A new data grid control supports sorting, data paging, and everything you can expect from a dataset control. Event Aware Controls

1. 2. 3. 4. 5. 6. 7. 8. 9.

All ASP.NET objects on a Web page can expose events that can be processed by ASP.NET code. Load, Click and Change events handled by code makes coding much simpler and much better organized. ASP.NET Components ASP.NET components are heavily based on XML. Like the new AD Rotator, that uses XML to store advertisement information and configuration. User Authentication ASP.NET supports form-based user authentication, cookie management, and automatic redirecting of unauthorized logins. User Accounts and Roles ASP.NET allows user accounts and roles, to give each user (with a given role) access to different server code and executables. High Scalability Much has been done with ASP.NET to provide greater scalability. Server-to-server communication has been greatly enhanced, making it possible to scale an application over several servers. One example of this is the ability to run XML parsers, XSL transformations and even resource hungry session objects on other 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. The result of this is greatly increased performance. 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. No more metabase or registry puzzle. Easy Deployment No more server-restart to deploy or replace compiled code. ASP.NET simply redirects all new requests to the new 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. To overcome this problem, ASP.NET uses 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. Intro to ASP.Net o ASP.NET is a standard HTML file that contains embedded server-side scripts. o ASP.NET provides the following advantages of server-side scripting: Enables to access information from data sources that are stored on a Web server or a computer that is accessible to a Web server Enables to use a set of programming code called templates to create HTML documents Enables to separate HTML design from the data retrieval mechanism

o ASP.NET is built on Microsoft .NET Framework. o Microsoft introduced the .NET Framework to help developers create globally distributed software with Internet functionality and interoperability. o ASP.NET in .NET Framework o The following figure illustrates the elements of an ASP.NET application

Working of an ASP.NET Application After you create an ASP.NET application, the ASP.NET files need to be stored on an IIS server, which is the Web server for the Windows platform.
o The steps to execute an ASP.NET file are as follows: A Web browser sends a request for an ASP.NET file to a Web server by using a URL. The Web server receives the request and retrieves the appropriate ASP.NET file from the disk or memory. The Web server forwards the ASP.NET file to the ASP.NET script engine for processing. The ASP.NET script engine reads the file from top to bottom and executes any server-side script it encounters. The processed ASP.NET file is generated as an HTML document and the ASP.NET script engine sends the HTML page to the Web server. The Web server then sends the HTML page to the client. The Web browser interprets the output and displays it.

Features of ASP.NET
The features of ASP.NET are as follows: Compiled Code Enriched Tool Support Power and Flexibility Simplicity Manageability Scalability Security Extensibility Programming Models in ASP.NET The two types of programming models in ASP.NET are as follows: 1.Web Forms o Enable you to create the user interface for a Web application. o Can be designed and programmed using Rapid Application Development (RAD) tools. o Support a rich set of controls, including user-defined and third party controls that make the application user-friendly. o Web Forms consist of a user interface and application logic that is applied to the components of user interfaces. 2. Web Services o Is defined as a program unit that can be accessed by other applications through the Internet. o Can be called by any application over Hypertext Transfer Protocol (HTTP). o Make use of Extensible Markup Language to transfer data through HTTP.

o Combine the features of component-based development and the Web based applications.

Files Used in an ASP.NET Application


When you create an ASP.NET application, the following files get created: AssemblyInfo.cs Global.asax Web.config WebApplication.vsdisco WebForm1.aspx

Code-Behind Feature of ASP.NET


The code-behind feature enables to divide an ASP.NET page into two files. o .aspx file containing the presentation content. o .cs file containing all the application logic, which is called the code-behind file. The advantages of code-behind feature are as follows: o Makes the code easy to understand and debug, by separating application logic from HTML tags. o Allows the separation of effort between graphic designers who design pages and software engineers who write code. o Eliminates the issues of browser incompatibility by allowing code files to exist on the Web server and supporting Web pages to be compiled on demand. o In the presentation (.aspx) file and the code-behind (.cs) file, ASP.NET automatically generates some lines of code. o In the HTML mode, the first line of an .aspx file contains the following line of code: <%@ Page language="c# Codebehind="WebForm1.aspx.cs"AutoEventWireup="false Inherits="WebApplication1.WebForm1"%> o The @ Page directive is used to specify the page attributes based on which a page will be created. The different page attributes are: 1. Language 2. AutoEventWireup 3. Codebehind 4. Inherits Asp.net Page event lifecyle 1. PreInit 2. Init 3. InitComplete 4. PreLoad 5. Load 6. Control events 7. LoadComplete 8. PreRender 9. PreRenderComplete 10. SaveStateComplete 11. Render

12. Unload Question. Explain ASP.net page events briefly?


ASP.NET - Server Controls

These tags are not standard HTML elements, so a browser will not be able to understand them if they appear on a page. However, when you request an ASP.NET page from a web server, these tags are converted into HTML elements dynamically, so the browser only receives HTML content that it can understand. When a web site is deployed, it is uploaded to a web server, which everyone can access via its URL (Uniform Resource Locator). When the site is hosted on a web server, people can access the site from other machines and browse through the HTML pages. However, if the server has the right software installed, you can then do much more than offer static HTML pages. When you request an HTML page, the server looks up the file and sends it to you. When you request an ASP.NET page (a page with the file extension .aspx), the server looks up the file on its file system and reads the file, and then it performs some processing before sending the resulting page back to you. The performs some processing bit is the magic that makes ASP.NET work. The extra processing that the server performs includes the capability to read an ASP.NET page and convert the server controls in that page into HTML that the browser can understand. Remember, your browser doesnt speak ASP.NET. A web browser is a simple thing that only speaks HTML and possibly JavaScriptit cannot process ASP.NET code. The server reads the ASP.NET code and processes it, converting anything that is ASP.NET-specific to HTML and (as long as the browser supports it) a bit of JavaScript, and sends the freshly generated HTML back to the browser. The process of converting ASP.NET code to HTML is how server controls workyou can happily create pages that contain server controls when you are developing the source .aspx page, yet the browser that requests that page from the web server will only receive HTML and JavaScript.

Server controls are tags that are understood by the server.

There are three kinds


of server controls:

HTML Server Controls - Traditional HTML tags Web Server Controls - New ASP.NET tags Validation Server Controls - For input validation

ASP.NET - HTML Server Controls HTML server controls are HTML tags understood by the server.HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control. The id attribute is added to identify the server control. The id reference can be used to manipulate the server control at run time. Note: 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. ASP.NET - Web Server Controls Web server controls are special ASP.NET tags understood by the server. Like HTML server controls, Web server controls are also created on the server and they require a runat="server" attribute to work. However, Web server controls do not necessarily map to any existing HTML elements and they may represent more complex elements. The syntax for creating a Web server control is: <asp:control_name id="some_id" runat="server" /> All ASP.NET control events happen on the server. For example, the Click event is not raised when you actually click a button. The Click event is not raised until the page containing the Button control is posted back to the server. The ASP.NET Framework is a server-side web application framework. The .NET Framework code that you write executes on the server and not within the web browser. From the perspective of ASP.NET, nothing happens until the page is posted back to the server and can execute within the context of .NET Framework.

ASP.NET - Validation Server Controls Validation server controls are used to validate user-input. If the userinput does not pass validation, it will display an error message to the user. Each validation control 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. You can prevent validation when a button control is clicked by setting the CausesValidation property to false.
The syntax for creating a Validation server control is: <asp:control_name id="some_id" runat="server" />

The ASP.NET Framework contains more than 90 controls. These controls can be divided into seven groups:
1. Standard ControlsEnable you to render standard form elements 2.

3. 4.

5. 6. 7.

such as buttons, input fields, and labels. Validation ControlsEnable you to validate form data before you submit the data to the server. For example, you can use a RequiredFieldValidator control to check whether a user entered a value for a required input field. Rich ControlsEnable you to render things such as calendars, file upload buttons, rotating banner advertisements, and multistep wizards. Data ControlsEnable you to work with data such as database data. For example, you can use these controls to submit new records to a database table or display a list of database records. Navigation ControlsEnable you to display standard navigation elements such as menus, tree views, and bread crumb trails. Login ControlsEnables you to display login, change password, and registration forms. HTML ControlsEnable you to convert any HTML tag into a server-side control.

Standard Controls These controls are the Web equivalents of the tools that you encounter when using Windows applications. Web pages that include these controls have that

standard application feel that were all familiar with, so the process of adding them to pages is quick and simple. Here are some of the most commonly used controls: TextBox control: Used for entering text on a page, commonly seen on order forms on shopping sites, or for logging in to a site. Button control: From submitting an order to changing preferences on a web site, clicking a button on a page normally causes information to be sent to the server, which reacts to that information and displays a result. Label control: Used for displaying simple text in a specified position on a page. The Label control is an easy way to change the text on part of a page in response to user interaction. Hyperlink control: Used for providing hyperlink functionality on a page that enables navigation to other parts of a site, or to other resources on the Internet. Image control: Used for displaying images on a page. The server can change the image that is displayed in the control programmatically in response to user input. DropDown List control: Used for offering the user a list of options to choose from; collapses when not in use to save space. Listbox control: Used for offering a fixed-size list of items to choose from. CheckBox and Radio Button controls: Used for selecting optional extras with either a yes/no or this one out of many style, respectively. Understanding View State The HTTP protocol, the fundamental protocol of the World Wide Web, is a stateless protocol. Each time you request a web page from a website, from the websites perspective, you are a completely new person. The ASP.NET Framework, however, manages to transcend this limitation of the HTTP protocol. For example, if you assign a value to a Label controls Text property, the Label control retains this value across multiple page requests. <body> <form id=form1 runat=server> <div> <asp:Button id=Button1 Text=Add OnClick=Button1_Click Runat=server /> <asp:Label id=Label1 Text=0 Runat=server /> </div>

</form> </body> protected void Button1_Click(object sender, EventArgs e) { Label1.Text = (Int32.Parse(Label1.Text) + 1).ToString(); } Above Example contains a Button control and a Label control. Each time you click the Button control, the value displayed by the Label control is incremented by 1. How does the Label control preserve its value across postbacks to the web server? The ASP.NET Framework uses a trick called View State. the page includes a hidden form field named __VIEWSTATE. This hidden form field contains the value of the Label controls Text property (and the values of any other control properties stored in View State). When the page is posted back to the server, ASP.NET Framework rips apart this string and re-creates the values of all the properties stored in View State. In this way, ASP.NET Framework preserves the state of control properties across postbacks to the web server. By default, View State is enabled for every control in ASP.NET Framework. If you change the background color of a Calendar control, the new background color is remembered across postbacks. If you change the selected item in a DropDownList, the selected item is remembered across postbacks. The values of these properties are automatically stored in View State. View State is a good thing, but sometimes it can be too much of a good thing. The __VIEWSTATE hidden form field can become large. Stuffing too much data into View State can slow down the rendering of a page because the contents of the hidden field must be pushed back and forth between the web server and web browser. Every ASP.NET control includes a property named EnableViewState. If you set this property to the value False, View State is disabled for the control. In that case, the values of the control properties are not remembered across postbacks to the server. Now if we introduce a new label and disable ViewState for this label When you click the button, only the value of the first Label control is incremented past 1.

Using the Page.IsPostBack Property The Page class includes a property called the IsPostBack property, which you can use to detect whether the page has already been posted back to the server. Because of View State, when you initialize a control property, you do not want to initialize the property every time a page loads. Because View State saves the state of control properties across page posts, you typically initialize a control property only once, when the page first loads. Many controls dont work correctly if you reinitialize the properties of the control with each page load. In these cases, you must use the IsPostBack property to detect whether the page has been posted. The Following example illustrates how you can use the Page.IsPostBack property when adding items to a DropDownList control. <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </form> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { ArrayList l1 = new ArrayList(); l1.Add("item1"); l1.Add("item2"); l1.Add("item3"); DropDownList1.DataSource = l1; DropDownList1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Text; }

The code in the Page_Load() event handler executes only once when the page first loads. When you post the page again, the IsPostBack property returns True and the code contained in the Page_Load() handler is skipped. If you remove the IsPostBack check from the Page_Load() method, you get a strange result. The DropDownList always displays its first item as the selected item. Binding the DropDownList to a collection of items reinitializes the DropDownList control. Therefore, you want to bind the DropDownList control only once, when the page first loads. Reference
Web Server Control AdRotator Button Calendar CalendarDay CheckBox CheckBoxList DataGrid DataList DropDownList HyperLink Image ImageButton Label LinkButton ListBox ListItem Literal Panel PlaceHolder RadioButton RadioButtonList BulletedList Repeater Style Table TableCell TableRow TextBox Xml Description Displays a sequence of images Displays a push button Displays a calendar A day in a calendar control Displays a check box Creates a multi-selection check box group Displays fields of a data source in a grid Displays items from a data source by using templates Creates a drop-down list Creates a hyperlink Displays an image Displays a clickable image Displays static content which is programmable (lets you apply styles to its content) Creates a hyperlink button Creates a single- or multi-selection drop-down list Creates an item in a list Displays static content which is programmable(does not let you apply styles to its content) Provides a container for other controls Reserves space for controls added by code Creates a radio button Creates a group of radio buttons Creates a list in bullet format Displays a repeated list of items bound to the control Sets the style of controls Creates a table Creates a table cell Creates a table row Creates a text box Displays an XML file or the results of an XSL transform

Anda mungkin juga menyukai