Sei sulla pagina 1di 21

Data Binding Controls

ASP.NET comes with two sets of


data aware
control

Data Source Control


Data Bound Control

DATA SOURCE CONTROLS


These are used to bind data to the data

bound
control.
The XmlDataSource and SiteMapSource, are
used to bind hierarchical, XML-based data to
these controls.

The AccessDataSource is used to display


data from a Microsoft Access database in your
web pages.
The ObjectDataSource allows you to connect
your data-bound controls to separate objects in
your application.

DATA BOUND CONTROL


These are used to display and edit
data.
Data bound control can be divided in
two types:These are
designed
to Data Bound Control
Simple
show multiple records

Data Bound
TheseControl
are designed
at the same Complex
time.
to show one record
These are:at a time.
GridView
These are:DataList
ListView
Repeaters

DetailsView
FormView

SQL Data Source


Provides access to any data source that has an
ADO.NET Data
Provider available; by default, the control has access
to the ODBC,
OLE DB, SQL Server, Oracle, and SQL Server CE

The steps that we follow to configure our SQL


providers.
data
1. Drag
sqldatasource control from the
source
areand
as drop
follows:tool
box on the web page.

SQL Data Source(Cont.)


2. Then configure your sqldatasource control:-

Click
here

SQL Data Source(Cont.)


Click Here

Type the
sqlserver name
here

Select your
database

SQL Data Source(Cont.)


Connection string

Storing Connection Information


web.config file is a central location for storing application
configuration
data in a readable and portable format.

<connectionStrings>
<add name="AppCon" connectionString="Server=localhost;
User ID=sa;Password=password;Database=Northwind;
Persist Security Info=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Note:- To retrieve connection string from web.config file we useString constr=


System.Configuration.ConfigurationManager.ConnectionStrings[AppCon"].ConnectionString;

Data Bound Control


Repeater:-

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.
It has no built-in layout or styles, so you must explicitly declare all
layout, formatting, and style tags within theThis
control's
templates.
template
is used for
Repeater has 5 inline template to format
elements
it: that you want to
render once before your
1. <HeaderTemplate>
ItemTemplate section.
This template is used for
2. <FooterTemplate>
3. <ItemTemplate>
4. <AlternatingItemTemplate>
5. <SeperatorTemplate>
It is used for elements to render
between each row, such as line
breaks.

elements that you want to


render once after your
ItemTemplate section.
This template is used for
elements that are rendered
once per row of data. It is
used to display records
This template is used for
elements that are rendered
every second row of data

Data Bound Control(Cont.)


using
using
using
using
using
using
using
using
using

System;
System.Configuration;
System.Data;
System.Web;
System.Web.Security;
System.Wb.UI;
System.Web.UI.HtmlControls;
System.Web.UI.WebControls;
System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page


{
SqlConnection con;
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
con = new
SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
cmd.Connection = con;
cmd.CommandText = "select * from student";
con.Open();
RepeatInformation.DataSource = cmd.ExecuteReader();
RepeatInformation.DataBind();
con.Close();
}
}

Data Bound Control(Cont.)


<%@ 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/xhtml1transitional.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>

Data Bound Control(Cont.)


The Output of this example:-

Data Bound Control(Cont.)


GridView:-The GridView control is used to display the values of a data
source in a table. Each column represents a field, while each row
represents a recor
Steps for using GridView: Binding to data source controls, such as
SqlDataSource

Data Bound Control(Cont.)


AutoFormat:-

Paging ,Sorting
and Selection:-

Data Bound Control(Cont.)


Editing , Deleting and
Selection:-

OutPut:-

Data Bound Control(Cont.)


Data List:- The DataList control is, like the Repeater control, used to display a
repeated list of items that are bound to the control.

Data binding:- By using DataSourceId


property we bind the Data List with a
particular data source.

Templates :-We have different


templates in Data List through which
we can configure the appearance of
Data List and also the functionality of
this control.

Data Bound Control(Cont.)


List View:The ASP.NET ListView control enables you to bind to data items that are
returned from a data source and display them. You can display data in
pages
It is useful for data in any repeating structure, similar to the DataList and
Repeater controls. However, unlike those controls, with the ListView control
you can enable users to edit, insert, and delete data, and to sort and page
data, all without code.
Binding Data
Source:-

Configuring List
View:-

Data Bound Control(Cont.)


Designing List View:-

Output:-

Data Bound Control(Cont.)


Details View:-The DetailsView control in ASP.Net 4.0 is used to

create an HTML table that displays the contents of a


single database record.

Binding
DataBase:-

Paging, Inserting,
Editing and
Deleting:-

Data Bound Control(Cont.)

OutPut:-

Data Bound Control(Cont.)

Potrebbero piacerti anche