Unit 1
Unit 1
NET
ASP.NET is the next generation ASP, but it's not an upgraded version of ASP. ASP.NET is
an entirely new technology for server-side scripting. ASP.NET is a part of the Microsoft
.NET framework, and a powerful tool for creating dynamic and interactive web pages.
ASP.NET is a server side scripting technology that enables scripts (embedded in web
pages) to be executed by an Internet server
In Above Figure:
ASP.NET AJAX.
New ListView and DataPager Controls.
WCF Support for RSS, JSON, POX and Partial Trust.
LINQ (Language Integrated And Query).
New Web Design Interface.
Multi-targeting Support.
The new assemblies that would be of use to ASP.NET 3.5 developers are as
follows:
Advantage Of ASP.NET:
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls ;
using System.Data.OleDb;
string connStr =
ConfigurationManager.ConnectionStrings[ "connectionstring"].ToString();
public PersonDAL()
{
}
conn.Open();
}
catch
{
throw;
}
finally
{
dCmd.Dispose();
conn.Close();
conn.Dispose();
}
}
}
Business Access Layer (BAL) Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls ;
try
{
return pDAL.Insert(name, address, age);
}
catch
{
throw;
}
finally
{
pDAL = null;
}
}
}
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls ;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
if (!Page.IsValid)
return;
int intResult = 0;
PersonBAL pBAL = new PersonBAL();
// Instantiate the object we have to deal with
string name = txtname.Text;
string address = txtaddress.Text;
int age = Int32.Parse(txtAge.Text);
try
{
intResult = pBAL.Insert(name, address, age);
if (intResult > 0)
lblMessage.Text = "New record inserted successfully.";
else
lblMessage.Text = "FirstName [<b>" + txtname.Text + "</b>]
alredy exists, try another name";
}
catch (Exception ee)
{
lblMessage.Text = ee.Message.ToString();
}
finally
{
pBAL = null;
}
}
}
Presentation Layer
<%@ Page
Language="C#"AutoEventWireup="true"Codevirtual="Default.aspx.cs"Inherits="_Defaul
t"%>
<table class="style1">
<tr>
<td colspan="2">
ADD RECORDS </td>
</tr>
<tr>
<td class="style2">
Name </td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:Text Box>
</td>
</tr>
<tr>
<td class="style2">
 
