ManuelAbadia.com
PagerDataSource samples
Samples



Product Information

CRUD operations using the PagerDataSource

This sample shows how to use the PagerDataSource to obtain, update and delete data from an ObjectDataSource. If a control that has full support for the new DataSourceControls like the GridView is bound to a PagerDataSource, the PagerDataSource will allow inserting, updating and deleting if the underlying data source of the PagerDataSource supports it.

What we want is to populate the GridView with items obtained from the ObjectDataSource and also let the users update or delete an item of the current page. We are using the PagerDataSource instead of the built-in pager of the GridView.

As you can see, the PagerDataSource can work with two way data binding without any problems, and with any control that needs to bind to a data source.

The source code for this page is shown after the live sample.


Product List

<<<1|2|3|4|5|6|7|8>>>
 idnamedescriptionprice
Edit Delete1Product 1Full description for product number 12
Edit Delete2Product 2Full description for product number 24
Edit Delete3Product 3Full description for product number 36
Edit Delete4Product 4Full description for product number 48
Edit Delete5Product 5Full description for product number 510
Edit Delete6Product 6Full description for product number 612
Edit Delete7Product 7Full description for product number 714
Edit Delete8Product 8Full description for product number 816
Edit Delete9Product 9Full description for product number 918
Edit Delete10Product 10Full description for product number 1020
Edit Delete11Product 11Full description for product number 1122
Edit Delete12Product 12Full description for product number 1224
Edit Delete13Product 13Full description for product number 1326
Edit Delete14Product 14Full description for product number 1428
<<<1|2|3|4|5|6|7|8>>>

Sample source code

The ASPX file for this sample is:.


<%@ Page Language="C#" MasterPageFile="~/DefaultMaster.master" AutoEventWireup="true" CodeFile="CRUD_operations.aspx.cs" Inherits="CRUD_operations" Title="CRUD Operations" %>


<%
@ Register Assembly="PagerDataSource" Namespace="Manu.Web.UI.WebControls" TagPrefix="manu" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="Content" Runat="Server">

    <h2>Product List</h2>

 

    <manu:PagerDataSource ID="PagerDataSource1" runat="server"

        DataSourceID="ObjectDataSource1" PageSize="14"
        OnPageChanged
="PagerDataSource_PageChanged">

    </manu:PagerDataSource>

 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataKeyNames="id" DataSourceID="PagerDataSource1" CssClass="gridview">

        <Columns>

            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" />

            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />

            <asp:BoundField DataField="description" HeaderText="description" />

            <asp:BoundField DataField="price" HeaderText="price" ReadOnly="True"
            SortExpression="price" />

        </Columns>

    </asp:GridView>

 

    <manu:PagerDataSource ID="PagerDataSource2" runat="server"

        DataSourceID="PagerDataSource1" PageSize="14"
        OnPageChanged="PagerDataSource_PageChanged">

    </manu:PagerDataSource>

 

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="ProductsDAL2"

        SelectMethod="GetPagedData" SelectCountMethod="CountAll" EnablePaging="True"

        MaximumRowsParameterName="maxRows" StartRowIndexParameterName="startIndex"

        UpdateMethod="Update" DeleteMethod="Delete"
        OldValuesParameterFormatString
="Original_{0}">

    </asp:ObjectDataSource>

</asp:Content>


The code-behind file (ASPX.CS) is:


using System;

using System.Web.UI.WebControls;

using Manu.Web.UI.WebControls;

 

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

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void PagerDataSource_PageChanged(object sender, PageChangedEventArgs e)

    {

        // if we have changed the curent page and we were in edit mode, cancel editing

        if (GridView1.EditIndex != -1) {

                GridView1.EditIndex = -1;

        }

    }

 

    protected void ObjectDataSource1_Deleted(object src,ObjectDataSourceStatusEventArgs e)

    {

        // the ObjectDataSource does not automatically assign the afffected rows

        e.AffectedRows = (int)e.ReturnValue;

    }

}