Wednesday 8 August 2012

"Detail View in asp.net"


detailview.aspx

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px"
        AllowPaging="True"  DataKeyNames="AutoID"
    Width="125px" EmptyDataText="no data exist in record." AutoGenerateRows="False"
        CellPadding="4" ForeColor="#333333" GridLines="None"
    onitemdeleting="DetailsView1_ItemDeleting"
    oniteminserting="DetailsView1_ItemInserting"
    onitemupdated="DetailsView1_ItemUpdated"
        onmodechanging="DetailsView1_ModeChanging"
        onpageindexchanging="DetailsView1_PageIndexChanging"
        oniteminserted="DetailsView1_ItemInserted"
        onitemupdating="DetailsView1_ItemUpdating">
        <AlternatingRowStyle BackColor="White" />
        <CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
        <FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
        <Fields>
            <asp:TemplateField HeaderText="Auto ID" Visible="false">
                <EditItemTemplate>
                    <asp:TextBox ID="editTextBox1" runat="server" Text='<%# Bind("AutoID") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="insertTextBox1" runat="server" Text='<%# Bind("AutoID") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("AutoID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Page Name">
                <EditItemTemplate>
                    <asp:TextBox ID="editTextBox2" runat="server" Text='<%# Bind("PageName") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="insertTextBox2" runat="server" Text='<%# Bind("PageName") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("PageName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Page Description">
                <EditItemTemplate>
                    <asp:TextBox ID="editTextBox3" runat="server" Text='<%# Bind("PageDescription") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="insertTextBox3" runat="server" Text='<%# Bind("PageDescription") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("PageDescription") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Active">
                <EditItemTemplate>
                    <asp:TextBox ID="editTextBox4" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
                </EditItemTemplate>
                <InsertItemTemplate>
                    <asp:TextBox ID="insertTextBox4" runat="server" Text='<%# Bind("Active") %>'></asp:TextBox>
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Active") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowInsertButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
        </Fields>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
    </asp:DetailsView>


detailview.aspx.cs

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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
            MFIll_DetailView();
        }
    }

    private void MFIll_DetailView() 
    {
        string strConn = @"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True";
     SqlConnection conn = new SqlConnection(strConn);
     conn.Open();
     SqlCommand cmd = new SqlCommand("select * from mySampleTable", conn);
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataSet ds = new DataSet();
     da.Fill(ds);
     DetailsView1.DataSource = ds.Tables[0];
     DetailsView1.DataBind();
    
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        
        TextBox txtpagename = (TextBox)DetailsView1.FindControl("insertTextBox2");
        TextBox txtdesc = (TextBox)DetailsView1.FindControl("insertTextBox3");
string strConn = @"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True";
     SqlConnection conn = new SqlConnection(strConn);
     conn.Open();

     SqlCommand cmd = new SqlCommand("sp_insert", conn);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.Add("@PageName", txtpagename.Text);
     cmd.Parameters.Add("@PageDescription", txtdesc.Text);
     cmd.ExecuteNonQuery();
     
     MFIll_DetailView();
       

    }
    protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);       
    }
    protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {

        string strConn = @"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True";
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();
        DataKey dk = DetailsView1.DataKey;
        int id =int.Parse( dk.Value.ToString());
        string strqry = @"DELETE FROM mySampleTable WHERE AutoID='" + id + "'";
        SqlCommand cmd = new SqlCommand(strqry,conn);
        cmd.ExecuteNonQuery();
        MFIll_DetailView();
        Label5.Text = "Record Deleted....";
       

    }
    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        DetailsView1.ChangeMode(e.NewMode);
        MFIll_DetailView();
        if (e.NewMode == DetailsViewMode.Edit)
        {
            DetailsView1.AllowPaging = false;

        }
        else 
        {
            DetailsView1.AllowPaging = true;
        }
    }
    protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        MFIll_DetailView();
    }
    protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        DataKey dk = DetailsView1.DataKey;
        int id= int.Parse(dk.Value.ToString());

        TextBox txtpagename = (TextBox)DetailsView1.FindControl("editTextBox2");
        TextBox txtdesc = (TextBox)DetailsView1.FindControl("editTextBox3");
        string strConn = @"Data Source=.\sqlexpress;Initial Catalog=Test;Integrated Security=True";
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();

        SqlCommand cmd = new SqlCommand("sp_update", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@AutoID", id);
        cmd.Parameters.Add("@PageName", txtpagename.Text);
        cmd.Parameters.Add("@PageDescription", txtdesc.Text);
        cmd.ExecuteNonQuery();
        MFIll_DetailView();
       
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
    }
}

No comments:

Post a Comment