Showing posts with label c-sharp. Show all posts
Showing posts with label c-sharp. Show all posts

Monday, October 21, 2019

How to Work with FormView Control in Asp.net


FormView Control In Asp.net


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



# FormView

-         It is similar to DetailView
-         In it we can display single record
-         No rows or Column (Customised view and all Feature details)
-         Paging is inbuilt

# Templates applied

-         Item Template
-         Header Template
-         Footer Template
-         Edit Item Template
-         Pager Template
-         Insert Item Template
-         Empty Data Template

# Open Visual Studio and Make new Project

*Add new webform
*Toolbox > Data > FormView

#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 Default2 : System.Web.UI.Page
{
    String psg;
    protected void Page_Load(object sender, EventArgs e)
    {
        psg = ConfigurationManager.ConnectionStrings["ps"].ConnectionString;
        if (Page.IsPostBack == false)
        {
            Form_Bind();
        }
    }

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

# Design

*FormView> Tasklist> Edit Template

# Item template

Empno : Label > Edit Data Binding > Eval(“empno”)
Name : Label > Edit Data Binding > Eval(“ename”)
Address : Label > Edit Data Binding > Eval(“eadd”)
Salary : Label > Edit Data Binding > Eval(“esal”)
Add Link Button > Property > Text = Edit > Command Name= edit
Add Link Button > Property > Text = Delete> Command Name= delete
Add Link Button > Property > Text = New> Command Name= new

# Edit Item template

Empno : Label > Edit Data Binding > Eval(“empno”)
Name : TextBox > Edit Data Binding > Eval(“ename”)
Address : TextBox > Edit Data Binding > Eval(“eadd”)
Salary : TextBox > Edit Data Binding > Eval(“esal”)
Add Link Button > Property > Text = Update > Command Name= update
Add Link Button > Property > Text = Cancel> Command Name= cancel

# Insert Item template

Empno : TextBox
Name : TextBox
Address : TextBox
Salary : TextBox
Add Link Button > Property > Text = Insert > Command Name= insert
Add Link Button > Property > Text = Cancel> Command Name= cancel

# Empty Data Template

Add Button > Text = Add More Data > Command Name= new
#FormView > Properties> Allow paging= True

# FormView > Properties> Events > Page Index Changing

protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
    {
        FormView1.PageIndex = e.NewPageIndex;
        Form_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 FormView

In FormView 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

# FormView > Properties> Events > Mode Changing

protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
    {
        FormView1.ChangeMode(e.NewMode);
        Form_Bind();
    }

# Save Code

* FormView > Properties> Events > Item Inserting
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        Int32 eno, es;
        String en, ed;
        eno = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox4"))).Text);
        en = ((TextBox)(FormView1.FindControl("TextBox5"))).Text;
        ed = ((TextBox)(FormView1.FindControl("TextBox6"))).Text;
        es = Convert.ToInt32(((TextBox)(FormView1.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);
        FormView1.ChangeMode(FormViewMode.ReadOnly);
        Form_Bind();
    }

# For Update

* FormView > Properties> Events > Item Updating
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        Int32 eno, es;
        String en, ed;
        eno = Convert.ToInt32(((Label)(FormView1.FindControl("Label5"))).Text);
        en = ((TextBox)(FormView1.FindControl("TextBox1"))).Text;
        ed = ((TextBox)(FormView1.FindControl("TextBox2"))).Text;
        es = Convert.ToInt32(((TextBox)(FormView1.FindControl("TextBox3"))).Text);
        Object[] ar = new Object[4];
        ar[0] = eno;
        ar[1] = en;
        ar[2] = ed;
        ar[3] = es;
        SqlHelper.ExecuteNonQuery(psg, "updemp", ar);
        FormView1.ChangeMode(FormViewMode.ReadOnly);
        Form_Bind();
    }

# For Delete

* FormView > Properties> Events > Item Deleting
protected void FormView1_ItemDeleting(object sender, FormViewDeleteEventArgs e)
    {
        Int32 eno;
        eno = Convert.ToInt32(((Label)(FormView1.FindControl("Label1"))).Text);
        Object[] ar = new Object[1];
        ar[0] = eno;
        SqlHelper.ExecuteNonQuery(psg, "delemp", ar);
        Form_Bind();
    }


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