Add,Update,Delete,paging with DetailsView in asp.net c#
Introduction: In this article i will show you how to
bind DetailsView in asp.net c# . I have implemented all operation like
paging ,add ,edit and delete in DetailsView in asp.net c# .
In following i have written the Css class to format the DetaildView.
Html Code for DetailsView in asp.net :
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DetailView.aspx.cs" Inherits="HamidSite.DetailView" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.DetailsViewClass
{
font-family: verdana,arial,sans-serif;
font-size: 11px;
color: #333333;
border-width: 1px;
border-color: #999999;
border-collapse: collapse;
border-style: solid;
}
.Header
{
background: #b5cfd2;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
.Foter
{
background: #dcddc0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #999999;
}
.btn
{
background: #ffffff;
border-width: 1px;
padding: 2px;
border-style: solid;
border-color: #999999;
font-family: verdana,arial,sans-serif;
font-size: 11px;
}
.textbox
{
border-width: 1px;
padding: 1px;
border-style: solid;
border-color: #999999;
font-family: verdana,arial,sans-serif;
font-size: 11px;
width: 100px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsViewExample" AllowPaging="true" AutoGenerateRows="false"
runat="server" Height="50px" CssClass="DetailsViewClass" CellPadding="4" OnPageIndexChanging="DetailsViewExample_PageIndexChanging"
OnItemCommand="DetailsViewExample_ItemCommand1" OnItemUpdating="DetailsViewExample_ItemUpdating"
OnModeChanging="DetailsViewExample_ModeChanging" OnItemInserting="DetailsViewExample_ItemInserting"
Width="270px" OnItemDeleting="DetailsViewExample_ItemDeleting">
<Fields>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label>
<asp:Label ID="lblFirstName" Text='<%# Eval("FirstName") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblIDEdit" Text='<%# Eval("ID") %>' Visible="false" runat="server"></asp:Label>
<asp:TextBox ID="txtFirstname" Text='<%# Eval("FirstName") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" Text='<%# Eval("LastName") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtLastName" Text='<%# Eval("LastName") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:Label ID="lbldob" Text='<%# Eval("City") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCity" Text='<%# Eval("City") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblAddress" Text='<%# Eval("Address") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAddress" Text='<%# Eval("Address") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pin No">
<ItemTemplate>
<asp:Label ID="lblPinNo" Text='<%# Eval("PinNo") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPinNo" Text='<%# Eval("PinNo") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile No">
<ItemTemplate>
<asp:Label ID="lblMobileNo" Text='<%# Eval("MobileNo") %>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMobileNo" Text='<%# Eval("MobileNo") %>' runat="server" CssClass="textbox"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField Visible="true" ShowInsertButton="true" ShowCancelButton="true"
ShowDeleteButton="true" ShowEditButton="true" />
</Fields>
<PagerStyle CssClass="Foter" />
<FieldHeaderStyle Width="80px" CssClass="Header" />
</asp:DetailsView>
</div>
</form>
</body>
</html>
C# Code for DetailsView in asp.net c# :
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.SqlClient;
namespace HamidSite
{
public partial class DetailView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindDetailtView();
}
}
private void bindDetailtView()
{
try
{
DataSet Ds = GetDataSet("Select * from Employee");
DetailsViewExample.DataSource = Ds;
DetailsViewExample.DataBind();
}
catch (Exception ex) { throw ex; }
}
private DataSet GetDataSet(string Query)
{
DataSet Ds = new DataSet();
try
{
string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True";
SqlConnection Con = new SqlConnection(strCon);
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(Ds);
}
catch (Exception) { }
return Ds;
}
private void ExecuteQuery(string Query)
{
try
{
string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True";
SqlConnection Con = new SqlConnection(strCon);
Con.Open();
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
Con.Close();
}
catch (Exception) { }
}
protected void DetailsViewExample_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsViewExample.PageIndex = e.NewPageIndex;
bindDetailtView();
}
protected void DetailsViewExample_ItemCommand1(object sender, DetailsViewCommandEventArgs e)
{
switch (e.CommandName.ToString())
{
case "Edit":
DetailsViewExample.ChangeMode(DetailsViewMode.Edit);
bindDetailtView();
break;
case "Cancel":
DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailtView();
break;
case "New":
DetailsViewExample.ChangeMode(DetailsViewMode.Insert);
bindDetailtView();
break;
}
}
protected void DetailsViewExample_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
}
protected void DetailsViewExample_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
TextBox txtFirstname = (TextBox)DetailsViewExample.FindControl("txtFirstname");
TextBox txtLastName = (TextBox)DetailsViewExample.FindControl("txtLastName");
TextBox txtCity = (TextBox)DetailsViewExample.FindControl("txtCity");
TextBox txtAddress = (TextBox)DetailsViewExample.FindControl("txtAddress");
TextBox txtPinNo = (TextBox)DetailsViewExample.FindControl("txtPinNo");
TextBox txtMobileNo = (TextBox)DetailsViewExample.FindControl("txtMobileNo");
Label lblIDEdit = (Label)DetailsViewExample.FindControl("lblIDEdit");
string Query = "Update Employee Set FirstName='" + txtFirstname.Text + "' ,LastName ='" + txtLastName.Text + "' ,City ='" + txtCity.Text + "',Address='" + txtAddress.Text + "',PinNo='" + txtPinNo.Text + "',MobileNo='" + txtMobileNo.Text + "' where ID =" + lblIDEdit.Text;
ExecuteQuery(Query);
DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailtView();
}
protected void DetailsViewExample_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
TextBox txtFirstname = (TextBox)DetailsViewExample.FindControl("txtFirstname");
TextBox txtLastName = (TextBox)DetailsViewExample.FindControl("txtLastName");
TextBox txtCity = (TextBox)DetailsViewExample.FindControl("txtCity");
TextBox txtAddress = (TextBox)DetailsViewExample.FindControl("txtAddress");
TextBox txtPinNo = (TextBox)DetailsViewExample.FindControl("txtPinNo");
TextBox txtMobileNo = (TextBox)DetailsViewExample.FindControl("txtMobileNo");
string Query = "Insert into Employee ([FirstName] ,[LastName] ,[City] ,[Address] ,[PinNo] ,[MobileNo]) values ('" + txtFirstname.Text + "' ,'" + txtLastName.Text + "' ,'" + txtCity.Text + "','" + txtAddress.Text + "','" + txtPinNo.Text + "','" + txtMobileNo.Text + "')";
ExecuteQuery(Query);
DetailsViewExample.ChangeMode(DetailsViewMode.ReadOnly);
bindDetailtView();
}
protected void DetailsViewExample_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
Label lblID = (Label)DetailsViewExample.FindControl("lblID");
string Query = "Delete from Employee where ID =" + lblID.Text;
ExecuteQuery(Query);
bindDetailtView();
}
}
}
Related Other posts