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();
}
|
No comments:
Post a Comment