Here is the scenario: You have a FormView you are using mostly to edit or insert records, but not display them. You've set the DefaultMode to Insert so the user sees the InsertItemTemplate first. After the Insert, you want to change modes to either Edit or ReadOnly, so you add some code to do this on the ItemInserted event:
protected void fvItemInserted(object sender, FormViewInsertedEventArgs e) {
fv.ChangeMode(FormViewMode.ReadOnly);
}
This doesn't work - why? The reason is the DefaultMode is applied after your call to change mode. If this is by design or bug, I can't find any information to confirm, but it's there. So how do you stop the DefaultMode from overriding your explicit setting? With the following addition:
protected void fvItemInserted(object sender, FormViewInsertedEventArgs e) {
e.KeepInInsertMode = true;
fv.ChangeMode(FormViewMode.ReadOnly);
}
Holy confusing syntactical attacks Batman! What have you done? Relax Robin, things are not what they seem. In an effort to label properties by what they do versus what they are, Microsoft has jumped the gun on the use of KeepInInsertMode (as well as KeepInEditMode). What these properties really do is tell the framework not to apply DefaultMode - which is what we want.
Posted By Mike On Monday, October 02, 2006
Filed under asp.net formview |
Comments (8)
David Kirkby
-
Tuesday, February 06, 2007
4:43:24 PM
Thanks Mike. Do you recommend any ASP.NET 2.0 books and are you going to do an MCTS in web development?
Mike
-
Tuesday, February 06, 2007
6:47:54 PM
Hi David, I am working on an MCTS right now - very dry reading ;) For learning, the Microsoft Press books have been very good - ASP.NET Core Reference, and Advanced.
J055
-
Monday, July 30, 2007
2:12:58 PM
Holy cow! I've spent the best part of a morning trying to work out why I couldn't change the FormView's mode. I tried e.cancel in the ModeChanging event and that didn't work either. Thanks for this.
PJ
-
Tuesday, September 11, 2007
8:44:29 PM
Right on! Your explaination is short and exactly to the point. Saved me time and hair:)
Steve Bywaters
-
Tuesday, October 23, 2007
3:00:10 AM
THANK YOU!
Exactly what I was looking for.. this return to default mode had me stumped!
Darek
-
Friday, December 14, 2007
3:05:16 PM
Thanks a lot! Who would have guessed.
dEn303
-
Tuesday, January 08, 2008
11:36:48 PM
I have a deadline tomorrow, and this was the last issue i had to solve. thanks a lot!!!
JOhn
-
Tuesday, November 04, 2008
10:59:41 PM
Nice one mate. You'ld think that the default mode would automagically be lower priority than a mode set programatically...