Domanda

This popped up, when I was trying to find why the OnModeChanging handler wasn't being called when I called the ChangeMode event of my formview.

On the formview's ChangeMode method MSDN page , it is stated that it:

switches the FormView control to the specified data-entry mode

but also that:

the ModeChanged and ModeChanging events are not raised when this method is called

And in the ModeChanged and ModeChanging events pages, it says that they occur:

when the FormView control switches between edit, insert, and read-only mode

after/before the mode changes, respectively.

Can you explain it to me: when are the ModeChanged/ing events raised?

And, is there a way to force these events to be raised?

È stato utile?

Soluzione

I think I know why now. I've found an answer in other forum, and though I didn't find the code of FormView, I've found a DetailsView implementation and I think in this case it might be similar.

Basically what I've understood of it, is that the ModeChanged/ing events are raised when command buttons are clicked (Cancel, Edit, Insert, New and Update), i.e. when one doesn't have direct control over these events, and when we use the ChangeMode method, we know that the mode has changed (or will be changed) and it would make no sense of raising an event..

DetailsView ChangeMode:

public void ChangeMode(DetailsViewMode newMode) {
    Mode = newMode;
}

DetailsView command handlers:

private void HandleCancel() {
    bool isBoundToDataSourceControl = IsBoundUsingDataSourceID;

    DetailsViewModeEventArgs e = new DetailsViewModeEventArgs(DefaultMode, true);
    OnModeChanging(e);

    if (e.Cancel) {
        return;
    }

    if (isBoundToDataSourceControl) {
        Mode = e.NewMode;
        OnModeChanged(EventArgs.Empty);
    }

    RequiresDataBinding = true;
}



private void HandleEdit() {
    if (PageIndex < 0) {
        return;
    }

    DetailsViewModeEventArgs e = new DetailsViewModeEventArgs(DetailsViewMode.Edit, false);
    OnModeChanging(e);

    if (e.Cancel) {
        return;
    }

    if (IsBoundUsingDataSourceID) {
        Mode = e.NewMode;
        OnModeChanged(EventArgs.Empty);
    }

    RequiresDataBinding = true;
}

private bool HandleInsertCallback(int affectedRows, Exception ex) {
    DetailsViewInsertedEventArgs dea = new DetailsViewInsertedEventArgs(affectedRows, ex);
    dea.SetValues(_insertValues);
    OnItemInserted(dea);

    _insertValues = null;
    if (ex != null && !dea.ExceptionHandled) {
        if (PageIsValidAfterModelException()) {
            return false;
        }
        dea.KeepInInsertMode = true;
    }

    if (!dea.KeepInInsertMode) {
        DetailsViewModeEventArgs eMode = new DetailsViewModeEventArgs(DefaultMode, false);
        OnModeChanging(eMode);
        if (!eMode.Cancel) {
            Mode = eMode.NewMode;
            OnModeChanged(EventArgs.Empty);
            RequiresDataBinding = true;
        }
    }
    return true;
}

private void HandleNew() {
    DetailsViewModeEventArgs e = new DetailsViewModeEventArgs(DetailsViewMode.Insert, false);
    OnModeChanging(e);

    if (e.Cancel) {
        return;
    }

    if (IsBoundUsingDataSourceID) {
        Mode = e.NewMode;
        OnModeChanged(EventArgs.Empty);
    }

    RequiresDataBinding = true;
}


private bool HandleUpdateCallback(int affectedRows, Exception ex) {
    DetailsViewUpdatedEventArgs dea = new DetailsViewUpdatedEventArgs(affectedRows, ex);
    dea.SetOldValues(_updateOldValues);
    dea.SetNewValues(_updateNewValues);
    dea.SetKeys(_updateKeys);

    OnItemUpdated(dea);

    _updateKeys = null;
    _updateOldValues = null;
    _updateNewValues = null;

    if (ex != null && !dea.ExceptionHandled) {
        if (PageIsValidAfterModelException()) {
            return false;
        }
        dea.KeepInEditMode = true;
    }

    if (!dea.KeepInEditMode) {
        DetailsViewModeEventArgs eMode = new DetailsViewModeEventArgs(DefaultMode, false);
        OnModeChanging(eMode);
        if (!eMode.Cancel) {
            Mode = eMode.NewMode;
            OnModeChanged(EventArgs.Empty);
            RequiresDataBinding = true;
        }
    }
    return true;
}

Altri suggerimenti

With ChangeMode you are choosing that the control switch to one of it's modes. When it starts to performing this task, the ModeChanging event is raised (to indicate that it's in progress) (optionally do something here). Once that task is completed, it raises the ModeChanged event (to indicate that it's done) (optionally do something here).

[Updated] I see your point. How could you consume the events if they don't get raised.

I'm going to guess at it, that they don't get raised initially because of nothing to do, just perform the changing of the mode.

In either case I guess, it's more of a state change than the raising of events.

[Updated] I think what we are both saying is that if no one has subscribed to the event (i.e., no one is listening for it), there's no point in raising it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top