techturtle
techturtle

Reputation: 2587

Is there a way to tell if my FormView is in Edit mode?

I have some code for my FormView that fires on the DataBound event. Unfortunately (for what I'm doing, anyway) it fires the same whether it's first rendering the page or if I just clicked Edit. I need it to do a few things differently if it's being run on the ItemTemplate verses the EditItemTemplate. My searches on the subject have thus far been fruitless. Is there a simple way to do something along the lines of if(IsEditItemTemplate) ?

Upvotes: 1

Views: 2143

Answers (2)

alwaysVBNET
alwaysVBNET

Reputation: 3310

Better use the proper function:

 Private Sub EmployeeFormView_ModeChanged(sender As Object, e As EventArgs)
 Handles EmployeeFormView.ModeChanged

Upvotes: 0

Shai Cohen
Shai Cohen

Reputation: 6249

FormView.CurrentMode is your friend

More explanation here


From the quoted site:

Mode                   Description
FormViewMode.Edit      The FormView control is in edit mode, which allows the 
                       user to update the values of a record.
FormViewMode.Insert    The FormView control is in insert mode, which allows the 
                       user to add a new record to the data source.
FormViewMode.ReadOnly  The FormView control is in read-only mode, which is the 
                       normal display mode.

Sample Code

void EmployeeFormView_OnPageIndexChanging(Object sender, FormViewPageEventArgs e)
{
    // Cancel the paging operation if the user attempts to navigate 
    // to another record while the FormView control is in edit mode. 
    if (EmployeeFormView.CurrentMode == FormViewMode.Edit)
    {
        e.Cancel = true;
        MessageLabel.Text = 
          "Please complete the update before navigating to another record.";
    }
}

Upvotes: 2

Related Questions