0% found this document useful (0 votes)
17 views9 pages

Unit Iv

The document provides an overview of various ASP.NET data-bound controls including Repeater, DataList, GridView, ListView, DetailsView, and FormView, detailing their features and functionalities. It explains how to display data from a data source, such as a database, using these controls and includes code examples for binding a GridView to an MS Access database. Additionally, it outlines properties, events, and methods associated with the GridView control.
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)
17 views9 pages

Unit Iv

The document provides an overview of various ASP.NET data-bound controls including Repeater, DataList, GridView, ListView, DetailsView, and FormView, detailing their features and functionalities. It explains how to display data from a data source, such as a database, using these controls and includes code examples for binding a GridView to an MS Access database. Additionally, it outlines properties, events, and methods associated with the GridView control.
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/ 9

Example

1. da = new SqlDataAdapter("Select * from Employee", con);


2. ds = new DataSet();
3. da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
4. da.Fill(ds, "Emp");
5. bldr = new SqlCommandBuilder(da);
6. dataGridView1.DataSource = ds.Tables["Emp"];

DISPLAY DATA ON DATA BOUND CONTROLS AND DATA GRID


ASP.NET provides a wide variety of rich controls that can be bound to data. Under the Data tab of the
Visual Studio Toolbox, you can get several controls under the Data tab that could be used to display
data from a data source, like a database or XML file.

The standard ASP.NET data presentation controls are:


• DataList
• DetailsView
• FormView
• GridView
• ListView
• Repeater
Repeater Control
• The ASP.NET Repeater is a basic container control that allows us to create custom lists from any
data available to the page.
• It provides a highly customized interface. It renders a read-only template
• The Repeater control is a Data Bind Control, also known as container controls.
• The Repeater control is used to display a repeated list of items that are bound to the control.
• This control may be bound to a database table, an XML file, or another list of items.
• The Repeater control supports the following features:
• List format
• No default output
• More control and complexity
• Item as row
• Paging, Sorting and Grouping requires custom code writing
• only Web control that allows you to split markup tags across the templates
• no built-in selection capabilities
• no built-in support for edit, insert and delete capabilities
• no built-in support for paging, sorting and grouping capabilities
• no built-in layout or styles, need to declare all layout, formatting and style tags explicitly within
the control's templates
Using the data we stored in table Student of our sample database, the Repeater control will look like
this.

DataList Control
• The DataList control works like the Repeater control, used to display the data in a repeating
structure, such as a table.
• DataList allows you to repeat columns horizontally or vertically.
• You can configure the DataList control to enable users to edit or delete a record in the table.
• We can use a DataList control where we need a single-column list.
• It displays data in a format that you can define using a template and styles.
• However, it arranges the data defined in the template within various HTML structures.
• This includes options for horizontal or vertical layout and it also allows you to set how the data
should be repeated, as flow or table layout.
• The DataList control does not automatically use a data source control to edit data.
• Instead, it provides command events in which you can write your own code for these scenarios.
You can configure the DataList control where the user can edit or delete a record in the table.
The DataList control supports the following features:
• Support for binding data source controls such as SqlDataSource, LinqDataSource and
ObjectDataSource
• Directional rendering
• Good for columns
• Item as cell
• Updatable
• Control over Alternate item
• Paging function needs handwriting.
After execution our ListView will look like this.

GridView Control
The GridView control is used to display the values of a data source in a table. Each column represents
a field where each row represents a record. It can also display empty data. The GridView control
provides many built-in capabilities that allow the user to sort, update, delete, select and page through
items in the control. The GridView control can be bound to a data source control, in order to bind a
data source control, set the DataSourceID property of the GridView control to the ID value of the data
source control.

The GridView control supports the following features:


• Improved data source binding capabilities
• Tabular rendering – displays data as a table
• Item as row
• Built-in sorting capability
• Built-in select, edit and delete capabilities
• Built-in paging capability
• Built-in row selection capability
• Multiple key fields
• Programmatic access to the GridView object model to dynamically set properties, handle
events and so on
• Richer design-time capabilities
• Control over Alternate item, Header, Footer, Colors, font, borders, and so on.
• Slow performance as compared to Repeater and DataList control

ListView Control
The ListView control resembles the GridView control. The only difference between them is that the
ListView control displays data using user-defined templates instead of row fields.
The ListView control supports the following features:
• Binding to data source controls Customizable appearance through user-defined templates and
styles.
• Built-in sorting and grouping capabilities
• Built-in insert, edit and delete capabilities
• Support for paging capabilities using a DataPager control.
• Built-in item selection capabilities
• Multiple key fields
• Programmatic access to the ListView object model to dynamically set properties, handle events
and so on
• Fast performance as compared to GridView

DetailsView control
The DetailsView control uses a table-based layout where each field of the data record is displayed as
a row in the control. Unlike the GridView control, the DetailsView control displays one row from a
data source at a time by rendering an HTML table. The DetailsView supports both declarative and
programmatic data binding. It shows the details for the row in a separate space. We can customize
the appearance of the DetailsView control using its style properties.
The DetailsView control supports the following features:

• Tabular rendering
• Supports column layout, by default two columns at a time
• Optional support for paging and navigation.
• Built-in support for data grouping
• Built-in support for edit, insert and delete capabilities

FormView control
The FormView control renders a single data item at a time from a data source, even if its data source
exposes a multiple records data item from a data source. It allows for a more flexible layout when
displaying a single record. The FormView control renders all fields of a single record in a single table
row. In contrast, the FormView control does not specify a pre-defined layout for displaying a record.
Instead, you create templates that contain controls to display individual fields from the record. A
FormView is a databound control used to insert, display, edit, update and delete data in ASP.NET that
renders a single record at a time. A FormView control is similar to a DetailView in ASP.NET but the
only difference is that a DetailsView has a built-in tabular rendering whereas a FormView requires a
user-defined template to insert, display, edit, update and delete data.

The FormView control supports the following features:


• Template driven
• Supports column layout
• Built-in support for paging and grouping
• Built-in support for insert, edit and delete capabilities

DISPLAY DATA ON DATA GRID


GridView is a control used to display data in tables on a web page. It displays data in both rows and
columns, where each column represents a field, and each row represents a record.
Properties of GridView
Here are some properties of GridView:
Name Description
BorderWidth BorderWidth represents the border width of a Web Control Server. The specified
border width of a web server is negative.
BackColor BackColor represents and sets the background color of the Web Server Control.
By default, it sets the color to be empty to show that there is no property set.
BorderColor BorderColor sets the border color for the Web Server Control. It is inherited from
the WebControl base class.
Width A Width is a unit that represents the width of a Web Server. The width of the Web
Server is a negative value.
Control Events of GridView
GridView control provides several events that help develop programs for an application.
Here are some of the Control Events:
Name Description
RowCommand RowCommand is an event that performs a task when a button is clicked in the
GridView control.
RowCreated RowCreated is an event used to modify the contents of a row when a row is
created.
RowDeleted RowDeleted is an event used to check the results of a deleted operation.
RowUpdated RowUpdated helps to check the results of an updated operation..
Sorted An event is used to perform tasks after the user clicks a hyperlink to sort a
column.
RowEditing RowEditing is an event used to cancel the editing operations whenever the event
occurs.
Sorting Sorting is used to perform the sorting functionality when a GridView control is
bound to a DataTable object by setting the data source property.
RowDataBound RowDataBound occurs when a row is bound to data in a GridView control.
Methods of GridView
Here are some of the methods of GridView:
Name Description
AddedControl AddedContol method is used when a control is added to Control’s collection.
DataBind DataBind binds a data source that invokes a server control and child controls
with an option to raise the DataBinding event.
Dispose Dispose is a method that enables a server control to perform a cleaning
operation before the release of memory.
FindControl FindControl is used to search the current naming container for the specific
server control with the specified id parameter.
RemovedControl RemovedControl is a method called immediately after a child control is
removed from the controls collection of the control object.
InitializerPage InitializePage initializes the page row displayed when the paging feature is
enabled.
LoadViewState LoadViewState loads the previously saved view state of the GridView control.
This method is mainly used by the .net framework.
OnBubbleEvent OnBubbleEvent is a method used to pass the interface for server control
hierarchy to the web server control.
Bind GridView With MS Access Database
Step 1: Open Visual Studio 2010 and create an empty website and give it a suitable name
Gridview_demo
Step 2: In Solution Explorer you will get your empty website, Add a web form by Right click and
Add New Item Web Form. Name it Gridview_demo.aspx.
Step 3:Open your MS Access 2007 or whatever version you have and click on Blank Database,
then Create.

After creating a database in the left pane you see your default table, then right click on that table and
go to Design View. Here in Design view we will add some entity to our table.
When you will get the complete design, again go to the table [tbl_data] and right click on Datasheet
View. Feed some data in those fields, so that we can bind our data.

Creating Connection between MS Access and Visual Studio

Open Visual Studio, then in Server Explorer go to Connect to Database and Add Connection window
will open like the following image.

Click on Change, you will get a new window of Change Data Source as in the following image.
Here you saw Microsoft Access Database been taken as Data source because we have access
database with us. After that you have to browse the database (access database) that you saved,
browse it and click Test Connection. If it says Test Connection succeeded, then you can go ahead
with the other process, otherwise you have to repeat the process.
Step 3: Open Gridview_demo.aspx file where we will create our design for binding GridView.

Step 4: Open Gridview_demo.aspx.cs and write some code so that our application starts working.
Gridview_demo.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

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


{
protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{
refreshdata();
}
}

public void refreshdata()


{

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;


Data Source=C:\Users\nilu\Desktop\Database1.accdb");
OleDbCommand cmd = new OleDbCommand("select * from tbl_data",con);
OleDbDataAdapter olda = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
olda.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();

You might also like