Wednesday, 3 April 2013

insert, update and delete in DetailsView in asp.net

Design code:
<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
     <div align="center">
   <asp:DetailsView ID="dv" runat="server" 
             AllowPaging="True"
             onpageindexchanging="dv_PageIndexChanging" AutoGenerateRows="false">
            <Fields>
            <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
            <asp:LinkButton ID="lbt" runat="server" CommandArgument='<%#Eval("NAME") %>'
                    Text='<%#Eval("ID") %>' onclick="lbt_Click"></asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="NAME">
            <ItemTemplate>
            <asp:Label ID="lb" runat="server" Text='<%#Eval("NAME") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>
            </Fields>
         <FooterTemplate>
         <table>
         <tr><td>EID</td><td><asp:TextBox ID="tb" runat="server"></asp:TextBox></td></tr>
          <tr><td>NAME</td><td><asp:TextBox ID="tb1" runat="server"></asp:TextBox></td></tr>
          <tr><td colspan="2"><asp:Button ID="btn" runat="server" Text="Save"
                  CommandName="insert" Width="80"
                  oncommand="btn_Command" />&nbsp;<asp:Button
                  ID="Button1" runat="server" Text="Update" CommandName="update" Width="80"  oncommand="btn_Command"
                  />&nbsp;<asp:Button ID="Button2" runat="server"
                  Text="Delete" CommandName="delete" oncommand="btn_Command" Width="80"   /></td></tr>
         </table>
         </FooterTemplate>
         </asp:DetailsView>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
  
    </form>
</body>
</html>
c# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    DataClassesDataContext o = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fill();
        }
    }
    void fill()
    {
        dv.DataSource = o.EMPs.ToList();
        dv.DataBind();
      
    }

 
    protected void dv_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        dv.PageIndex = e.NewPageIndex;
        fill();
    }
   

    protected void btn_Command(object sender, CommandEventArgs e)
    {
        switch (e.CommandName.ToString())
        {
            case "insert":
               
                    EMP p = new EMP();
                    p.ID = Convert.ToInt32(((TextBox)dv.FooterRow.FindControl("tb")).Text);
                    p.NAME = ((TextBox)dv.FooterRow.FindControl("tb1")).Text;
                    o.EMPs.InsertOnSubmit(p);
                    o.SubmitChanges();
                    ScriptManager.RegisterClientScriptBlock(this, GetType(), "x", "alert('One record saved.')", true);
                    ((TextBox)dv.FooterRow.FindControl("tb")).Text = "";
                    ((TextBox)dv.FooterRow.FindControl("tb1")).Text = "";
                    ((TextBox)dv.FooterRow.FindControl("tb")).Focus();
                    fill();
                    break;
            case "update":
                o.EMPs.Where(b => b.ID == Convert.ToInt32(((TextBox)dv.FooterRow.FindControl("tb")).Text)).First().NAME = ((TextBox)dv.FooterRow.FindControl("tb1")).Text;
                o.SubmitChanges();
                 ScriptManager.RegisterClientScriptBlock(this, GetType(), "x", "alert('One record updated.')", true);
                fill();
                    break;

        case "delete":
            o.EMPs.DeleteOnSubmit(o.EMPs.Where(b => b.ID == Convert.ToInt32(((TextBox)dv.FooterRow.FindControl("tb")).Text)).First());
            o.SubmitChanges();
                 ScriptManager.RegisterClientScriptBlock(this, GetType(), "x", "alert('One record deleted.')", true);
                fill();
                break;
        }
    }
    protected void lbt_Click(object sender, EventArgs e)
    {
        ((TextBox)dv.FooterRow.FindControl("tb")).Text = ((LinkButton)dv.Rows[0].Cells[1].FindControl("lbt")).Text;
        ((TextBox)dv.FooterRow.FindControl("tb1")).Text = ((LinkButton)dv.Rows[0].Cells[1].FindControl("lbt")).CommandArgument.ToString();
    }
}

page looks like:

 

No comments:

Post a Comment