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 (10)
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...
smp
-
Wednesday, March 16, 2011
5:59:37 PM
I ran into an issue because of the bug and it will be of great help if somebody could help me to fix this.
I have 3 user controls inside update panel. First 2 controls have only formview. The 3 uc has a grid view and form view.
-in the page-load I put an alert fv.currentmode.tostring
uc1- it is in Edit
uc2 -it is in ReadOnly
uc3- it is in Edit mode
When I click the edit button in gridview of uc3, uc1 formview gets fv_databound is called. But uc2's formview databound is not called!!!
So whatever the user entered in uc1 is lost. But not in 2.
How do I fix this?
smp
-
Thursday, March 17, 2011
12:33:39 PM
The formview databound in other user controls got changed because profile got changed. And uc1 was calling this value where as uc2 was not calling this value