Tutorial :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?


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


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


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

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »