views:

401

answers:

3

I have a formview control, and on the ItemCreated event, I am "priming" some of the fields with default values.

However, when I try to use the formview to insert, before the ItemInserting event gets called, for some reason it calls ItemCreated first. That results in the fields being over-written with the default values right before the insert happens.

How do I get it to not call the ItemCreated event before the ItemInserting event?

A: 

You cannot change the order in which the events fire. However, you should probably wrap the code that sets the default values inside !IsPostBack so that it doesn't reset your values for example:

protected void FormView_ItemCreated(Object sender, EventArgs e)
{
  if(!IsPostBack)
  {
    //Set default values ...
  }
}
Jose Basilio
That won't work because when I actually do want to hit the ItemCreated, it is on a postback.
matthew_360
In that case, you may have to add an If statement checking whether there's a value before setting it.
Jose Basilio
A: 

Try checking the CurrentMode property of the form view.

void FormView_ItemCreated(object sender, EventArgs e)
{
    if (FormView.CurrentMode != FormViewMode.Insert)
    {
        //Initialize your default values here
    }
}
Phaedrus
A: 

you need to use formview Databound event instead of formview ItemCreated event to set values, try like

protected void frm_DataBound(object sender, EventArgs e)
{
    if (frm.CurrentMode == FormViewMode.Edit)//whatever your mode here is.
    {
        TextBox txtYourTextBox = (TextBox)frm.FindControl("txtYourTextBox");
        txtYourTextBox.Text// you can set here your Default value
    }
}

Also check this thread of similare issue http://stackoverflow.com/questions/1574860/formviewload-being-overwritten-c-asp-net/1576174#1576174

Muhammad Akhtar