430 likes | 632 Views
ASP.Net Web Applications. Characteristics of a typical data driven web application. Web Server. HTTP Request. ASP / JSP ISAPI / NSAPI …… /……. HTTP Response. ADO / JDBC. HTML Graphics Active-X Java Applets. Database Server. Building dynamic web application. Working with ASP.NET.
E N D
Characteristics of a typical data driven web application Web Server HTTP Request ASP / JSPISAPI / NSAPI…… /…… HTTP Response ADO / JDBC HTML GraphicsActive-XJava Applets DatabaseServer
Building dynamic web application • Working with ASP.NET. • Using Web Controls • Looking behind “The ViewState hidden field” • Using Page.IsPostBack • Using Microsoft ADO.NET to Access Data • Creating a Microsoft ASP.NET Web Application
Working with ASP.NET • Introducing ASP.NET • Creating Web Forms • Adding ASP.NET Code to a Page • Handling Page Events
Introducing ASP.NET ASP.NET Web Server Browser Web Forms Web.config Page1.aspx Code-behind pages Output Cache Internet Page2.aspx machine.config global.asax Database Components XML Data
Creating Web Forms 1/3 Web Matrix
Creating Web Forms 2/3 Visual Studio .Net
Creating Web Forms 3/3 ASP.Net Whidbey IntelliSense Everywhere Asp.Net Whodgey is the Microsoft next generation development tool for Asp.Net Web Applications. It is an improved mixture of Web Matrix and Visual Studio.Net.
Adding ASP.NET Code to a Page 2/2 <%@ Page Language="C#" %> <html> <head> </head> <body> <form runat="server"> <asp:Button id="Button1" runat="server" Text="Button"></asp:Button> <asp:Label id="Label1" runat="server" >Label</asp:Label> <asp:TextBox id="TextBox1" runat="server"></asp:TextBox> <input type=“text” name=“TextBox12”> <%Response.Write (“shalom”)%> </form> </body> </html>
Handling Page Events <%@ Page Language="C#" %> <script runat="server"> void Button1_Click(object sender, EventArgs e) { Label1.Text = TextBox1.Text; } </script> <html> <head> </head> <body> <form runat="server"> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button> Dubble Click on Buttone1
Using Web Controls • What Are Web Controls? • Using Intrinsic Controls • Using Input Validation Controls • Selecting Controls for Applications • Demo
What Are Server Controls? • Runat="server" • Events happen on the server • ViewState saved • Have built-in functionality • Common object model • All have Id and Text attributes • Create browser-specific HTML <asp:Button id="Button1" runat="server" Text="Submit"/>
Types of Server Controls • HTML server controls • Web server controls • Intrinsic controls • Validation controls • Rich controls • List-bound controls • Internet Explorer Web controls
HTML Server Controls • Based on HTML elements • Exist within the System.Web.UI.HtmlControls namespace <input type="text" id="txtName" runat="server" />
Web Server Controls • Exist within theSystem.Web.UI.WebControls namespace Control syntax HTML generated by control <asp:TextBox id="TextBox1" runat="server">Text_to_Display </asp:TextBox> <input name="TextBox1" type="text" value="Text_to_Display" Id="TextBox1"/>
What Is Input Validation? • Verifies that a control value is correctly entered by the user • Blocks the processing of a page until all controls are valid • Avoids spoofing,or the addition ofmalicious code
Client-Side and Server-Side Validation • ASP.NET can create both client-side and server-side validation • Client-side validation • Dependent on browser version • Instant feedback • Reduces postback cycles • Server-side validation • Repeats all client-side validation • Can validate against stored data User Enters Data Error Message Valid? No Client Yes Server Valid? No Yes Web ApplicationProcessed
ASP.NET Validation Controls ASP.NET provides validation controls to: • Compare values • Compare to a custom formula • Compare to a range • Compare to a regular expression pattern • Require a user input • Summarize the validation controls on a page
Selecting the Appropriate Server Control Use HTML Server Controls if: Use Web Server Controls if: You prefer an HTML-like object model You prefer a Visual Basic-like programming model You are working with existing HTML pages and want to quickly add ASP.NET Web page functionality You are writing a page that might be used by a variety of browsers The control will interact with client and serverscript You need specific functionality such as a calendar or ad rotator Bandwidth is limited Bandwidth is not a problem
Looking under the hood “The ViewState hidden field” • Hidden ViewState control of name-value pairs stored in the Web Form • On by default, adjustable at Web Form and control level <%@ Page EnableViewState="False" %> <asp:List id="ListName" EnableViewState="true" runat="server"> <input type="hidden" name="__VIEWSTATE" value="dDwtMTA4MzE0MjEwNTs7Pg==" />
Looking under the hood “The ViewState hidden field” 1/3 • Understanding ViewState via a demo : • Add the following Web Controls a Form • ASP:Text • ASP:Lable • ASP:Button • Double click the Text Box to generate the TextBox1_OnChange Event Code. • Inside the event code write the following code: Label1.Text = System.DateTime.Now.ToLongTimeString();
Looking under the hood “The ViewState hidden field” 2/3 The value of TextBox1, “yosef” is being html delivered to the browser in two places. First place in <Input name=“TextBox1” type=“text”> This value can be changed by the user Second place within the VIEWSTATE hidden field. This value can not be changed. When the html form is being submitted the server automatically response to changes in TextBox1and generate server event.
Looking under the hood “The ViewState hidden field” 3/3 <%@ Page Trace="true" Language="C#" %>
Using Microsoft ADO.NET to Access Data • Displaying Data in the DataGrid Control • Using Templates • Using the Repeater Control • Using Datasets vs. DataReaders
Displaying Data in the DataGrid Control 1. Build a Connected/Disconnected Data Source 2. Bind the Data Grid to your Data Source DataGrid1.DataSource = reader1; Or DataGrid1.DataSource = DataSet1.Tables[0].DefaultView; 3. Tell The Control to generate HTML Datagrid1.DataBind();
The DataAdapter Object Model DataSet DataAdapter SelectCommand UpdateCommand InsertCommand DeleteCommand DataReader Command Command Command Command Connection sp_SELECT sp_UPDATE sp_INSERT sp_DELETE Database
Generating a Dataset • You can generate a dataset… • …through the UI… • Creates a dataset that allows you to access data as an object • …or through code… • …and then fill the DataSet from the DataAdapter(s) DataSet ds = New DataSet() DataAdapter1.Fill(ds) DataAdapter2.Fill(ds)
Property Description DataSource • The DataSet containing the data DataMember • The DataTable in the DataSet DataTextField • The field in the DataTable that is displayed DataValueField • The field in the DataTable that becomes the value of the selected item in the list Displaying DataSet Data in List-Bound Controls • Set the properties • Fill the DataSet, then call the DataBind method DataAdapter1.Fill(ds) lstEmployees.DataBind()
Updating Data 1/3 Using the DataGrid property builder add the Edit, Update, Cencle Buttons/Links
Updating Data 2/3 Click on the Events button (mark with red circle) and duble click inside the Cancel Command. You will see DataGrid1_CancelCommand.Do the same for EditCommand and UpdateCommand void DataGrid1_CancelCommand(object sender, DataGridCommandEventArgs e) { } void DataGrid1_EditCommand(object sender, DataGridCommandEventArgs e) { } void DataGrid1_UpdateCommand(object sender, DataGridCommandEventArgs e) { }
Updating Data 3/3 Click on the Events button (mark with red circle) and duble click inside the Cancel Command. You will see DataGrid1_CancelCommand.Do the same for EditCommand and UpdateCommand void DataGrid1_CancelCommand(object sender, DataGridCommandEventArgs e) { } void DataGrid1_EditCommand(object sender, DataGridCommandEventArgs e) { } void DataGrid1_UpdateCommand(object sender, DataGridCommandEventArgs e) { }
Updating Data 4/3 Click on the Events button (mark with red circle) and duble click inside the Cancel Command. You will see DataGrid1_CancelCommand.Do the same for EditCommand and UpdateCommand void DataGrid1_CancelCommand(object sender, DataGridCommandEventArgs e) { } void DataGrid1_EditCommand(object sender, DataGridCommandEventArgs e) { } void DataGrid1_UpdateCommand(object sender, DataGridCommandEventArgs e) { }
Using Datasets vs. DataReaders • Open conversation
Creating a Microsoft ASP.NET Web Application • Requirements of a Web Application • Sharing Information Between Pages • Securing an ASP.NET Application • Page Output Caching • Application Caching • Demo Samples
Project 2 0f 3 • Building Dynamic Web Application