Web Tech Unit4
Web Tech Unit4
The HTML elements are considered as text in ASP.NET file. They cannot be referred as server
side code. These controls can be treated as server control by adding runat=”server” attribute.
The id attribute in the element can be added as reference to the control. All the HTML server
controls are written in the <form> tag.
o The HTML server controls follow the HTML centric object model. It is similar to HTML
o The controls can interact with the Client side scripting
o The migration of the code can be made easy by adding runat=”server attribute
o The abstraction of the HTML tag is similar to the HTML server control
o The controls do not possess any mechanism for identifying capabilities of the client browser
Control List
1) HtmlAnchor control:
The <a> Html element allows the user to create a hyperlink. User can navigate to the page
defined in the link. The Target property is used to display the Web page. The values can
be _blank, _self, _parent, _top. The properties are mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Inner Text It sets or returns the text between the opening and closing tags of
the element
InnerHtml It sets or returns the content between the opening and closing
tags of the element
On Server Click The function to be executed when the link is clicked by the user
a2.HRef=”http://www.yahoo.com”;
a2.Title=”Welcome to yahoo”;
a2.Target=”_blank”;
}
}
Output:
2) HtmlButton control:
The Html Button control is defined using the <button> element. The properties are as mentioned
below:
Property Description
Id It defines a unique id for the control
Attributes It returns the attribute name and value pairs of the element
Inner Html It sets or returns the content between the opening and closing tags of the
element
Inner Text It sets or returns the text between the opening and closing tags of the
element
On Server Click The function to be executed when the button is clicked by the user
Style It is used to set or return the CSS properties applied to the control
Output:
3) HtmlForm control:
Html server controls are written within the Html form control. User can add only one form
control for a web page. The POST method is the default method of the Html form control. The
properties are mentioned below:
Property Description
Action It contains a URL defining where to send the data in the form submitted
Attributes It returns the attribute name and value pairs of the element
Enc Type It is mime type used to encode the content of the form
Inner Html It sets or returns the content between the opening and closing tags of the
element
Inner Text It sets or returns the text between the opening and closing tags of the
element
Style It is used to set or return the CSS properties applied to the control
4) HtmlGeneric control:
The HtmlGeneric control is used to control elements other than Html server control. The
<body>, <div>, <span> and <p> are used as Html generic control. The properties are as
mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Inner Html It sets or returns the content between the opening and closing tags of the
element
Inner Text It sets or returns the text between the opening and closing tags of the
element
Style It is used to set or return the CSS properties applied to the control
Property Description
Attributes It returns the attribute name and value pairs of the element
Border It is the width of the borders around the image
Style It is used to set or return the CSS properties applied to the control
<html xmlns=”http:www.w3.org/1999/xhtml”>
<head run at=”server”>
<title></title>
</head>
<body>
<form id=”form1” runat=”server”>
<div>
<img id=”img1” runat=”server” />
</div>
</form>
</body>
</html>
public partial class _Default : System.Web.UI.Page
{
protected void Page Load( object sender, Event Args e)
{
img1.Src =”Image\\Desert.jpg”;
img1.Alt = “Desert”;
img1.Height=90;
img1.Width=100;
}
}
Output:
The Html Input Button control is used to control <input type=”button”>, <input type=”reset”>
and <input type=”submit”> elements. The properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
On Server Click It is the function name to executed when the button is clicked
Style It is used to set or return the CSS properties applied to the control
The Html Input CheckBox control is used to control as <input type=”checkbox”> element. The
properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Style It is used to set or return the CSS properties applied to the control
8) Html Input File control: The Html Input File control is used to control an
<input type=”file”> element. The properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Disabled It is a Boolean value indicating whether the control can be disabled
Max Length It defines the maximum number of characters allowed in the element
Style It is used to set or return the CSS properties applied to the control
Property Description
Attributes It returns the attribute name and value pairs of the element
10) Html Input Radio Button: The Html Input Radio Button control is used to control an
<input type=”radio”> element. The properties are as mentioned below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Run at C
Style It is used to set or return the CSS properties applied to the control
11) HtmlInputText control: The HtmlInputText control is used to control <input type=”text”>
and <input type=”password”> elements. The properties are stated below:
Property Description
Attributes It returns the attribute name and value pairs of the element
Property Description
Data Text Field It is the data source filed to display the drop down list
Data Value Field It specifies the value selected from the drop down list
Property Description
Attributes It returns the attribute name and value pairs of the element
Property Description
The Drop Down List control is a web server element that creates the drop-down menu and lets
users select a single item from various options. You can add any number of items to the
Drop Down List.
Almost all web pages contain a drop-down list, but it is mostly present in registration forms or
sign-in pages. A common occurrence is “Select your Location”, where you have to select one
location from various available options.
Syntax
</asp:DropDownList>
Explanation
The first line sets the ID of the drop-down list used to set the properties of this particular
element, i.e., a DropDownList. The second line to the fourth line describes the items in the list
where an item with value -1 is visible to the user before clicking on it. The DropDownList starts
and ends with a <asp:DropDownList> tag, similar to HTML.
Properties
DropDownList1.Items.Count
The Count() property provides us with the total number of options or items in the drop-down list.
DropDownList1.Items.Add(“ItemName”)
The Add() property lets us add items in the drop-down list by their names.
DropDownList1.Items.Remove(“ItemName”)
The Remove() property lets us remove items from the drop-down list by their names.
The Insert() property helps us to add new items to the drop-down list at a specific position.
DropDownList1.Items.RemoveAt(int index)
The Remove() property has another form, i.e., when provided with the index (position) of an
item in the drop-down list, it removes the element or the item by its position.
DropDownList1.Items.Clear()
As the name suggests, if we want to replace all the items in the drop-down list or if we want to
clear everything and start afresh, the Clear() property lets us clear the entire drop-down list.
DropDownList1.SelectionItem.Text
This property returns the text of the item selected in the drop-down list and hence is a very
prominent property.
DropDownList1.SelectedIndex
The SelectedIndex property returns the index or the position of the selected item in the drop-
down list. It always starts from 0.
DropDownList1.DataSource
The DataSource property is mainly associated with the Data Table or the Data Set and comes
into action when you need to bind a database or datasheet to the drop-down list.
DropDownList1.DataValueField
The DataValueField property binds the values of the datasheet or database to the drop-down list
which is then visible in the dropdown list.
Item
The Item property provides us with the collection of the items from the drop-down list.
The AutoPostBack property comes with two choices, true or false to enable or disable the
automatic postback. The default AutoPostBack value is false. The “true” value indicates that the
server is automatically notified about any changes in user selection of the items from the drop-
down list.
Unlike the DataValueField, the DataTextField is the text that is visible to the end-user.
For example, DataValueField can be the ID of the employees, stored in the database whereas
DataTextField can be the name of the employees, visible to the user.
Step 1:
Create a new form by specifying its name. Initially, the web form is empty.
Step 2:
Click on the toolbox section on the left and drag a DropDownList item from the menu to the web
form.
Step 3:
To add items to the list, navigate to the properties window and add items to the list. The property
window appears as shown below:
Step 1: Click on the Items (Collection) in the properties window. A new window appears that
looks similar to the image below:
Step 2: Add items to the Members section by clicking on the Add button on the left and set its
properties on the right.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace DropDownListExample
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "")
{
Label1.Text = "Please Select a City";
}
else
Label1.Text = "Your Choice is: " + DropDownList1.SelectedValue;
}
}
}
Files
It is an input controller which is used to upload file to the server. It creates a browse button on the form that pop up a
window to select the file from the local machine.
This is a server side control and ASP.NET provides own tag to create it. The example is given below.
Server renders it as the HTML control and produces the following code to the browser.
This control has its own properties that are tabled below.
Property Description
Example
// WebControls.aspx
Inherits="WebFormsControlls.WebControls" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Browse to Upload File</p>
<asp:FileUpload ID="FileUpload1" runat="server" />
</div>
<p>
<asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_
Click" />
</p>
</form>
<p>
<asp:Label runat="server" ID="FileUploadStatus"></asp:Label>
</p>
</body>
</html>
Code
// WebControls.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFormsControlls
{
public partial class WebControls : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.HtmlControls.HtmlInputButton Submit1;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0)
)
{
string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string SaveLocation = Server.MapPath("upload") + "\\" + fn;
try
{
FileUpload1.PostedFile.SaveAs(SaveLocation);
FileUploadStatus.Text = "The file has been uploaded.";
}
catch (Exception ex)
{
FileUploadStatus.Text = "Error: " + ex.Message;
}
}
else
{
FileUploadStatus.Text = "Please select a file to upload.";
}
}
}
}
Create a directory into the project to store uploaded files as we did in below screen shoot.
Output:
Output:
It displays a successful file uploaded message after uploading as shown in the following screenshot.
The file is stored into upload folder. Look inside the folder, it shows the uploaded file is present.
Download File
ASP.NET provides implicit object Response and its methods to download file from the
server. We can use these methods in our application to add a feature of downloading file
from the server to the local machine.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FileDownloadExample
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filePath = "C:\\Users\\Admi\\Desktop\\abc.txt";
FileInfo file = new FileInfo(filePath);
if (file.Exists)
{
// Clear Rsponse reference
Response.Clear();
// Add header by specifying file name
Response.AddHeader("Content-
Disposition", "attachment; filename=" + file.Name);
// Add header for content length
Response.AddHeader("Content-Length", file.Length.ToString());
// Specify content type
Response.ContentType = "text/plain";
// Clearing flush
Response.Flush();
// Transimiting file
Response.TransmitFile(file.FullName);
Response.End();
}
else Label1.Text = "Requested file is not available to download";
}
}
}
Output:
This application will prompt a window to download the file from the server.
DataGrid
.NET Framework provides DataGrid control to display data on the web page. It was introduced
in .NET 1.0 and now has been deprecated. DataGrid is used to display data in scrollable grid. It
requires data source to populate data in the grid.
It is a server side control and can be dragged from the toolbox to the web form. Data Source for
the DataGrid can be either a DataTable or a database. Let's see an example, how can we create a
DataGrid in our application.
This tutorial contains two examples. One is using the DataTable and second is using the database
to display data into the DataGrid.
DataGrid Example with DataTable
// DataGridExample2.aspx
// DataGridExample2.aspx.cs
using System;
using System.Data;
namespace DataGridExample
{
public partial class DataGridExample2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("ID");
table.Columns.Add("Name");
table.Columns.Add("Email");
table.Rows.Add("101", "Deepak Kumar", "[email protected]");
table.Rows.Add("102", "John", "[email protected]");
table.Rows.Add("103", "Subramanium Swami", "[email protected]");
table.Rows.Add("104", "Abdul Khan", "[email protected]");
DataGrid1.DataSource = table;
DataGrid1.DataBind();
}
}
}
Output:
Create a new form to drag DataGrid upon it. See, as we did in the following screenshot.
After adding, now, open toolbox and drag DataGrid control to the form.
After dragging, initially it looks like the following.
// DataGridExample.aspx
In the CodeBehind file, we have code of database connectivity, and binding fetched record to the
DataGrid.
CodeBehind file
// DataGridExample.aspx.cs
using System;
using System.Data;
using System.Data.SqlClient;
namespace AdoNetExample
{
public partial class DataGridExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection("data source=.; database=student; integrate
d security=SSPI"))
{
SqlDataAdapter sde = new SqlDataAdapter("Select * from student", con);
DataSet ds = new DataSet();
sde.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
}
}
}
Records in SQL Server Table
A student table contains records that we want to display by using the DataGrid. This table contains the following records.
Output:
After executing this application, it fetches records from the SQL server and displays to the web
browser.
The Repeater control is used to display a repeated list of items that are bound to the control. The
Repeater control may be bound to a database table, an XML file, or another list of items.
Repeater is a Data Bind Control. Data Bind Controls are container controls. Data Binding is the
process of creating a link between the data source and the presentation UI to display the data.
ASP .Net provides rich and wide variety of controls, which can be bound to the data.
Repeater has 5 inline template to format it:
1. <HeaderTemplate>
2. <FooterTemplate>
3. <ItemTemplate>
4. <AlternatingItemTemplate>
5. <SeperatorTemplate>
6. <AlternatingItemTemplate>
HeaderTemplate: This template is used for elements that you want to render once before your
ItemTemplate section.
Footer Template: - This template is used for elements that you want to render once after your
ItemTemplate section.
Item Template: This template is used for elements that are rendered once per row of data. It is
used to display records
Alternating Item Template: This template is used for elements that are rendered every second
row of data. This allows you to alternate background colors. It works on even number of records
only.
Seperator Template: It is used for elements to render between each row, such as line breaks.
-> DataBind()
-> ItemCreated -> Event
-> DataBound -> Event
System.Commom.Data namespace
-> DBDataRecord Class
The data in Data Source is bound to Repeater using its DataBind Method. Once the data is bound,
the format of each data item is defined by a template like ItemTemplate.
Default.aspx file code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %
>
<!DOCTYPE html PUBLIC "-
//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link rel="Stylesheet" type="text/css" href="StyleSheet.css" />
<title>Repeater Controls in ASP.NET</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="RepeatInformation" runat="server">
<HeaderTemplate>
<table class="tblcolor">
<tr>
<b>
<td>
Roll No
</td>
<td>
Student Name
</td>
<td>
Total Fees
</td>
</b>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr class="tblrowcolor">
<td>
<%#DataBinder.Eval(Container,"DataItem.RollNo")%>
</td>
<td>
<%#DataBinder.Eval(Container,"DataItem.Name")%>
</td>
<td>
<%#DataBinder.Eval(Container,"DataItem.Fees")%>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td>
<hr />
</td>
<td>
<hr />
</td>
<td>
<hr />
</td>
</tr>
</SeparatorTemplate>
<AlternatingItemTemplate>
<tr>
<td>
<%#DataBinder.Eval(Container,"DataItem.RollNo")%>
</td>
<td>
<%#DataBinder.Eval(Container,"DataItem.Name")%>
</td>
<td>
<%#DataBinder.Eval(Container,"DataItem.Fees")%>
</td>
</tr>
</AlternatingItemTemplate>
<SeparatorTemplate>
<tr>
<td>
<hr />
</td>
<td>
<hr />
</td>
<td>
<hr />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<tr>
<td>
School Records displayed
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
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;
using System.Data.SqlClient;
Additional Resources:
http://www.icwai.org/
http://www.nasbaregistry.org/
Reference:
1. T. A. Powell Complete Reference HTML (Third Edition), TMH, 2002
2. G. Buczek ASP.NET Developers Guide TMH, 2002
3. J. Jaworski Mastering Javascript BPB Publicat ions,19 99
Text Book:
I. Bayross Web Enable Commercial Application Development Using HTML, DHTML ,
Pen CGI, BPB Publications, 2000
2. Deitel & Deitel internet & world wide web How to program, Pearson Education