Address </td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:Text Box>
</td>
</tr>
<tr>
<td class="style3">
Age </td>
<td class="style4">
<asp:TextBox ID="TextBox3" runat="server"></asp:Text Box>
</td>
</tr>
<tr>
<td class="style2">
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Save Record" />
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Page_Init: The server controls are loaded and initialized from the Web form's
view state. it is basically used for initialized the object and control
Page_Load: The server controls are loaded in the page object. View state
information is available at this point, so this is where you put code to change
control settings or display text on the page.
Page_PreRender: The application is about to render the page object.
Page_Unload: The page is unloaded from memory.
Page_Disposed: The page object is released from memory. This is the last
event in the life of a page object.
Page_Error: An unhandled exception occurs.
Page_AbortTransaction: A transaction is aborted.
Page_CommitTransaction: A transaction is accepted.
Page_DataBinding: A server control on the page binds to a data source .
Control in ASP.NET
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 use d 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.
function Submit1_onclick() {
alert("click me");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
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.
.ASPX CODE
<html xmlns="http://www.w3.org/1999/xht ml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 246px; background-color: #66FFFF;">
Name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2"
runat="server" ></asp:TextBox>
<br />
<br />
</div>
</form>
</body>
</html>
.CS CODE
Validation server controls are used to validate user-input. If the user-input does not pass
validation, it will display an error message to the user.
.ASPX CODE
<html xmlns ="http://www.w3.org/1999/xht ml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width: 414px; background-color: #66FFFF;">
Name
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RequiredF ieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Text Box1" ErrorMessage="Blank are not allowed"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<br />
Address
<asp:TextBox ID="TextBox2"
runat="server" ontextchanged="TextBox2_TextChanged"></asp:Text Box>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="click me" />
<br />
<br />
</div>
</form>
</body>
</html>
.CS CODE
Property
A property is a member that provides a flexible mechanism to read, write, or compute
the value of a private field. Properties can be used as if they are public data members,
but they are actually special methods called accessors.
Event
Events are user actions such as key press, clicks, mouse movements, etc., or some
occurrence such as system generated notifications. Applications need to respond to events
when they occur. For example, interrupts. Events are used for inter-process
communication.
The events are declared and raised in a class and associated with the event handlers using
delegates within the same class or some other class. The class containing the event is
used to publish the event. This is called the publisher class. Some other class that
accepts this event is called the subscriber class. Events use the publishe r-
subscriber model.
A publisher is an object that contains the definition of the event and the delegate. The
event-delegate association is also defined in this object. A publisher class object invokes
the event and it is notified to other objects.
A subscriber is an object that accepts the event and provides an event handler. The
delegate in the publisher class invokes the method (event handler) of the subscriber class.
Methods
A method is a code block that contains a series of statements. A program causes the
statements to be executed by calling themethod and specifying any
required method arguments. In C#, every executed instruction is performed in the context of
a method.
When you hear the name .NET, it gives a feeling that it is something to do only with
internet or networked applications. Even though it is true that .NET provides solid
foundation for developing such applications it is possible to create many other types of
applications.
Following list will give you an idea about various types of application that we can develop
on .NET.
ASP.NET Web applications: These include dynamic and dat a driven browser
based applications.
Windows Form based applications: These refer to traditional rich client
applications.
Console applications: These refer to traditional DOS kind of applications like
batch scripts.
Component Libraries: This refers to components that typically encapsulate some
business logic.
Windows Custom Controls: As with traditional ActiveX controls, you can develop
your own windows controls.
Web Custom Controls: The concept of custom controls can be extended to web
applications allowing code reuse and modularization.
Web services: They are “web callable” functionality available via industry
standards like HTTP, XML and SOAP.
By the use of partial Class you can split the definition into multiple classes by using the
partial keyword. When the application is complied, the C# complier will group all the partial
classes together and treat them as a single class. There are a couple of good reasons to
use partial classes. Programmers can work on different parts of a class without needing to
share the same physical file. Also you can separate your application business logic from
the designer-generated code.
State management is the process by which you maintain state and page information over
multiple requests for the same or different pages.
ASP.NET provides various client side state management options like Cookies, Query
Strings (URL), Hidden fields, View State and Control state (ASP.NET 2.0).This stores
information on the client's computer by embedding the information into a Web page, a
uniform resource locator (url), or a cookie. The techniques available to store the state
information at the client computer.
a. View State – Asp .Net uses View State to track the values in the Controls. You can add
custom values to the view state. It is used by the Asp .net page framework to automatically
save the values of the page and of each control just prior to rendering to the page. When
the page is posted, one of the first tasks performed by page processing is to restore view
state.
.Cs Code
Advantages
The main advantage of view state is persisting controls properties without any
programming and memory will not be occupied on the client system or on the server
system.
Disadvantage
The disadvantage can be considered as a lot amount of data transmission between client
and server.
1. Control State – If you create a custom control that requires view state to work
properly, you should use control state to ensure other developers don’t break your
control by disabling view state.
2. Hidden fields – Like view state, hidden fields store data in an HTML form without
displaying it in the user's browser. The data is available only when the form is
processed.
3. Cookies – Cookies store a value in the user's browser that the browser sends with
every page request to the same server. Cookies are the best way to store state
data that must be available for multiple Web pages on a web site.
4. Query Strings - Query strings store values in the URL that are visible to the user.
Use query strings when you want a user to be able to e-mail or instant message
state data with a URL.
Cookies in ASP.NET
A cookie is a small bit of text file that browser creates and stores on your machine (hard
drive). Cookie is a small piece of information stored as a string. Web server sends the
cookie and browser stores it, next time server returns that cookie .Cookies are mostly
used to store the information about the user. Cookies are stores on the client side .To
store data in a cookie is not secure due to it's location at client end. Cookie was introduced
with first version of Netscape navigator (that was 1.0).
Advantages
1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.
3. You can configure cookies to expire when the browser session ends (session
cookies) or they can exist for a specified length of time on the client computer
(persistent cookies).
Disadvantages
1. Users can delete a cookies.
2. Users browser can refuse cookies, so your code has to anticipate that possibility.
3. Cookies exist as plain text on the client machine and they may pose a possible
security risk as anyone can open and tamper with cookies.
.ASPX CODE
<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>rdfdf </legend>
<asp:TextBox ID="Text Box1" runat="server"></asp:Text Box>
<asp:Button ID="Button1" runat="server" Text="Add" Width="70px"
onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="View" Width="84px"
onclick="Button2_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="" Width="138px"></asp:Label>
</fieldset>
</div>
</form>
</body>
</html>
.CS CODE
Query string is used to pass the values or information form one page to anothe r
page.
The query string is a holdover from the ASP days of web programming. You will see this
a lot when you are surfing around the internet. Basically, it is passing information to the
next page with the URL .You can use a query string to submit data back to your page or
to another page through the URL. Query strings provide a simple but limited way of
maintaining some state information.
No server resources required. The query string is contained in the HTTP request for
a specific URL.
Broad support. Almost all browsers and client devices support passing values in a
query string
it is very easy
Security. The information in the query string is directly visible to the user via the
browser user interface. The query values are exposed to the Internet v ia the URL
so in some cases security may be an issue.
Limited capacity. Most browsers and client devices impose a 255-character limit on
URL length
.CS CODE
Our first code part builds a query string for your application and send contents of your
textboxes to second page. Now how to retrieve this values from second page.
.ASPX CODE
State Management is the process by which we maintain session related information and
additional information about the controls and its state. The necessity of state management
arises when multiple users request for the same or different Web Pages of a Web Site.
State management can be accomplished using Client Side options or the Server side
options.
Session State: Its nothing but defined as a period of time shared between the web
application and user. Every user has individual session. Items/Objects can be placed into
the Session which would only define these object for that user. Session contains key
variables which help to identify the related values. This can be thought of as a hash table.
Each user would represent a different key node in the hash identifying unique values. The
Session variables will be clear by the application which can clear it, as well as through the
timeout property in the web config file. Usually the timeout is 20 minutes by default.
ASP.NET allows you to save values using session state, a storage mechanism that is
accessible from all pages requested by a single Web browser session. Therefore, you can
use session state to st ore user-specific information. Session state is similar to application
state, except that it is scoped to the current browser session. If different users are using
your application, each user session has a different session state. In addition, if a user
leaves your application and then returns later after the session timeout period, session
state information is lost and a new session is created for the user. Session state is stored
in the Session key/value dictionary.
Session Variables are stored on the server, can hold any type of data including references,
they are similar to global variables in a windows application and use HTTP cookies to store
a key with which to locate user's session variables .The collection of session variables is
indexed by the name of the variable or by an integer index. Session variables are created
by referring to the session variable by name. You do not have to declare a session variable
or explicitly add it to the collection.
Example:
Application State:
Application State is used to store information which is shared among users of the ASP.Net
web application. Application state is stored in the memory of the windows process which
is processing user requests on the web server. Application state is useful in storing small
amount of often-used data. If application state is used for such data instead of frequent
trips to database, then it increases the response time/performance of the web application.
Application state can be used in similar manner as session state but it should be noted
that many user might be accessing application state simultaneously so any call to
application state object needs to be thread safe. This can be easily achieved in ASP.Net
by using lock keyword on the statements which are accessing application state object. This
lock keyword places a mutually exclusive lock on the statements and only allows a single
thread to access the application state at a time.
Code Sample