Since I am having so many problems with using a detailsview 2010 control, I am going to convert my logic to use a formview control instead. Thus can you tell me if there is a referenceon on how to convert and/or copy detailsview code to formsview code?
Basically I am going to take the itemtemplate and editiemtemplates and hopefully use them in the formwview control.
I will place the update and clear buttons outside of the formsview control but on the webform page. Thus to be able to update the data on the buttons outside of the the formsview would I use a updateitem method or some other method?
Loading
Satyapriya NayakPosted Dec 3, 2011, 11:03 PM
Formview edit insert delete update using buttons
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Thanks
If this post helps you mark it as answer
Sie StePosted Dec 3, 2011, 5:40 PM
Is the EmployeeFormView_ModeChanging event and/or
EmployeeFormView_PageIndexChanging event called? If so, can you explain?
What event does the CancelUpdateButton call? I want to place my cancel button on a 2010 web forms control page but not within the formsview control.
Satyapriya NayakPosted Dec 3, 2011, 12:21 PM
Try this...
Formview edit insert delete update
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
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;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter sqlda = new SqlDataAdapter();
SqlCommand com = new SqlCommand();
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection conn = new SqlConnection(connStr);
dt = new DataTable();
com.Connection = conn;
com.CommandText = "SELECT * FROM Employees";
sqlda = new SqlDataAdapter(com);
sqlda.Fill(dt);
EmployeeFormView.DataSource = dt;
EmployeeFormView.DataBind();
}
protected void EmployeeFormView_PageIndexChanging(object sender, FormViewPageEventArgs e)
{
EmployeeFormView.PageIndex = e.NewPageIndex;
bindgrid();
}
protected void EmployeeFormView_ItemDeleting(object sender, FormViewDeleteEventArgs e)
{
DataKey key = EmployeeFormView.DataKey;
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "DELETE FROM Employees WHERE EmployeeID='" + key.Value.ToString() + "'";
conn.Open();
com.ExecuteNonQuery();
conn.Close();
Response.Write( "Record deleted successfully");
bindgrid();
}
protected void EmployeeFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
DataKey key = EmployeeFormView.DataKey;
TextBox txtFirstName = (TextBox)EmployeeFormView.FindControl("txtFirstName2");
TextBox txtLastName = (TextBox)EmployeeFormView.FindControl("txtLastName2");
TextBox txtAddress = (TextBox)EmployeeFormView.FindControl("txtAddress2");
TextBox txtDesignation = (TextBox)EmployeeFormView.FindControl("txtDesignation2");
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "UPDATE Employees SET FirstName ='" + txtFirstName.Text + "',LastName ='" + txtLastName.Text + "',Address ='" + txtAddress.Text + "',Designation ='" + txtDesignation.Text + "' WHERE EmployeeID='" + key.Value.ToString() + "'";
conn.Open();
com.ExecuteNonQuery();
Response.Write("Record updated successfully");
bindgrid();
conn.Close();
}
protected void EmployeeFormView_ModeChanging(object sender, FormViewModeEventArgs e)
{
EmployeeFormView.ChangeMode(e.NewMode);
bindgrid();
if (e.NewMode == FormViewMode.Edit)
{
EmployeeFormView.AllowPaging = false;
}
else
{
EmployeeFormView.AllowPaging = true;
}
}
protected void EmployeeFormView_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
}
protected void EmployeeFormView_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
}
protected void EmployeeFormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
TextBox txtEmployeeID = (TextBox)EmployeeFormView.FindControl("txtEmployeeID1");
TextBox txtFirstName = (TextBox)EmployeeFormView.FindControl("txtFirstName1");
TextBox txtLastName = (TextBox)EmployeeFormView.FindControl("txtLastName1");
TextBox txtAddress = (TextBox)EmployeeFormView.FindControl("txtAddress1");
TextBox txtDesignation = (TextBox)EmployeeFormView.FindControl("txtDesignation1");
SqlConnection conn = new SqlConnection(connStr);
com.Connection = conn;
com.CommandText = "INSERT INTO Employees Values('" + txtEmployeeID.Text + "','" + txtFirstName.Text + "', '" + txtLastName.Text + "', '" + txtAddress.Text + "', '" + txtDesignation.Text + "')";
conn.Open();
com.ExecuteNonQuery();
Response.Write("Record inserted successfully");
bindgrid();
conn.Close();
}
}
Thanks