Subscribe to Subscribers

Online Members

More...

Resources » Code Snippets » ASP.NET WebForms

How to use Details view control in ASP.NET?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 50


In this article I am going to explain about Details view control and its uses with example. In this resource example I cover all insert, edit, update and delete record details through details view.



Description


Mostly we are used details view control to view all details of single record in a one page. Details view control is same like other data control, it is also used to display record in the web page. In this control records are displayed with details in vertical format. Each record display in single page mean if you assign 10 records as DataSoure then 10 Details view paging is occur.
In this article I have explained in detail about Updating / Deleting records through Details View.

Details View Example


Consider below example how to bind data in the details view and also how to manipulate Data Edit, Update, Insert and delete operations.

Initially i have load data from database to details view, each records are displayed in details view each page. After User click Update button to Edit and update selected record details. If click Add New Button then able to add new record in the database.

Client side


I have placed here details view control

<%@ 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/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Details View Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Details View Example</b><br />
<br />
<asp:Label ID="lblmsg" runat="server"></asp:Label><br /><br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="400px" AutoGenerateRows="False"
CellPadding="4" ForeColor="#333333" GridLines="None" OnItemUpdating="DetailsView1_ItemUpdating"
OnModeChanging="DetailsView1_ModeChanging"
OnItemCommand="DetailsView1_ItemCommand" AllowPaging="true"
oniteminserting="DetailsView1_ItemInserting"
onitemdeleting="DetailsView1_ItemDeleting"
onpageindexchanging="DetailsView1_PageIndexChanging" >
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:TemplateField HeaderText="Emp No">
<ItemTemplate>
<asp:Label ID="lbleno" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.eno") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txteno" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.eno") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Emp Name">
<ItemTemplate>
<asp:Label ID="lblempname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.empname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtempname" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.empname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<asp:Label ID="lblsal" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.sal") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtsal" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.sal") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True"></asp:CommandField>
<asp:CommandField ShowInsertButton="True"></asp:CommandField>
<asp:CommandField ShowEditButton="True" CancelText="Cancel" EditText="Edit" UpdateText="Update" />
</Fields>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
</div>
</form>
</body>
</html>

Code Behind

In the code behind i have manipulate insert, update, delete operation code and query details.

using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadDet();
}
}
void LoadDet()
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from emp", sqlcon);
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
sqlcon.Close();
DetailsView1.DataSource = dt;
DetailsView1.DataBind();
}
//Below code is used to display page wise records
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
LoadDet();
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.CancelingEdit == true)
{
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
}
//Below code is used to update record details
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
string eno = ((TextBox)DetailsView1.FindControl("txteno")).Text.ToString();
string empname = ((TextBox)DetailsView1.FindControl("txtempname")).Text.ToString();
string sal = ((TextBox)DetailsView1.FindControl("txtsal")).Text.ToString();
sqlcon.Open();
sqlcmd = new SqlCommand("UPDATE emp SET empname='" + empname + "', sal='" + sal + "' WHERE eno='" + eno + "' ", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName == "New")
{
DetailsView1.ChangeMode(DetailsViewMode.Insert);
LoadDet();
}
if (e.CommandName == "Edit")
{
DetailsView1.ChangeMode(DetailsViewMode.Edit);
LoadDet();
}
}
//Below code is used to insert record into database table
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
sqlcon.Open();
string eno = ((TextBox)DetailsView1.FindControl("txteno")).Text.ToString();
string empname = ((TextBox)DetailsView1.FindControl("txtempname")).Text.ToString();
string sal = ((TextBox)DetailsView1.FindControl("txtsal")).Text.ToString();
SqlCommand cmd = new SqlCommand("select eno from emp where eno = '" + eno + "'", sqlcon);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
lblmsg.Text = "Employee No already exists";
}
else
{
dr.Close();
sqlcmd = new SqlCommand("insert into emp values('" + eno + "', '" + empname + "', '" + sal + "')", sqlcon);
sqlcmd.ExecuteNonQuery();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
}
sqlcon.Close();
LoadDet();
}
//Below code is used to delete record from database
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
string eno = ((Label)DetailsView1.Rows[0].FindControl("lbleno")).Text;
sqlcon.Open();
sqlcmd = new SqlCommand("delete from emp where eno = '" + eno + "'", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
LoadDet();
}
}


Output


Insert like below After Click new button in footer
DetailsView_Insert

Update Values like this after click Edit button in the details view footer
DetailsView_Update

Display Data Like this in Details View
DetailsView_ShowRecord


Source code:

Client Side: ASP.NET
Code Behind: C#

Conclusion

I hope this article is help you to know all Insert, update and delete through Details view operations.

Attachments
  • DetailsView_Control (44061-05954-DetailsView-Control.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to use Details view control in ASP.NET?"
    Guest Author: Manjeet Singh     19 Jan 2013
    I hope this article is help you to know all Insert, update and delete through Details view operations.


    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to read files from directory and insert into database as bytes?
    Previous Resource: How to give alternate color based on record count?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Details View  .  Update Details view  .  Insert Details View  .  Delete Details view  .  

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.