Asp.net formview control
Links to Category table used to bind data in Formview click here
Step1: Drag formView control form data controls in Toolbox
<asp:FormView ID=”FormView1″ runat=”server”>
</asp:FormView>
Click Edit Template
formview editTemplate
ItemTemplate: used to display data (Edit Button,New Button)
EditItemTemplate: used to edit data (update button)
InsertItemTemplate: used to insert new record (Save Button)
ItemTemplate: insert table with 3 rows and 2 columns
Drag 2 labels and 2 Buttons
Bind Label1 to CategoryName
<asp:Label ID=”Label1″ runat=”server” Text=’<%# Eval(”CategoryName”) %>’></asp:Label>
Bind Label2 to CategoryDesc
<asp:Label ID=”Label2″ runat=”server” Text=’<%# Eval(”CategoryDesc”) %>’></asp:Label>
Given CommandName Properties to Edit Button and AddNew Button
<asp:Button CommandName=”Edit” Text=”Edit” ID=”EditFrm” runat=”server” />
<asp:Button CommandName=”Insert” Text=”Add New” ID=”InsertFrm” runat=”server” />
Complete code for ItemTemplate in Formview control
<ItemTemplate>
<table>
<tr>
<td class=”style2″>
CategoryName</td>
<td>
<asp:Label ID=”Label1″ runat=”server” Text=’<%# Eval(”CategoryName”) %>’> </asp:Label>
</td>
</tr>
<tr>
<td class=”style2″>
CategoryDesc</td>
<td>
<asp:Label ID=”Label2″ runat=”server” Text=’<%# Eval(”CategoryDesc”) %>’> </asp:Label>
</td>
</tr>
<tr>
<td class=”style2″>
</td>
<td>
<asp:Button CommandName=”Edit” Text=”Edit” ID=”EditFrm” runat=”server” />
<asp:Button CommandName=”Insert” Text=”Add New” ID=”InsertFrm” runat=”server” />
</td>
</tr>
</table>
</ItemTemplate>
FormView Edit Template
Step1: insert table with 3 rows and 2 columns
Drag 2 textbox’s and 2 Buttons
Update Button and Cancel Button
<asp:Button CommandName=”update” Text=”update” ID=”EditFrm” runat=”server” />
<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”EditCancelFrm” runat=”server” />
Complete code for FormView EditTemplate Field
<EditItemTemplate>
<table>
<tr>
<td class=”style2″>
CategoryName</td>
<td>
<asp:TextBox ID=”txtCatName” runat=”server”
Text=’<%# Eval(”CategoryName”) %>‘></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
CategoryDesc</td>
<td>
<asp:TextBox ID=”txtCatDesc” runat=”server”
Text=’<%# Eval(”CategoryDesc”) %>’></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
</td>
<td>
<asp:Button CommandName=”update” Text=”update” ID=”EditFrm” runat=”server” />
<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”EditCancelFrm” runat=”server” />
</td>
</tr>
</table>
</EditItemTemplate>
FormView InsertTemplate Field
Step1: insert table with 3 rows and 2 columns
Drag 2 textbox’s and 2 Buttons
Save Button and Cancel Button
<asp:Button CommandName=”Save” Text=”Save” ID=”SaveFrm” runat=”server” />
<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”CancelFrm” runat=”server” />
Complete code for FormView InsertTemplate Field
<InsertItemTemplate>
<table>
<tr>
<td class=”style2″>
CategoryName</td>
<td>
<asp:TextBox ID=”InsCatName” runat=”server”
></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
CategoryDesc</td>
<td>
<asp:TextBox ID=”InsCatDesc” runat=”server”
></asp:TextBox>
</td>
</tr>
<tr>
<td class=”style2″>
</td>
<td>
<asp:Button CommandName=”Save” Text=”Save” ID=”SaveFrm” runat=”server” />
<asp:Button CommandName=”Cancel” Text=”Cancel” ID=”CancelFrm” runat=”server” />
</td>
</tr>
</table>
</InsertItemTemplate>
Code for Default.aspx.cs
using System.Data;
using System.Data.SqlClient;
#region “ConnectionString”
string ConnectionString = “Data Source=192.168.1.23;Initial Catalog=Purchase;User;Password=abc;”;
#endregion
#region “Page_Load”
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetCategories();
}
}
#endregion
#region “GetCategories()”
public void GetCategories()
{
SqlConnection objConn = new SqlConnection(ConnectionString);
string query = “select *from tblCategory”;
SqlCommand objCmd = new SqlCommand(query, objConn);
objCmd.CommandType = CommandType.Text;
DataSet objDs = new DataSet();
SqlDataAdapter objDa = new SqlDataAdapter(objCmd);
//objConn.Open();
objDa.Fill(objDs);
//objCmd.ExecuteNonQuery();
FormView1.DataSource = objDs;
FormView1.DataBind();
//objConn.Close();
}
#endregion
Asp.net FormView Paging
protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
FormView1.PageIndex = e.NewPageIndex;
GetCategories();
FormView1.ChangeMode(FormViewMode.ReadOnly);
}
FormView Edit, Delete, Update and Insert Events
protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
//this commandName from ItemTemplate
if (e.CommandName == “Edit”)
{
FormView1.ChangeMode(FormViewMode.Edit);
GetCategories();
}
//this commandName from ItemTemplate
if (e.CommandName == “Insert”) {
FormView1.ChangeMode(FormViewMode.Insert);
}
//this commandName from EditItemTemplate
if (e.CommandName == “update”)
{
SqlConnection objConn = new SqlConnection(ConnectionString);
string catName = ((TextBox)FormView1.FindControl(”txtCatName”)).Text;
string catDesc = ((TextBox)FormView1.FindControl(”txtCatDesc”)).Text;
objConn.Open();
string query = “Update tblCategory Set categoryname = ‘” + catName + “‘, CategoryDesc = ‘” + catDesc + “‘ Where categoryID=’” + FormView1.DataKey.Value + “‘”;
SqlCommand objCmd = new SqlCommand(query, objConn);
objCmd.ExecuteNonQuery();
objConn.Close();
//FormView1.ChangeMode(FormViewMode.Edit);
GetCategories();
}
//commandName from InsertItemTemplate
if (e.CommandName == “Save”)
{
SqlConnection objConn = new SqlConnection(ConnectionString);
string catName = ((TextBox)FormView1.FindControl(”InsCatName”)).Text;
string catDesc = ((TextBox)FormView1.FindControl(”InsCatDesc”)).Text;
objConn.Open();
string query = “insert into tblCategory(CategoryName,CategoryDesc) ”
+ “values(@CategoryName,@CategoryDesc)”;
SqlCommand objCmd = new SqlCommand(query, objConn);
objCmd.Parameters.Add(”@CategoryName”, SqlDbType.VarChar).Value = catName;
objCmd.Parameters.Add(”@CategoryDesc”, SqlDbType.VarChar).Value = catDesc;
objCmd.ExecuteNonQuery();
objConn.Close();
FormView1.ChangeMode(FormViewMode.ReadOnly);
GetCategories();
}
//this commandName from EditItemTemplate and InsertItemTemplate
if (e.CommandName == “Cancel”)
{
FormView1.ChangeMode(FormViewMode.ReadOnly);
GetCategories();
}
}
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
}
protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
{
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
//FormView1.ChangeMode(FormViewMode.ReadOnly);
}

