Tuesday 14 May 2013

insert,Edit,update and delete operations in detailsview asp.net


insert,Edit,update and delete operations in detailsview asp.net



Here we are going to discuss about detailsview i.e insert,Edit,update and delete operations in detailsview asp.net. The main purpose of using this control is to show only one record at a time on screen.

This control has his pagination no need to manage the pagination external using data pager or custom paging.

Main thing is that you just need to use this control and enable paging edit update and delete functionality using command fields.

To do this you need to use one aspx page which will hold the detailsview control and having code behind.



<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/Site.Master"CodeBehind="DetailsView.aspx.cs"
    Inherits="AspNetDemo.DetailsView" %>

<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <asp:UpdatePanel ID="Updatepanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:DetailsView ID="dvDemo" runat="server" Height="50px" Width="275px" BackColor="White"
                BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3"CellSpacing="1"
                GridLines="None" AllowPaging="True" AutoGenerateRows="False" DataKeyNames="HolidayNum"
                HorizontalAlign="Center" OnItemDeleting="dvDemo_ItemDeleting"OnItemInserting="dvDemo_ItemInserting"
                OnItemUpdating="dvDemo_ItemUpdating" OnModeChanging="dvDemo_ModeChanging"OnPageIndexChanging="dvDemo_PageIndexChanging">
                <EditRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                <Fields>
                    <asp:BoundField DataField="HolidayName" HeaderText="HolidayName"SortExpression="HolidayName" />
                    <asp:BoundField DataField="Day" HeaderText="Day" SortExpression="Day" />
                    <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"ShowInsertButton="True" />
                </Fields>
                <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                <PagerSettings Mode="NumericFirstLast" />
                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            </asp:DetailsView>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>


Here I have taken details view control with some properties now set datasource of this control.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace AspNetDemo
{
    public partial class DetailsView : System.Web.UI.Page
    {
        string connectionString = @"Data Source=YourServerName;Initial Catalog=DataBaseName;Integrated Security=True";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetData();
            }
        }

        private void GetData()
        {
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = connectionString;
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText = "Select * from HolidaysDetails Where Deleted='N'";
                    cmd.ExecuteReader();
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    con.Close();
                    DataTable dt = new DataTable();
                    sda.Fill(dt);

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

                }
            }
        }

        protected void dvDemo_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
        {
            dvDemo.PageIndex = e.NewPageIndex;
            GetData();
        }

        protected void dvDemo_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            //--- Your updating code goes here-------
        }

        protected void dvDemo_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            //--- Your inserting code goes here-------
        }

        protected void dvDemo_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
        {
            //--- Your Delete code goes here-------
        }

        protected void dvDemo_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {           
            dvDemo.ChangeMode(e.NewMode);
            this.GetData();          
        }
    }
}

You nee to add in commented section to work your application as expected.
Finally when you run your application you will see details view like this.

ReadOnly View:
 
EditView:
 
InertView:
 

No comments:

Post a Comment