Working FormView in Asp.net Ado.net way C#
Hello there, In this article i am explaining how to use formview control. Unlike the DetailsView, the FormView is not composed of fields. You can’t add a BoundField or TemplateField to a FormView Conttrol. The formView control is completely template driven You can also add validation controls Instead, the FormView is rendered using templates. Think of the FormView as a DetailsView control that contains a single TemplateField. The FormView supports the following templates:
- ItemTemplate render the particular record displayed in the FormView Control.
- HeaderTemplate specify an optional header row.
- FooterTemplate specify an optional footer row.
- EmptyDataTemplate when the FormView’s DataSource lacks any records,EmptyDataTemplate is used in place of the ItemTemplate for rendering the control’s.
- PagerTemplate used to customize the paging interface for FormViews that have paging enabled.
- EditItemTemplate / InsertItemTemplate used to customize editing interface or inserting interface for FormViews that support such functionality.
FormView control to:
- Display
- Insert
- Edit
- Delete
- Page
Formview control has a property name Defaultmode in which there are three options ReadOnly, Edit and Insert. These mode will fire for Editing the Deleting the records as this control behaviour little different than other controls. Here we start copy and paste the following database script and run it into your sql server.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id =OBJECT_ID(N‘[dbo].[tbformview]‘) AND type in (N‘U’))
BEGIN
CREATE TABLE [dbo].[tbformview](
[id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[Class] [varchar](50) NULL,
[Address] [varchar](50) NULL
) ON [PRIMARY]
END
Now, create a web form and named it Formview.aspx and paste the whole following source code in it.
<%@ Page Language=”C#” AutoEventWireup=”true”CodeFile=”Formview.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>Untitled Page</title>
<style type=”text/css”>
.style1
{
width: 26%;
height: 103px;
}
.style2
{
width: 109px;
}
.style4
{
width: 100%;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table class=”style1″>
<tr>
<td class=”style2″>
Name
</td>
<td>
<asp:TextBox ID=”Txt_Name” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
Class
</td>
<td>
<asp:TextBox ID=”Txt_Class” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
Address
</td>
<td>
<asp:TextBox ID=”Txt_Address” runat=”server”></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
</td>
<td>
<asp:Button ID=”Bt_Submit” runat=”server” Height=”32px”OnClick=”Bt_Submit_Click”
Text=”Submit” Width=”110px” />
</td>
</tr>
</table>
</div>
<asp:FormView ID=”FormView1″ runat=”server” AllowPaging=”True”
onitemdeleting=”FormView1_ItemDeleting1″
onitemupdating=”FormView1_ItemUpdating1″
onmodechanging=”FormView1_ModeChanging”
onpageindexchanging=”FormView1_PageIndexChanging1″ Width=”259px”>
<PagerSettings PageButtonCount=”3″ />
<EditItemTemplate>
<table class=”style3″>
<tr>
<td>
Name</td>
<td>
<asp:TextBox ID=”Txt_ename” runat=”server” Text=’<%# Eval(“Name”)%>‘></asp:TextBox>
</td>
</tr>
<tr>
<td>
Class</td>
<td>
<asp:TextBox ID=”Txt_eclass” runat=”server” Text=’<%# Eval(“Class”) %>‘></asp:TextBox>
</td>
</tr>
<tr>
<td>
Address</td>
<td>
<asp:TextBox ID=”Txt_eaddress” runat=”server” Text=’<%# Eval(“Address”) %>‘></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Label ID=”lbl_edit” runat=”server” Text=’<%# Eval(“id”) %>‘></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:LinkButton ID=”btn_update” runat=”server”CommandName=”update”>Update</asp:LinkButton>
<asp:LinkButton ID=”btn_cancel” runat=”server”CommandName=”cancel”>Cancel</asp:LinkButton>
</td>
</tr>
</table>
<br />
<br />
<br />
<br />
</EditItemTemplate>
<ItemTemplate>
<table class=”style4″>
<tr>
<td>
Name :</td>
<td>
<asp:Label ID=”lbl_Name” runat=”server” Text=’<%# Eval(“Name”)%>‘></asp:Label>
</td>
</tr>
<tr>
<td>
Class :</td>
<td>
<asp:Label ID=”class” runat=”server” Text=’<%# Eval(“Class”) %>‘></asp:Label>
</td>
</tr>
<tr>
<td>
Address :</td>
<td>
<asp:Label ID=”lbl_address” runat=”server” Text=’<%# Eval(“Address”) %>‘></asp:Label>
</td>
</tr>
</table>
<asp:Label ID=”lbl_delete” runat=”server” Text=’<%# Eval(“id”) %>‘
Visible=”False”></asp:Label>
<br />
<br />
<asp:LinkButton ID=”btn_edit” runat=”server”CommandName=”edit”>Edit</asp:LinkButton>
<asp:LinkButton ID=”btn_delete” runat=”server”CommandName=”delete”>Delete</asp:LinkButton>
<br />
</ItemTemplate>
</asp:FormView>
</form>
</body>
</html>
*Set the AllowPaging= True and PagerSetting->PageButtonCount=3(for paging)
Now following step will tell you how to add the Events into your code.
Coding
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; // add the namespace
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(); //is used for connection
SqlCommand cmd;
SqlDataAdapter adp; // is used for fetch the data from database
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString =ConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
con.Open();
if (con.State ==ConnectionState.Closed )
{
con.Open();
}
con.Close();
if (IsPostBack ==false ) //to show the Data on runtime
{
frmview(); //calling the function of formview
}}
private void frmview() // to make a function of formview
{
if (con.State==ConnectionState .Closed )
{
con.Open();
}
adp = new SqlDataAdapter(“select * from tbformview”, con);//access the data from database
dt = new DataTable();
adp.Fill(dt); //insert the data from database into datatable
adp.Dispose(); //
if (dt.Rows.Count ==0) //check the condition if datatable is empty
{
Response.Write(“NO Record Found”); // display the message
}
else
{
FormView1.DataSource = dt; //
FormView1.DataBind(); //bind the data to collection server control,which is fetched from its datasource
}
con.Close();
}
protected void Bt_Submit_Click(object sender, EventArgs e)
{
if (con.State==ConnectionState .Closed ) // to take condition if connection is closed
{
con.Open(); // Open the Connection
}
cmd = new SqlCommand(“insert into tbformview values(@Name,@Class,@Address)”, con); // insert the record into database
cmd.Parameters.AddWithValue(“@Name”, Txt_Name.Text);
cmd.Parameters.AddWithValue(“@Class”,Txt_Class .Text);
cmd.Parameters .AddWithValue (“@Address”,Txt_Address.Text);
cmd.ExecuteNonQuery (); // to used for insert the data & update
cmd.Dispose();
frmview(); //calling the function of formview
txtclr(); //calling the function to clear the textbox after inserting the record into database
con.Close ();
}
private void txtclr()// to make a function to clear the textbox
{
Txt_Name.Text = String.Empty;
Txt_Class.Text = String.Empty;
Txt_Address.Text = String.Empty;
}
protected void FormView1_ItemDeleting1(object sender,FormViewDeleteEventArgs e)
{
Label id = ((Label)(FormView1.Row.FindControl(“lbl_delete”)));
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand(“delete from tbformview where id=@id”, con);
cmd.Parameters.AddWithValue(“@id”, id.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
FormView1.ChangeMode (FormViewMode .ReadOnly );
frmview();
}
protected void FormView1_ItemUpdating1(object sender,FormViewUpdateEventArgs e)
{
Label id = ((Label)(FormView1.Row.FindControl(“lbl_edit”)));
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd = new SqlCommand(“update tbformview set Name=@Name,Class=@Class,Address=@Address where id=@id”, con);
cmd.Parameters.AddWithValue(“@Name”, ((TextBox)(FormView1.Row.FindControl(“Txt_ename”))).Text);
cmd.Parameters.AddWithValue(“@Class”, ((TextBox)(FormView1.Row.FindControl(“Txt_eclass”))).Text);
cmd.Parameters.AddWithValue(“@Address”, ((TextBox)(FormView1.Row.FindControl(“Txt_eaddress”))).Text);
cmd.Parameters.AddWithValue(“@id”, id.Text);
cmd.ExecuteNonQuery();
cmd.Dispose();
FormView1.ChangeMode(FormViewMode.ReadOnly);
frmview();
con.Close();
}
protected void FormView1_PageIndexChanging1(object sender,FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex; //paging
frmview();
}
protected void FormView1_ModeChanging(object sender,FormViewModeEventArgs e//mode of formsview
{
if (e.NewMode == FormViewMode.Edit)
FormView1.ChangeMode(FormViewMode.Edit); //will change into Edit Mode
else
FormView1.ChangeMode(FormViewMode.ReadOnly); //Will Change into Template mode.
frmview();
}}
Snapshots
Editing Record
Updating Record
No comments:
Post a Comment