matthew_360
matthew_360

Reputation: 6061

using FormView to insert

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?

Upvotes: 1

Views: 2248

Answers (3)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

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 FormView_Load being overwritten C# ASP.NET

Upvotes: 2

Phaedrus
Phaedrus

Reputation: 8421

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
    }
}

Upvotes: 0

Jose Basilio
Jose Basilio

Reputation: 51468

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 ...
  }
}

Upvotes: 0

Related Questions