AWP Unit 2
AWP Unit 2
Server Controls
The following table contains the server-side controls for the Web
Forms.
LinkButton Click, Command It is used to create a button that looks similar to the
hyperlink.
CheckBoxList SelectedIndexChang It is used to create a group of check boxes that all work
ed together.
HTML Controls
These controls render by the browser. We can also make HTML
controls as server control.
Controls Description
Name
Reset Resets all other HTML form elements on a form to a default value
Button
Submit Automatically POSTs the form data to the specified page listed in the Action
Button attribute in the FORM tag
File Field Places a text field and a Browse button on a form and allows the user to select a
file name from their local machine when the Browse button is clicked
Password An input area on an HTML form, although any characters typed into this field are
Field displayed as asterisks
CheckBox Gives the user a check box that they can select or clear
Radio Used two or more to a form, and allows the user to choose one of the controls
Button
Table Allows you to present information in a tabular format
ListBox Displays a list of items to the user. You can set the size from two or more to
specify how many items you wish show. If there are more items than will fit
within this limit, a scroll bar is automatically added to this control.
Dropdown Displays a list of items to the user, but only one item at a time will appear. The
user can click a down arrow from the side of this control and a list of items will
be displayed.
1.Server Controls
Web Forms provides rich set of server controls. These controls are
objects that run when the page is requested and render markup to
the browser. Some Web server controls are similar to familiar
HTML elements, such as buttons and text boxes. It also provides
controls that we can use to connect to data sources and display
data.
2.Master Pages
It allows us to create a consistent layout for the pages in our
application. This page defines the look and feel and standard
behavior that we want for all of the pages in our application. When
users request the content pages, they merge with the master
page to produce output that combines the layout of the master
page with the content from the content page.
3.Working with Data
In an ASP.NET Web Forms application, we use data-bound controls
to automate the presentation or input of data in web page UI
elements such as tables and text boxes and drop-down lists.
4.Membership
Project's Account folder contains the files that implement the
various parts of membership: registering, logging in, changing a
password, and authorizing access. Additionally, ASP.NET Web
Forms supports OAuth and OpenID. These authentication
enhancements allow users to log into your site using existing
credentials, from such accounts as Facebook, Twitter and Google.
5.Client Script and Client Frameworks
We can enhance the server-based features of ASP.NET by
including client-script functionality in ASP.NET Web Form pages.
We can use client script to provide a richer, more responsive user
interface to the users. We can also use client script to make
asynchronous calls to the Web server while a page is running in
the browser.
6.Routing
We can configure URL routing of our application. A request URL is
simply the URL a user enters into their browser to find a page on
our web site. We use routing to define URLs that are semantically
meaningful to users and that can help with search-engine
optimization (SEO).
7.State Management
ASP.NET Web Forms includes several options that help you
preserve data on both a per-page basis and an application-wide
basis.
8.Security
Developing a secure application is most important aspect of
software development process. ASP.NET Web Forms allow us to
add extensibility points and configuration options that enable us to
customize various security behaviors in the application.
9.Performance
Web Forms provides good performance and allows us to modify
performance related to page and server control processing, state
management, data access, application configuration and loading,
and efficient coding practices.
10. Debugging and Error Handling
We can diagnose problems that occur in our Web Forms
application. Debugging and error handling are well supported
within ASP.NET Web Forms so that our applications compile and
run effectively.
Writing Code
To start with writing a code i.e. dynamic and operational coding
we need to switch to code-behind class. To switch up and down
the code we use View code or View Designer buttons which
appears just above the Solution Explorer window. Another
approach is by double clicking either on .aspx page in the solution
explorer or .aspx.cs page. Here we will be discussing C# code not
the HTML code .
Using the Code-Behind Class
Code-Behind class refers to the code that is written in
separate class file that has extension .aspx.cs or .aspx.vb
depending on the language used.
When we switch to code view we will see the page class for
our webpage.
For example when we create a webpage named
SamplePage.aspx.cs we will see a code behind class looks
like this :
Using System;
Using.System.Collection.Generic;
Using.System.Linq;
Using.System.Web;
Using.System.Web.UI;
Using.System.Web.UI.WebControls;
Public partial class SamplePage: System.Web.UI.Page
{
Protected void Page_Load(object sender , EventArgs e)
{
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebApplication7._Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 225px">
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button id="Button1" runat="server" Text="Click Me"
onclick="Button1_Click1" /></br> </br>
<%-- server control(data submitted to server after click on button)--
%>
<input type="text" name="TextBox2" />
<%--<input type="submit" name="Submit1" value="Click Me" />--%> <
%--//html control--%>
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication7
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
View State:
View State is used to store user data on page at
the time of post back of web page. It does not hold
the controls; it holds the values of controls.
It does not restore the value to control after page
post back.
View State can hold the value on single web page,
if we go to other page using response.redirect
then View State will be null.
Example:
Click on Table>Insert Table>
Default.aspx.cs
namespace WebApplication21
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void setdate_Click(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
}
}
Output:
Property Description
IsPost Returns true if the HTTP data transfer method used by the client is a POST request
Page Provides property-like access to data shared between pages and layout pages
Request Gets the HttpRequest object for the current HTTP request
Server Gets the HttpServerUtility object that provides web-page processing methods
Response Instance of the HttpResponse class; sends HTTP response data to the client. It is fun
equivalent to the ASP intrinsic Response object
Calendar Control:
The calendar control is used to display a calendar
in the browser. This control displays a one month
calendar that allows the user to select dates and
move to the next and previous months. It is a
functionality rich web control which provides
displaying one month at a time, selecting a day, a
week or a month, selecting a range of days
moving from month to month and controlling the
display of the days programmatically.
Syntax:-
<asp:Calendar ID=”Calendar1” runat=”server”>
</asp:Calendar>
Properties Description
Gets or sets the caption for the
Caption
calendar control.
Gets or sets the alignment for
CaptionAlign
the caption.
Gets or sets the number of
CellPadding spaces between the data and
the cell border.
Gets or sets the space between
CellSpacing
cells.
Gets the style properties for the
DayHeaderStyle section that displays the day of
the week.
DayNameFormat Gets or sets format of days of
the week.
Gets the style properties for the
DayStyle
days in the displayed month.
Gets or sets the day of week to
FirstDayOfWeek
display in the first column.
Gets or sets the text for next
NextMonthText month navigation control. The
default value is >.
Gets or sets the format of the
NextPrevFormat next and previous month
navigation control.
Gets the style properties for the
OtherMonthDayS days on the Calendar control
tyle that are not in the displayed
month.
Gets or sets the text for
PrevMonthText previous month navigation
control. The default value is <.
SelectedDate Gets or sets the selected date.
Gets a collection of DateTime
SelectedDates objects representing the
selected dates.
SelectedDayStyl Gets the style properties for the
e selected dates.
Gets or sets the selection mode
that specifies whether the user
SelectionMode
can select a single day, a week
or an entire month.
Gets or sets the text for the
SelectMonthText month selection element in the
selector column.
Gets the style properties for the
SelectorStyle week and month selector
column.
Gets or sets the text displayed
SelectWeekText for the week selection element
in the selector column.
Gets or sets the value indicating
ShowDayHeader whether the heading for the
days of the week is displayed.
Gets or sets the value indicating
ShowGridLines whether the gridlines would be
shown.
ShowNextPrevMo Gets or sets a value indicating
whether next and previous
nth month navigation elements are
shown in the title section.
Gets or sets a value indicating
ShowTitle whether the title section is
displayed.
Gets or sets the format for the
TitleFormat
title section.
Get the style properties of the
Titlestyle title heading for the Calendar
control.
Gets the style properties for
TodayDayStyle today's date on the Calendar
control.
Gets or sets the value for
TodaysDate
today's date.
Gets or sets a value that
indicates whether to render the
UseAccessibleHe table header <th> HTML
ader element for the day headers
instead of the table data <td>
HTML element.
Gets or sets the date that
VisibleDate
specifies the month to display.
Gets the style properties for the
WeekendDayStyl
weekend dates on the Calendar
e
control.
The Calendar control has the following three most
important events that allow the developers to
program the calendar control. They are:
Events Description
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
tab.
Click on Calendar property to change format > click on
apply>OK
Write click on project and create new Image Folder > add
image
Click on Image Button > properties > click on
ImageURL>select img folder>select image >OK
Output
Again add Textbox, Image button and Calendar
namespace WebApplication11
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Calendar Control Video Link:
https://www.google.com/search?q=how+to+use+calendar+control+in+asp.net+c
%23+in+visual+studio+2010&rlz=1C1OPNX_enIN1082IN1082&oq=how+to+use+calen
dar+control+in+asp.net+c
%23+in+visual+studio+2010&gs_lcrp=EgZjaHJvbWUyBggAEEUYOdIBCTIxMDA3ajBqNKg
CALACAQ&sourceid=chrome&ie=UTF-
8#fpstate=ive&vld=cid:a2d868b3,vid:XiXZFvnx8jU,st:63
List Controls
These controls are used to display the list with some list
items in the page. These controls include List Box, Drop
Down List, Check Box List, Radio Button List and Bulleted
List. To add items to the list we have to define elements
between the opening and closing tags of the control.
DropDownList
DropDownList control is used select single option from
multiple listed items. Basic syntax of DropDown List :
<asp:DropDownList ID="DropDownList1"
runat="server">
</asp:DropDownList>
Code:
<body style="background-color:aliceblue;text-align:center;font-
family:'Arial Rounded MT'">
<form id="form1" runat="server">
<div>
<h2> Practical 3 A - AutoPostback</h2>
<asp:Label ID="Label2" runat="server" Text="Name :
"></asp:Label>
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label1" runat="server" Text="Roll no :
"></asp:Label>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label3" runat="server" Text="Class :
"></asp:Label>
<asp:RadioButton ID="RadioButton1" runat="server" Text="FY"
GroupName="class"
AutoPostBack="True" /></asp:RadioButton>
<asp:RadioButton ID="RadioButton2" runat="server" Text="SY"
GroupName="class"
AutoPostBack="True" /></asp:RadioButton>
<asp:RadioButton ID="RadioButton3" runat="server" Text="TY"
GroupName="class"
AutoPostBack="True" /></asp:RadioButton> <br /><br />
<asp:Label ID="Label4" runat="server" Text="Course :
"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged
">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem >IT</asp:ListItem>
<asp:ListItem>CS</asp:ListItem>
<asp:ListItem>Bvoc</asp:ListItem>
</asp:DropDownList><br /><br />
<asp:Button ID="Button1" runat="server" Text="Submit"
onclick="Button1_Click" />
<br /><br />
<asp:Label ID="Label5" runat="server"
Text=""></asp:Label><br /><br />
<asp:Label ID="Label6" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
C# code:
namespace AutoPostBack
{
public partial class AutoPostBack : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s;
if (RadioButton1.Checked == true)
{
s = RadioButton1.Text;
}
else if (RadioButton2.Checked == true)
{
s = RadioButton2.Text;
}
else
{
s = RadioButton3.Text;
}
Label5.Text = Convert.ToString("Class : " + s);
}
protected void DropDownList1_SelectedIndexChanged(object
sender, EventArgs e)
{
Label6.Text = "You have been enrolled in " +
DropDownList1.SelectedItem;
}
}
}
Table Control:
A table Web control creates an HTML table in simple HTML
with the help of the <tr> and <td> tags.
You can use the <table>,<tr>, and <td> tags to create a
table and its rows in HTML. For example, the HTML code in
Listing 7-25 creates a table with two rows and three
columns with their values.
Asp.code :
<body style="background-color:aliceblue">
<center>
<h1>Student Form</h1><hr />
<form id="form1" runat="server">
<div>
<label for="Label1">Student Name : </label>
<asp:TextBox ID="TextBox1" runat="server" /><br /><br />
<label for="Label3">Student ID : </label>
<asp:TextBox ID="TextBox2" runat="server" /><br /><br />
<asp:Label ID="Label3" runat="server" Text="Course Name : "></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /><br />
<asp:Label ID="Label4" runat="server" Text="Date of Birth : "></asp:Label>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /><br />
<asp:Button ID="Button1" runat="server" Text="Submit"
OnClick="Button1_Click"/><br/><br />
<asp:Table ID="Tabel1" runat="server" Border="ActiveBorder">
<asp:TableHeaderRow>
<asp:TableHeaderCell>Student Name</asp:TableHeaderCell>
<asp:TableHeaderCell>Student ID</asp:TableHeaderCell>
<asp:TableHeaderCell>Course Name</asp:TableHeaderCell>
<asp:TableHeaderCell>Date of Birth</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</div>
</form>
</center>
</body>
C# Code:
nameCell.Text = name;
idCell.Text = studentID;
courseCell.Text = coursename;
dob.Text = DOB;
newRow.Cells.Add(nameCell);
newRow.Cells.Add(idCell);
newRow.Cells.Add(courseCell);
newRow.Cells.Add(dob);
Tabel1.Rows.Add(newRow);
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
TextBox3.Text = string.Empty;
TextBox4.Text = string.Empty;
}
VALIDATION CONTROLS:
There are six types of validation controls in ASP.NET
1. Required Field Validation Control
2. Compare Validator Control
3. Range Validator Control
4. Regular Expression Validator Control
5. Custom Validator Control
6. Validation Summary
Insert table>add 5 rows 3 columns
Output: