// aspx page
// aspx.cs page:
public partial class CarBodyDetails : BasePage
{
private int _id = 0;
public int Id
{
get { return _id; }
set { _id = value; }
}
private const string CarBodyDuplicateErrorMessage = "Aceasta caroserie exista in baza de date.";
protected void Page_Load(object sender, EventArgs e)
{
SetFormViewMode();
BindFormView(this.Id);
}
protected void SetFormViewMode()
{
this.FormView1.DefaultMode = FormViewMode.Insert;
int carBodyID = 0;
if (Request.QueryString["id"] != null)
{
try { carBodyID = Int32.Parse(Request.QueryString["id"]); }
catch { }
}
if (carBodyID > 0)
{
this.FormView1.DefaultMode = FormViewMode.Edit;
this.Id = carBodyID;
}
}
private void BindFormView(int id)
{
if (this.FormView1.CurrentMode == FormViewMode.Edit)
{
CarBody carBody = new CarBody(id);
List list = new List(1);
list.Add(carBody);
this.FormView1.DataSource = list;
this.FormView1.DataBind();
/*
((TextBox)FormView1.FindControl("NameTextBox")).Text = carBody.Name;
((CheckBox)this.FormView1.FindControl("ArchivedCheckBox")).Checked = carBody.Archived;
*/
}
}
protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
{
if (Page.IsValid)
{
string name = e.Values["Name"].ToString();
if (CarBody.Exists(name))
{
// cancel insert operation
e.Cancel = true;
// display error message
this.ShowMessage(CarBodyDuplicateErrorMessage);
}
else
{
// insert item
}
}
}
protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
if (Page.IsValid)
{
string name = ((TextBox)FormView1.FindControl("NameTextBox")).Text;
// int carBodyId = int.Parse(Request.QueryString["id"]);
if (CarBody.Exists(name, Id))
{
// cancel update operation
e.Cancel = true;
this.ShowMessage(CarBodyDuplicateErrorMessage);
}
else
{
// update item
bool archived = ((CheckBox)this.FormView1.FindControl("ArchivedCheckBox")).Checked;
CarBody .Update(Id, name, archived);
this.RefreshParentWindow();
this.ClosePopup();
}
}
}
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
this.RefreshParentWindow();
this.ClosePopup();
}
protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
{
this.RefreshParentWindow();
this.ClosePopup();
}
}