Monday, October 21, 2019

How to Work with DetailView Control in Asp.net

DetailView Control In Asp.net

This Article explain the DetailView Control and its use in Asp.net program



# Microsoft Applications block – To access data (another technique)
  • Microsoft.Applicationblocks.data.dll (download)
  • Not project specific
  • Less coding to be done
  • Cannot be used in more than one file
  • Bug free and tested
  • Sql caching (better performance)
  • To access data

Working with Detail View

#Open Visual Studio then File> New> Project > C# > website empty 

# Add Connection String

<connectionStrings>
    <add name="ps" connectionString="server=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\dbemployee.mdf;Integrated Security=True"/>
  </connectionStrings>

# Website > Add Reference > Microsoft.Applicationblocks.Data.dll
*Add webform
*Toolbox > Data > DetailsView

# DetailsView

-         It is similar to GridView

-         In DetailView we can display single record

-         Data is displayed in rows

-         Paging is inbuilt

# Templates applied

-         Item Template

-         Alternating Item Template

-         Header Template

-         Footer Template

-         Edit Item Template

-         Pager Template

-         Insert Item Template

-         Empty Data Template

# Coding

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.Configuration;
using Microsoft.ApplicationBlocks.Data;

public partial class _Default : System.Web.UI.Page
{
    String psg;
    protected void Page_Load(object sender, EventArgs e)
    {
        psg = ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
        if(Page.IsPostBack==false)
        {
            Det_Bind();
        }
    }

    private void Det_Bind()
    {
        DataSet ds = SqlHelper.ExecuteDataset(psg, "dispemp");
        DetailsView1.DataSource = ds;
        DetailsView1.DataBind();
    }
}

# Design

*DetailView> Properties> Autogenerate Rows=False
*DetailView> Properties> Fields>Collection
*Template field > Header Text> Empno
*Template field > Header Text> Name
*Template field > Header Text> Address
*Template field > Header Text> Salary
*Template field > Header Text> (Blank)

# DataView > Tasklist> Edit templates

# Empno

*Item Template
Add Label > Edit DataBindings > Eval(“empno”)
*Edit Item Template
Add Label > Edit DataBindings > Eval(“empno”)
*Insert Item Template
Add Textbox

# Name

*Item Template
Add Label > Edit DataBindings > Eval(“ename”)
*Edit Item Template
Add Textbox > Edit DataBindings > Eval(“ename”)
*Insert Item Template
Add Textbox

#Address

*Item Template
Add Label > Edit DataBindings > Eval(“eadd”)
*Edit Item Template
Add Textbox > Edit DataBindings > Eval(“eadd”)
*Insert Item Template
Add Textbox

#Salary

*Item Template
Add Label > Edit DataBindings > Eval(“esal”)
*Edit Item Template
Add Textbox > Edit DataBindings > Eval(“esal”)
*Insert Item Template
Add Textbox

#Field

*Item Template
Add LinkButton> Properties > Text=Edit > Command Name= edit
Add LinkButton> Properties > Text=New > Command Name= new
Add LinkButton> Properties > Text=Delete > Command Name= delete
*Edit Item Template
Add LinkButton> Properties > Text=Update > Command Name= update
Add LinkButton> Properties > Text=Cancel > Command Name= cancel
*Insert Item Template
Add LinkButton> Properties > Text=Insert > Command Name= insert
Add LinkButton> Properties > Text=Cancel> Command Name= cancel
#Command Name= New (It will call Insert Item Template)
#Command Name= insert (It will fire Item Inserting Event (to save))
#DetailView > Properties> Allow paging= True

# DetailView > Properties> Events > Page Index Changing

protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        DetailsView1.PageIndex = e.NewPageIndex;
        Det_Bind();
    }

# Drawbacks of Inbuilt Paging

-         It will bind all the records in the table for example 200,000 but show one record
-         Low performance

# Mode Concept in DetailView

In DetailView there are 3 modes=
1.     Edit Mode= It will call Edit Item Template
2.     Insert Mode = It will call Insert Item Template
3.     Read Only Mode = It will call Item Template

# DetailView > Properties> Events > Mode Changing

protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        DetailsView1.ChangeMode(e.NewMode);
        Det_Bind();
    }

NewMode – When clicked on Edit, New, Cancel respective templates will be called

# Save Code

* DetailView > Properties> Events > Item Inserting
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        Int32 eno, es;
        String en, ed;
        eno = 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] = eno;
        ar[1] = en;
        ar[2] = ed;
        ar[3] = es;
        SqlHelper.ExecuteNonQuery(psg, "insemp", ar);
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        Det_Bind();
    }

Object[] ar = new Object[4]-- 4 is number of elements in table or parameter
DetailsView1.ChangeMode(DetailsViewMode.ReadOnly)—To call Item Template

# For Update

* DetailView > Properties> Events > Item Updating
protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        Int32 eno, es;
        String en, ed;
        eno = 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] = eno;
        ar[1] = en;
        ar[2] = ed;
        ar[3] = es;
        SqlHelper.ExecuteNonQuery(psg, "updemp", ar);
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        Det_Bind();
    }

# For Delete

* DetailView > Properties> Events > Item Deleting
protected void DetailsView1_ItemDeleting(object sender, DetailsViewDeleteEventArgs e)
    {
        Int32 eno;
        eno = Convert.ToInt32(((Label)(DetailsView1.Rows[0].FindControl("Label1"))).Text);
        Object[] ar = new Object[1];
        ar[0] = eno;
        SqlHelper.ExecuteNonQuery(psg, "delemp", ar);
        Det_Bind();
    }

# Empty Data Template – When all records are deleted or there is no record in table
# DataView > Tasklist> Edit templates > Empty Data Template
Add Button > Text = Add More Data > Command Name = new

No comments:

Post a Comment