<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
font-size: x-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong><span class="auto-style1">Display Data in Detail View</span></strong><br />
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="316px" AutoGenerateRows="False" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnItemDeleting="DetailsView1_ItemDeleting" OnItemInserting="DetailsView1_ItemInserting" OnItemUpdating="DetailsView1_ItemUpdating" OnModeChanging="DetailsView1_ModeChanging" OnPageIndexChanged="DetailsView1_PageIndexChanged" OnPageIndexChanging="DetailsView1_PageIndexChanging">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<Fields>
<asp:TemplateField HeaderText="Emp no">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("eno") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("eno") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("en") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("en") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("en") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Eval("ed") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Eval("ed") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("ed") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Eval("es") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("es") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Eval("es") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:LinkButton ID="LinkButton6" runat="server" CommandName="Insert">Insert</asp:LinkButton>
<asp:LinkButton ID="LinkButton7" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');">Delete</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="New">New</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Fields>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>
<br />
</div>
</form>
</body>
</html>
Code File:
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;
using System.Configuration;
using Microsoft.ApplicationBlocks.Data;
//Add Microsoft.ApplicationBlocks.Data.dll
public partial class _Default : System.Web.UI.Page
{
string cs;
protected void Page_Load(object sender, EventArgs e)
{
cs = ConfigurationManager.ConnectionStrings["abhishek"].ConnectionString;
if (Page.IsPostBack == false)
{
detailview_bind();
}
}
private void detailview_bind()
{
DataSet ds;
ds = SqlHelper.ExecuteDataset(cs, "dispemp");
DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
detailview_bind();
}
protected void DetailsView1_PageIndexChanged(object sender, EventArgs e)
{
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
detailview_bind();
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
Int32 empno, es;
String en, ed;
empno = Convert.ToInt32(((TextBox)(DetailsView1.Rows[0].FindControl("TextBox1"))).Text);
en=((TextBox)(DetailsView1.Rows[1].FindControl("TextBox3"))).Text;
ed = ((TextBox)(DetailsView1.Rows[2].FindControl("TextBox5"))).Text;
es = Convert.ToInt32(((TextBox)(DetailsView1.Rows[3].FindControl("TextBox7"))).Text);
Object[] ar = new Object[4];
ar[0] = empno;
ar[1] = en;
ar[2] = ed;
ar[3] = es;
SqlHelper.ExecuteNonQuery(cs, "insemployee",ar);
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
detailview_bind();
}
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
Int32 empno, es;
String en, ed;
empno = Convert.ToInt32(((Label)(DetailsView1.Rows[0].FindControl("Label2"))).Text);
en = ((TextBox)(DetailsView1.Rows[1].FindControl("TextBox2"))).Text;
ed = ((TextBox)(DetailsView1.Rows[2].FindControl("TextBox4"))).Text;
es = Convert.ToInt32(((TextBox)(DetailsView1.Rows[3].FindControl("TextBox6"))).Text);
Object[] ar = new Object[4];
ar[0] = empno;
ar[1] = en;
ar[2] = ed;
ar[3] = es;
SqlHelper.ExecuteNonQuery(cs, "sp_update", ar);
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
detailview_bind();
}
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
{
Int32 empno;
empno = Convert.ToInt32(((Label)(DetailsView1.Rows[0].FindControl("Label1"))).Text);
Object[] ar = new Object[1];
ar[0] = empno;
SqlHelper.ExecuteNonQuery(cs, "sp_delete", ar);
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
detailview_bind();
}
}
Output:
0 comments:
Post a Comment