ASP.NET: Understanding FormView's DefaultMode with ChangeMode

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)

Submit this story to DotNetKicks   

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

Leave a comment



Your name:
 

Your email (not shown):
 
Will display your Gravatar image.

Your website (optional):



About Me

Michael C. Neel, born 1976 in Houston, TX and now live in Knoxvile, TN. Software developer, currently .Net focused. Board member and President of ETNUG, and organizes CodeStock, East Tennessee's annual developers conference. .Net speaker, a Microsoft ASP.NET MVP and ASPInsider. Founder of FuncWorks, LLC and Feel The Func podcast.

Proud father of two amazing girls, Rachel and Hannah.

 Subscribe to ViNull.com |  Comments

Follow me on Twitter | Contact Me

Related Posts

ASP.NET: Creating a UserControl with Child Content

I love ASP.NET User Controls, aka “ascx” files.  These little guys are great for reusable content and dividing up the components of a website.  ... Read more

ASP.NET SEO Interview on Polymorphic Podcast

Craig Shoemaker just posted the latest episode of the Polymorphic Podcast: ASP.NET SEO - Interview with Michael Neel.  Yes, I've now appeared in a ... Read more

Google Can You Hear Me?

In June of 2006 I wrote an article titled "Google Can You Hear Me? How to design URLs that are search engine friendly" that also included a sidebar titled ... Read more

Using LINQ to generate HTML

I hate seeing code mixed with markup. Seeing a template page with <% if(show) { %> makes me want to claw my eyes out.  Seeing String htmlTitle ... Read more

The ASP.NET MVC Definition

A few days ago I posted a question to the community, looking for a definition of the ASP.NET MVC framework that didn't depend upon faults in ASP.NET WebForms ... Read more

FeelTheFunc Podcast

CodeStock
Are you going?

ASPInsiders Member

ETNUG Member