0% found this document useful (0 votes)
5 views

Asp.net unit-4

The document discusses various ASP.NET data-bound controls, including Repeater, DataList, GridView, and FormView, highlighting their uses and properties. It explains data source controls such as SqlDataSource, ObjectDataSource, and LinqDataSource, detailing how they connect to databases and manage data operations. Additionally, it covers the DataList control's template fields and events, providing examples of how to implement these controls in web applications.

Uploaded by

meghanam003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Asp.net unit-4

The document discusses various ASP.NET data-bound controls, including Repeater, DataList, GridView, and FormView, highlighting their uses and properties. It explains data source controls such as SqlDataSource, ObjectDataSource, and LinqDataSource, detailing how they connect to databases and manage data operations. Additionally, it covers the DataList control's template fields and events, providing examples of how to implement these controls in web applications.

Uploaded by

meghanam003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

aCntie

Presenting Data Using ASPNet Bound Cntros Dets


Ven
Repeater and its uses, Data ligt controd, data M A
UNIT importance, form view detail's view, lit ven urntrs
g k
mettts, y
IV to user controls, registeringuser contrd paopertses l
loading user controls, master poges

AsP.NEr 4.1.1 DataSource Cotrols


A1PRESENTING DATA UsING
BoUND CONTROLS Q2. Write about data source controls i
asp.net.
bound controls.
at Wite about asp.net data
eze
A data surce crtrd iteras w
Data-bound Web server controls are controls bound controls and hides the coen t e birng
source control to make processes. These are the toos tz yiie zas
Aat can be bound to a data
data in your Web bound controls and s r e i n
t easy to display and modify server controls are the data zi
agplication. Data-bound Web operations like insertions, deins, s g
other ASPNET updates.
omposite controls that combine
TextBox controls. Each data source cortro es2 p r z
Web controls, such as Label and
Liocre
into a single layout. data províder-relational databses.
as a or custom cdasses and hebs in:
For example, a data-bound controlsuch such
results Managing connection
DeaisView control can bind to a set of
asatable of employees containing each employee's
Within the
Selecting data
on,
name, address, job title, and so Managingpresentation ass e
controls
DeailsView control, you can bind Label paging, caching, etc
name or address
to a single data values such as the Manipulating data
ield to create the data layout in the page. vaabie
There are many data source cortos
ASPNET provides a wide variety of rich $QL Seve
the Data in ASPNET for accessing date rom
contros that can be bound to data, Under from ODBC or OLE DB servers, rom
s
tab of the Visual Studio Toolbox, you can get severaland from business objects.
to
controls under the Data tab that could be used couid
or Based on type af data, these controis
display data from a data source, like a database be divided into two categories
XML file.
Hierarchical data source conrcis
Frequently used Databound controls: Table-based data source contos
Repeater data source controls used for hierarhca
The
Datalist
data are:
GridView XMLData Source: t alous binding to XM
schema
List View files and strings with or without
information.
Form View

(149 Ralst Picti


NIT
I-V

11 YEAR V SEMESTER D
BCA 7
In
provider that supplies site map information.
SiteMapDataSource: It allows binding to a
The data source controls used tor tabular data are: S
9
Descrlption
SI. No. Data source controls provider that U
1 represents a connection to an ADO.NET dataaccessible yia 10
SqlDataSource It data sources
returns SQL data, including
OLEDB and ODBC.
returns 11
2 allows binding to a custom .Net business object that
ObjectDataSource It
data.
Ling-to-SQL query 12
3 LinqdataSource It allows binding to the results of a
(supported by ASPNET 3.5 only).
Access database.
4 AccessDataSource It represents connection to a Microsoft The SqlDa
Data Source Views TheS
Data source views ane objects of the DataSourceiew class. Which represent a customized view of Oradedatat
data for different data operations such as sorting, filtering, etc.
p data is m
The DataSourceView class serves as the base class for all data source view classes, which define the The fo
capabilities of data source controls. <asp
The following table provides the properties of the DataSourceView class: Provi
SLNo. Properties Description Conn
1 CanDelete Indicates whether deletion is allowed on the underlying data source. Selec
2 Caninsert Indicates whether insertion is allowed on the underlying data source. <asp
Conf
3. CanPage Indicates whether paging is allowed on the underlying data source.
4 CanRetrieve Indicates whether total row count information is available. property g
TotalRowCount The
5. CanSort Indicates whether the data could be sorted. Provides th
6. CanUpdate Indicates whether updates are allowed on the
underlying data source.
SLNo.
7. Events Gets a list of event-handler 1
delegates for the data source view.
8. Narne Name of the view.

Thefollowing table provides the methods of the 2.


DataSourceView class:
SL.No. Methods
1. CanExecute
Description
Determines whether the specified command can be 3.
2 ExecuteComnand Executes the specific command. executed.
3 ExecuteDelete Performs a delete operation on the list
4
Executelnsert
DataSourceView object represents. of data that the
Performs an insert operation on the list
5 ExecuteSelect
DataSourceView object represents.
Gets a list of data from the
of data that the

6 ExecuteUpdate underlying data storage.


Performs an update operation
DataSourceView object represents.on the list of data that the 5
150
Rahul Publicativns
UNIT - IV PROGRAMMING USING ASP .NET

7. Delete Performs adelete operation on the data associated with the view.
8. Insert Performs an insert operation on the data associated with the
view.
9 Select Pourns the oueried data.
10 Update Perlorms an update operation on the data associated with the
view.
11
OnDataSourceVey Raises the DataSourceViewChanged event.
Changed
12
RaiseUnsupported Called by the RaiseUnsupportedCapabilitiesEror method to
CapabilitiesError compare the capabilities requested for an ExecuteSelect
operation against those that the view supports.
The SqlDataSource Control
he sqbataSource contral represents a connection to a relational database such as SQL Server or
Oracle database, or data accessible through OLEDB or Open Database Connectivity (ODBC). Connection
to data is made through two important properties ConnectionString and
ProviderName.
The following code snippet provides the basic syntax of the control:
<asp:SqlDataSource runat="server" ID="MySqSource"
ProviderName'<%$ ConnectionStrings:LocalNWind. ProviderName %>'
ConnectionString '<$ ConnectionStrings: LocalNWind %>
SelectionCommand "SELECT FROM EMPLOYEES" I>
<asp:GridViewlD="GridViewl"runat="server"Data SourcelD ="MySqlSource"/>
Configuring various data operations on the underlying data depends upon the various properties
(property groups) of the data source control
The following table provides the related setsof properties of the SqlDataSource control, which
provides the programming intert ce of the control
SI.No. Property Group Description
1 DeleteCommand. Gets or sets the SQL statement, parameters, and type for
DeleteParameters, deleting rows in the underlying data.
DeleteCommandType
2 FilterExpression, Gets or sets the data filtering string and parameters.
FilterParameters
3 InsertCommand, Gets or sets the SQL staternent, pararneters, and type for
InsertParameters, inserting rows in the underlying database.
InsertCommandType
4 SelectCommand, Gets or sets the SQL statement, parameters, and type for
SelectParameters, retrieving rows from the underlying database.
SelectCommandType
SortParameterName Gets or sets the name of an input parameter that the
command's stored procedure will use to sort data,.
5 UpdatcCommand, Gets or sets the SQL staterment, parameters, and type for
UpdateParameters, updating rows in the underlying data store.
UpdateCommandlype

151
Ruhul Publications
l YEARV SEMESIEP
BCA
Text='<%#Eval
Font Bold"true"
:<asp Label ID"bIDate" nunat"server"
Date:<
("PostedDate") %>/> <Ad>
</r>

<Aable>
<td>«>

td lsan-"2>&nbsp, </td>

temTemplate>
<Footer Template>
table

<FooterTemplate>
< asp.Repeater>
< div></tom>

< body>< html>


After completion of aspx page design add following namespaces in code behind
4.2 DATA LIST CONTROL

Q4. Explain about data list control in asp.net.

Datalist is a Databound control to display and manipulate data in a web application. It is a composite
control that can combine other ASPNet controls and it is present in the form. The Datalist appearance is
conroled by its ternplate fields.
The following template fields are supported by the DataList control:
Jtemtemplate: It specifies the Iterns present in the Datasource, it renders itself in the browser as
many rows present in the data source collection.
EdititemTemplate: Used to provide edit permissions to the user.
HeaderTemplate: Used to display header text to the data source collection.
FooterTemplate: Used to display footer text to the data source collectian.
ItemStyle: Used to apply styles to an ItemTemplate.
EditStyle: Used to apply styles to an EditltemTenplate
HeaderStyle: Used to apply styles to a HeaderTemplate
FooterStyle: Used to apply styles to a FooterTemplate.

158
Rahul Publications
NIT
- IV PROGRAMMING USING ASP NET
Frequently used properties with DataList
Propertv Name
RepeatDirection Desciption
Used to set or get the Direction of iferns
Horizontal
being rendet
\'ertical(Detault) Horizontal Items will render in Hori zontal
\ertical Itens will rendet in Vetica!

RepeatCoulnns Used to set nunber of columns dispia in


DataList Retun tvpe is int data type
an inde.
EditItenIndex I1s un-time property used to set
value to change its template from
ItemTemplate to EditItemTernplate
Ifit set to -l no Items will change its
Tennplate

Frequently used Events with DataList control


Event: Event is aprecise time that something happened.
Eventame Description
clicks on a
OnEdit Command Itwill beraised when user
button(present in DataList) whose
CommandName is "Edit
clics on a
It will be raised when user
OnUpdateCommand button(present in DataList) whose
CommandName is Update
clicks on a
It will be raised when user
OnCanceiCommand button(present in DataList) whose
CommandName is Cancel
clicks on a
It will be raised when user
OnDeleteCommand button(present in DataList) whose
ComnandNane is Delete'

with DataTable
ASPNET DataList Example
1|DataListExample2.aspx "DataListExample2.
AutoEventWireup= "rue" CodeBehind=
="C#"
<%@ Page Language
aspx.cs"
Inherits ="DataListExample.DatalListExample2" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="'server>
<title></title>
</head>

<body> runat="server">
<form id="form1"
<div>
Rahul Publications
{159
BCA YEAR V SEMESTEP

<p> The Datalist shows data of DataTable </p2


</div>
<asp:DataList I)-"Datali 1" nat"server
<ItemTemplate>
<table cellddng- "2 "1" style " width: 300px;
height:
cellspacing= "0" border
0px; border: dashed 2y wo4AEEE backround-color: #FFFFPF
<tr>

<td>
Eval("ID'")
<b>ID: </b><span class ="city"><%#
%> </span> <br />
<b>Name: </b><span class="postal">
<%# Eval("Namne") %> </span><br />
<b>Email: </b><span class="country'>
<%# Eval("Email") %></span><br />
</td> </tr>
</table>

</ItemTemplate>
</asp:DataList>
</form>
</body> </html>
CodeBehind
/ DataListExample2.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Ling;
using Systern.Web:
using System.Web.Ui;
using System.Web.UI.WebControls:;
namespace DataListExample

public partial class DataListExample2


System.Web.UI.Page
protected void Page Load(object sender, EventArgs e)

160
Rahul Publications
UNIT-
IV PROGRAMMING ySING ASP .NEJ

DataTable table = new Data'lable(0:


table.Columns.Add("ID");
table.Columns.Add("Name"):
table.Columns.Add("Email"):
table.Rows.Add("101", "Sachin Kumar", "'sachinwexarnple.corn");
table. Rows.Add("102", "Peter", "[email protected]");
table.Rows.Add("103", "Ravi Kumar", "ravi(@example.corn");
table.Rows.Add("104", "Irfan", "[email protected]"):
DataList1.DataSource = table:
DataList1.DataBind();

Output browser.
produces the following output to the
It
http//localhost:50134/Data X

6) localhost.5( 1208%

DataTable
shows data of
The DataList

ID: 10I
Name: Sachin Kumar
Email: [email protected]

ID: 102
Name: Peter
Email: [email protected]

ID: 103
Name: RavíKumar
Email: [email protected]

ID: 104
Name: Irfan
Email: ifaua esanple.com

database
Example with Rahul PubliCaliuns
ASPNET DataList
{161 }
PROGRAMMING USING ASP NET

4.3 D TA GRID CONTROL VIEW AND ITS IMPORTANCE


Explain about data grid control in asp.net.
05. (Inp.)
DataGrid
ASPNET
It was introduced in
NET Framework provides DataGrid control to display data on the web page. requires
and now has been deprecated. DataGrid is used to display data in scrollable grid. It
NET1.0
populate data in the arid.
ata source to Source for the
side control and can be dragged from the toolbox to the web form. Data DataGrid in
It is aserver database. Let's see an example, how can we create a
either a DataTable or a
DataGridcan be
u application.
using thedatabase to
contains two examples. One is using the DataTable and second is
This tutorial
DataGrid.
isplay data intothe
DataTable
ASPNET DataGrid Example with
DataGridExample2.aspx
AutoEventWireup="rue" CodeBehind= "Data GridExample2.
="C#"
<%@ Page Language GridExample. Data GridExaple2" %>
zspx.cs" Inherits = "Data
<DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server>
<title></title>
<head>
cbody> runat="server">
<form id="form1"
</p>
<div>
contains DataTable records
<p>This DataGrid
ID="DataGrid1" runat=
"server">
<asp:DataGrid
</asp:DataGrid>
</div>
</form>
<body>
<html>
CodeBehind
DataGridExampleZ.aspx.cs
using System;
using System.Data,
amespace DataGridExample Rahul Pubt'ec
{167}
BCA || YEARV

Puotc partial clase DataGridExample2 : System.Web.olos

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", "subramanium@example. com":
table. Rows.Add("104", "Abdul Khan', "[email protected]");
DataGrid1.DataSource = table;
DataGrid1.DataBind();

Output
It produces the following output to the browser.

http:Jccalhct:49959 Date x

Ic caincs: 120:

This DataGridcontains DataTable ecords

ID Name |Email
101Deepak Kumar deepak'a example.com
102John johna example.com
o3subránaniunn Swamilsubramanium @example.om
044bdul Khan abdut a example.com

ASP.NET DataGrid Example with Database


This example uses database as a data source to
the following steps. display data to the DataGrid. This incudes
example
168
Rahul Public
NET
PROGRAMMING USING ASP

<th>Unit Price</h>
</tr>
<tbody>
<asp:PlaceHolderlD ="itemPlacetHolder"runat "server"/>
</tbody>
</table>
<Layoutemplate>
<ltemTemplate>
<tr>
<td> <%# Eval("ProductlD") %></td>
<td> <%# Eval("ProductName")%></td>
<td><%# Eval("UnitPrice") %></td>
</tr>
</ltemTemplate>
<AlternatingltemTemplate>
<trstyle ="background-color: #dadada;">
<td><%# Eval(ProductID")%></d>
<td><%# Eval("ProductName")%></td>
<td> <%# Eval("UnitPrice")%></td>
</tr>
</AlternatingltemTemplate>
</asp:ListView>
following figure shows:
The above ListViewwill render HTML table as
Proic D Prodact Name Utt Pice
Del luspiron 132$ Nteme 500

3
Del Sudo 15 Bue
Del Inspiron 1$$ Black 600
Del Inspiron il5 Prenin
So0
Dell \'ostro 1310 Ntene Power
Del \PS \MI30 CG
600
Nokia

4.6 UsER CONTROLS

09. Explain about user controls in asp.net.


(lmp.)
controls. These user defined controls are categorizxd into:
ASPNET allows the users to create
User controls
Custom controls

{187 }
BCA YEAR VSEMESTER

User Controls
User controls behaves like miniature ASPNET pages or web forms, which could be used by rnany
other pages. These are derived from the System, Web,UL,UserControl cass. These controls have the

following characteristics:
They have an .ascx extension.
They may not contain anv <html>, <body>, or <form> tags
They have a Control directive instead of a Page directive. footer for
which will work as the we,
To understand the concept, let us create a simple user control,
pages. To create and use the user control, take the following steps:
Cheate a new web application.
Solu-tion Explorer and choose Add New Itern.
ight clhick on the project folder on the
Sclution bplorer cuto.

Solutcn 'custormcontrclderno

Cirn
Pubish..
Convert to Wed Appxion
2. Check Accessibility..

Add Refeiencc..
tng tem
Add Web ReferencG...
NewFolder
Add Servce Reference...
Add ASP.NEY Fo!der
s Componert..
Cls55... Sct as StetUp Prcect
Debug

dialog box and name it footer.ascx. Initially. the


Select Web User Control from the Add New Item
directive.
footer.ascx contains only a Control
"C#"AutoEventWireup="true"CodeBehind="footer.ascx.cs"
<%@ControlLanguage =
Inherits="custorncontroldemo.footer" %>

file:
Add the following code to the
<table>
<ir>
<td
<tdalion="center" >Copyright 02010 TutorialPoints Ltd.
</tr <tr>
</d
<tdalian="center"> Localion: Hyderabad, A.P
</tr></table > nstance o!
control to your web page, you must add the Register directive and an
To add the user following code shows the content file:
Control to the page. The
the user
188

Rahul Publications
.NET
PROGAAMMING USING ASP

geLanguage-"CeAutotventWireupue" CodeiBehind Delauit a


ston nt dem Detault
RessterSr"- tooter ascx" lagNane-footer"Tagrefix - Tfooter
UCTVPE htnl UBLIC"WIC DTD XHIML 1.0 TransitionalV/EN
httnll-tansational.dtd" >
hulnns=htt www.w3.org 1999/xhtml" >
<heinnat= sever"
Kitiex

Untitled Page
<tide
< head>

<body>
<iomid="forml runat="server">
<div>
<asp:LabellD="Label1'runat="server"Text-"Welcome to ASPNet Tutorials "></asp:Label>

<br ><br/>
<asp:ButtonlD="Buttonl"runat="serveronclick="Button1 Click"Text="Copyright Info">

<div>
<Tiooter:footerlD="footer1"runat="server"/>

<íomn>
< body></htmnl>
the pages ci your
When executed, the page shows the footer and this control could be used in all
website.

Welcome to ASP.Net Tutorials

|Copyright Info
Copyright 2010 TutorialPoints Ltd.
Location: Hyderabad,-AP T

Observe the following:


1. The Register directive specifies atag name as v/el! as tg preix for the contro!.
<%aRegisterSrc="~/ooter.asCx"Tariiame-"ooterTagl'ieixTioot er"
2. Thefollowing tagname and prefix shotd 'be oi whle nddng the usCT contro! on the e
<Tíooter:footerlD="footer 1"runat- ' r y r /

129

You might also like