Lecture 12 - ASP - NET (Part 1)
Lecture 12 - ASP - NET (Part 1)
6 Framework
4 5
Adding a control
1. Click on Toolbox
2. Drag the TextBox
3. Drop it to the form
Similarly drag and
drop a Button and a 1
Label to the form 3
2
Split view
1. Click on Split view
2. Is the design view
3. Is the code view
4. Add a new <br/>
tag 4
1
Change the properties
1. Click on the TextBox
2. Click on Properties
3. Locate the ID property, type the name
Similarly,
- Change the ID of the Button to btnDisplay and its 3
text to Display
- Change the Label ID to lblMsg and its text to “”
Note:
You could also change these properties from the code 2
1
Access to Incoming Form Data
1. Double click on the button Display
2. Go to the code behind
1 3. Code the event
3
The IsPostBack Property
• IsPostBack is aproperty of Page
– Will be true if the current HTTP request has been sent by a
user currently in session
– Will be false if this is the user's first interaction with the
page
• Is most helpful to execute a block of code only when
the user first accesses a given page
– E.g., to populate an ADO.NET DataSet when the user first
accesses an *.aspx file and cache the object for later use
– This can avoid the need to hit the database unnecessarily
(of course, some pages might require that the DataSet
always be updated upon each request, but that is another
issue).
Example of use of IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
// Fill Data only the very first time
// the user comes to this page.
if (!IsPostBack)
{
// Populate Data and cache it!
}
// Use cached Data.
}
Interacting with the Outgoing HTTP
Response
• The Response property of the Page class
– Provides access to an instance of the
HttpResponse type
Main Properties/Method of a Request
Redirecting Users
protected void btnWasteTime_Click(object sender, EventArgs e)
{
Response.Redirect("http://www.facebook.com");
}
THE LIFE CYCLE OF AN ASP.NET WEB
PAGE
The Life Cycle of an ASP.NET Web Page
• Every Web Forms page has a fixed life cycle
– Create the Page object using default constructor
– Then automatically fire a series of events
– By default, the Load event is the place you can
add your custom code
• Beyond the Load event, a given Page is able to
intercept any of the core events
– Will be listed in next page in order
Core events of the Page type
The Error Event
void Page_Error(object sender, EventArgs e)
{
Response.Clear();
Response.Write("I am sorry...I can't find a required file.<br>");
Response.Write($"The error was: <b>{ Server.GetLastError().Message }</b>");
Server.ClearError();
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Load event fired!");
}
protected void Page_Unload(object sender, EventArgs e)
{
// No longer possible to emit data to the HTTP
// response at this point, so we will write to a local file.
System.IO.File.WriteAllText(@"C:\MyLog.txt", "Page unloading!");
}
protected void btnPostback_Click(object sender, EventArgs e)
{
// Nothing happens here. This is just to ensure a
// postback to the page.
}
protected void btnTriggerError_Click(object sender, EventArgs e)
{
System.IO.File.ReadAllText(@"C:\IDontExist.txt");
}
The Role of the Web.config File
• Web Forms Application can leverage an XML-
based configuration file to instruct CLR how to
– Handle binding requests
– Handle assembly probing,
– Handle other runtime details
Core elements of Web.config file
LOADING DATA FROM A DATABASE
USING ENTITY FRAMEWORK
Loading data from a database
• Create a Library Project for Data Access Layer
• Use Entity Framework to load data
• Create new Web Site
• Add the Library Project to the Web Site
• Set the loaded data to a GridView
Create new Library Project
4
Using Entity Framework
2
1
3
4
EF Designer from database
2
Configure the connection string
While creating new Entity Data Model
1. Click on New Connection
2. Select Microsoft SQL Server
3. Click Continue
3
Creating the connection
2
While creating new Connection
1. Select the SQL Server
2. Select appropriate Authentication method
3. Select Database
3 4. Test connection (optional)
5. Click OK
4 5
3
1 4
2
Select the Tables
1
1. Select the tables
2. Click Finish
2
The Entity Diagram
Adding the InventoryRepo class
using System.Collections.Generic;
using System.Linq;
namespace InventoryDAL
{
public class InventoryRepo
{
public IEnumerable<Inventory> GetAll() {
using (var context = new AutoLotEntities()) {
return context.Inventories.ToList<Inventory>();
}
}
}
}
Create new website
1
2
4 5
Adding a GridView
1
3
Make sure that you are in the
Design view (Press Shift + F7 if
you are not)
1. Click on Toolbox
2. Drag the GridView
2
3. Drop it to the form
Setting the Select Method
2
1
1. Select Solution
2. Select the Project
3. Click OK
3
Adding the GetAll() method
using InventoryDAL;
using System;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e){}
public IEnumerable<Inventory> GetAll() {
return new InventoryRepo().GetAll();
}
}
Run and we will see this error
Adding connection string
1
3
2