2PGDCA4 (B) - Programming With ASP. Net Programming With ASP. Net Programming With ASP. Net
2PGDCA4 (B) - Programming With ASP. Net Programming With ASP. Net Programming With ASP. Net
Next, give the appropriate name, location, and the solution name for the ASP.NET Core
application. In this example, we will give the name "MyFirstCoreWebApp" and click on
the Create button, as shown below.
1.3 Configure Project
Next, select appropriate ASP.NET Core Web application template such as Empty, API,
Web Application, MVC, etc. Here, we want to create a web application, so select the
Web Application template. We don't want HTTPS at this point, so unchec
uncheckk Configure for
HTTPS checkbox, as shown below. Also, make sure you have selected the appropriate
.NET Core and ASP.NET Core versions. Click on the Create button to create a project.
This will create a new ASP.NET Core web project in Visual Studio 2019, as shown
below. Wait for some time till Visual Studio restores the packages in the project.
Restoring process means Visual Studio will automatically add, update or delete
configured dependencies as NuGet packages in the project.
1.5 Create ASP.NET Core 3 Application
To run this web application, click on IIS Express or press Ctrl + F5. This will open the
browser and display the following result.
2. Create a Multiform Web Project
Microsoft recommends the single project method for small-sized to medium-sized Web
applications. Visual Studio .NET directly supports this method. The whole Web
application is built as a single ASP.NET Web application project. Each team member
downloads a copy of the entire project to a development computer, where he or she
develops part of the application. You should use source control software to coordinate the
work of the team members on the files that make up the project.
If you separate the Web application into multiple Visual Studio .NET projects, you may
find it easier to manage the development of large Web applications. You can separate a
large development project into smaller projects, which you can manage and build
separately. Your team can work on separate parts of a Web application by working on
separate projects. You should still use source control software to coordinate work on
project files. Visual Studio .NET does not directly support this method because Visual
Studio .NET Web projects are always created in their own IIS application root directories
with their own assemblies. IIS Web applications cannot span multiple IIS application root
directories.
Additionally, you cannot use resources in one root directory from other root directories in
Visual Studio .NET. It is difficult to create a single application from multiple projects if
these applications cannot share resources. To resolve these problems, you can use the
procedure in the Make Multiple Visual Studio .NET Projects Participate in the Same Web
Application section so that multiple Visual Studio projects share the same IIS application
root directory.
Visual Studio .NET does not directly support this method. You must perform
additional steps so that separate Visual Studio .NET projects participate in the same
Web application.
Assemblies that access each other's resources must set references to each other.
Visual Studio .NET does not allow circular references.
This method is not ideal for small Web applications. For small Web applications, it
is more complex to manage multiple projects than to manage a single project.
2.3 Make Multiple Visual Studio .NET Projects Participate in the Same
Web Application
To make the Visual Studio .NET projects participate in the same Web application, the
projects must share the same IIS application root directory. Visual Studio .NET creates
Web projects in their own application root directories. Therefore, you must configure this
separately.
To make the Visual Studio .NET projects participate in the same Web application,
you must complete four main steps:
1. Create the main project in a directory that is the root directory for the whole
application.
2. Create the child projects in subdirectories of the root directory in the same Visual
Studio .NET solution.
3. Remove the Web applications that Visual Studio .NET creates for the child projects
through IIS.
4. NOTE: Because Visual Studio .NET does not allow you to create or to work with
multiple Web applications in the same physical directory, you must create the
various projects that make up your application in separate directories.
5. To deploy the projects to individual development computers, copy the application
directory structure to those computers, and then create an IIS application root
directory for the main project directory.
To create the IIS application root project for the Web application, follow these steps:
1. Start Visual Studio .NET.
2. On the File menu, point to New, and then click Project.
3. In the New Project dialog box, click the language that you want to use under Project
Types, and then click ASP.NET Web Application under Templates.
4. In the Location text box, replace the WebApplication# default name with MainWeb.
If you are using the local server, you can leave the server name as http://localhost.
The Location box should then appear as follows:
http://localhost/MainWeb
To remove the IIS applications that correspond to the child projects, follow these steps:
1. Click Start, point to Programs (or All Programs in Windows XP), point
to Administrative Tools, and then click Internet Services Manager.
2. Locate your main Web application and the child Web projects that you want to
remove.
3. For each child project, right-click the Web application node, and then
click Properties.
4. On the Directory tab, click Remove, and then click OK.
IMPORTANT: Do not click Delete. This can permanently delete the corresponding
file directory and your project files.
After you configure the child projects to share a common IIS application root directory,
you can share resources between projects in the solution. For example, you can drag a
user control from a shared resources project into an .aspx file in another project. Note that
you can only do this after you configure the projects to share a common IIS application
root. Visual Studio .NET does not allow you to share resources if the projects are still in
separate IIS application root directories.
To add references to the main Web application for all of the child projects, follow these
steps.
1. In Solution Explorer of the main Web application, right-click References, and then
click Add Reference.
2. In the Add Reference dialog box, click the Projects tab.
3. Select the child projects, and then click OK.
After you set references to the child projects, when you build the solution, the child
project assemblies are copied to the Bin directory of the main application. You can then
debug and use the components that are defined in the child projects.
To deploy the multi-project Web application to a development computer, you must copy
the main (root) project that represents the root of the Web application, as well as any
child applications that you want to work on. You must make the main project directory
the root of an IIS Web application.
There are many options for deployment, and it is beyond the scope of this article to
describe each option. For example, you can deploy built versions of some of the source
files in large Web applications to avoid bringing in many source files. Any references to
local projects must be project references. If you want to drag ASP.NET user controls
from another project onto a Web Form, the project that contains the user control must
exist on your computer and in your solution.
For smaller ASP.NET Web applications, you can combine all of the projects. A good
way to do this is to click Copy Project on the Project menu in Visual Studio .NET. You
can also copy the project files to a computer.
1. Copy the whole directory structure that you created in the previous steps to a
development computer.
2. Make the directory that contains the root project an IIS application root. The easiest
way to do this is to use the File System Explorer (if you are working with the NTFS
file system) as follows:
1. In the File System Explorer, locate the root directory of the Web application.
2. Right-click the directory, and then click Properties.
3. In the Properties dialog box, click the Web Sharing tab, and then click Share
this folder.
4. In the Alias box, type the name of the Web application, and then click OK.
5. Click OK to close the Properties dialog box.
3. Make sure that all of the user identities who will be using the Web application (such
as the IUSR account) have access to the directories.
4. Build the solution.
3. Form Validation
3.1 What is Validation?
Validation is basically the act of comparing something to a given set of rules and
determining if it satisfies the criteria those rules represent. In this case, the something that
we are tring to validate is the input that a visitor to our site has entered into a web form.
There are a number of reasons why we'd want to do this. Some basic examples are:
There are any number of explanations why one of the above might occur. Perhaps a
spider or web-bot has come across our form and tried to submit it without entering any
data, maybe the user entered a item that doesn't really exist (ie. Feb. 29, 2002), or
maybe they simply made a typo (ie. forgot to enter one of the digits in their phone
number). Whatever the reason, the best course of action is usually easiest to determine
if you know there is a problem while the user is still available to fix it.
Validations can be performed on the server side or on the client side (web browser). The
user input validation take place on the Server Side during a post back session is called
Server Side Validation and the user input validation take place on the Client Side (web
browser) is called Client Side Validation. Client Side Validation does not require a
postback. If the user request requires server resources to validate the user input, you
should use Server Side Validation. If the user request does not require any server
resources to validate the input, you can use Client Side Validation.
For example, if the user enters an invalid email format, you can show an error message
immediately before the user move to the next field, so the user can correct every field
before they submit the form.
Mostly the Client Side Validation depends on the JavaScript Language, so if users turn
JavaScript off, it can easily bypass and submit dangerous input to the server. So the
Client Side Validation cannot protect your application from malicious attacks on your
server resources and databases.
A validation control is used to validate data from the input control. The error message
will be displayed if the data is incorrect. The syntax for creating validation control in
ASP.NET control is as shown below:
Required Field Validator control is used to make the input field a required field in the
application. The control raises an error if the input value is not entered by the user. By
default, the initial value is an empty string.
Property Description
ErrorMessgae It is the text displayed when the property is not set for the
control
Output
Property Description
Output
The Compare Validator is used to compare the values of one input control to the another
input control or to a fixed value. If the input control value is empty then no validation
occurs. The properties of the control are as follows:
Property Description
Type It specifies the data type of the values to compare. The types
are currency, Date, Double, Integer, String
Output
The Regular Expression validator control is used to ensure that an input value matches a
specifies pattern. It is useful for checking email address, phone numbers, zip codes, etc.
The validation will not fail is the input control is empty. The properties of the control are
as mentioned below:
Property Description
The custom validator allows the user to write a method to handle the validation of the
value entered. It is used to compare the users input to a value in the database or the
arithmetic validations for the controls. The properties for the control are as mentioned
below:
Property Description
Output
The Validation Summary control is used to display the summary of all validation errors
in a web page. The error message displayed in this control is specified by the Error
Message property for every validation control. The summary can be displayed as a list,
bulleted list, single paragraph depending on the DisplayMode property. The properties
for the control are as follows:
Property Description
DisplayMode It defines how the summary will be displayed. The values are:
Output
5. AdRotator Control
The AdRotator control is used to display a sequence of advertisement images. It uses an
external XML file to store the ad information. The XML file consists of several tags for
designing the content in an application. The XML file consists of <Advertisements> tag
at the start and end of the tags added. There are several <Ad> tags defined inside the
<Advertisements> tag.
The list of elements that can be added in the <Ad> tag are as mentioned below:
Element Description
Property Description
BorderColor It gets or sets the border color of the Web Server control
CssClass It gets or sets the CSS class rendered by the Web Server
control
DataSource It gets or sets the object from which the data bound control
retrieves the list of data items
Enabled It gets or sets the value indicating the Web Server control
is enabled
ImageUrlField It gets or sets the custom data field to use instead of the
ImageUrl attribute
ItemType It gets or sets the name of the data type for data binding
NavigateUrlField It gets or sets the custom data field to use instead of the
NavigateUrl attribute
TabIndex It gets or sets the tab index of the Web Server control
Events Description
AdCreated It occurs once during the round trip to the server after the
creation of the control
CallingDataMethods It occurs when data methods are being called by the user
<Advertisments>
<Ad>
<ImageUrl>Tulips.jpg</ImageUrl>
<NavigateUrl>http://www.gmail.com</NavigateUrl>
<AlternateText>Gmail</AlternateText>
<Keyword>Site</Keyword>
</Ad>
<Ad>
<ImageUrl>LightHouse.jpg</ImageUrl>
<NavigateUrl>http://www.google.com</NavigateUrl>
<AlternateText>Google</AlternateText>
<Keyword>Site</Keyword>
</Ad>
</Advertisments>
6. Calendar control
Calendar control is used to display a calendar in the browser. It displays a one month
calendar allowing user to select dates and move to the different months. Some of the
properties of the calendar control are as mentioned below:
Property Description
BindingContainer It gets the control that contains the controls data binding
BorderWidth It gets or sets the border width of the Web Server control
Caption It gets or sets the text value rendered as caption for the
calendar
CellPadding It gets or sets the space between the cell contents and cell
border
Font It gets the font properties associated with the Web Server
control
PrevMonthText It gets or sets the text displayed for the previous month
navigation control
SelectMonthText It gets or sets the text displayed for the month selection
element
TabIndex It gets or sets the tab index of the Web Server control
WeekendDayStyle It gets or sets the style properties for the weekend date
on the control
Some of the events related to the calendar control are as mentioned below:
Events Description
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title></title>
</head>
<body>
<form is=”form1” runat=”server”>
<div>
<asp:Calender ID=”Calendar1” runat=”server”
DayNameFormat=”Full” BorderColor=”Black”
onselectionchanged=”Calendar1_SelectionChanged”>
<WeekendDayStyle BackColor=”Brown” ForeColor=”Red” />
<DayHeaderStyle ForeColor=”Aqua”/>
<TodayDayStyle BackColor=”PaleGreen” />
</asp:Calendar><br/>
<asp:TextBox ID=”TextBox1” runat=”server”
Width=”200px”></asp:TextBox>
</div>
</form>
</body>
</html>
Many web applications such as proxy web servers and multiple search engines are
required to access the HTML pages of other websites. The
classes WebClient, WebRequest, and WebResponse are usually used to perform these
requirements in ASP.NET.
On the other hand, a Web Browser control is used in a Windows Forms application to
browse web pages and other browser-enabled documents. Web Browser provides many
events to track data processes, and many properties and methods to access and create new
contents on the HTML element level.
ADO.NET provides a Data Set class that can be used to create Data Set object. It contains
constructors and methods to perform data related operations.
Constructor Description
Properties Description
DefaultViewManager It is used to get a custom view of the data contained in the DataSet
to allow filtering and searching.
HasErrors It is used to check whether there are errors in any of the DataTable
objects within this DataSet.
Method Description
Copy() It is used to copy both the structure and data for this
DataSet.
CreateDataReader(DataTable[]) It returns a DataTableReader with one result set per
DataTable.
ReadXml(XmlReader, It is used to read XML schema and data into the DataSet
XmlReadMode) using the specified XmlReader and XmlReadMode.
Example:
Here, in this example, we are implementing DataSet and displaying data into a gridview.
Create a web form and drag a gridview from the toolbox to the form. We can find it
inside the data category.
// DataSetDemo.aspx
CodeBehind
// DataSetDemo.aspx.cs
using System;
using System.Data.SqlClient;
using System.Data;
namespace DataSetExample
{
public partial class DataSetDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("data source=.; database=stu
dent; integrated security=SSPI"))
{
SqlDataAdapter sde = new SqlDataAdapter("Select * from student", con);
DataSet ds = new DataSet();
sde.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
9. ADO.NET Data Adapter
The Data Adapter works as a bridge between a Data Set and a data source to retrieve
data. Data Adapter is a class that represents a set of SQL commands and a database
connection. It can be used to fill the Data Set and update the data source.
Constructors Description
9.3 Methods
Method Description
Example
// DataSetDemo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataSetDemo.aspx.
cs"
Inherits="DataSetExample.DataSetDemo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" CellPadding="3" BackColor="#DE
BA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2"
>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-
Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-
Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
</form>
</body> </html>
Code Behind
using System;
using System.Data.SqlClient;
using System.Data;
namespace DataSetExample
{
public partial class DataSetDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("data source=.; database=stud
ent; integrated security=SSPI"))
{
SqlDataAdapter sde = new SqlDataAdapter("Select * from student", con);
DataSet ds = new DataSet();
sde.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
}
Output:
References:
https://support.microsoft.com/en-in/help/307467/how-to-create-an-asp-net-
application-from-multiple-projects-for-team-d
http://www.wideskills.com/aspnet/validation-controls
https://www.javatpoint.com/ado-net-dataadapter