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 (10)

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

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

Leave a comment



Your name:
 

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

Your website (optional):



About Michael

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

Proud father of two amazing girls, Rachel and Hannah, and loving husband to Cicelie who inflates and pops his ego as necessary.

 Subscribe to ViNull.com |  Comments

Follow me on Twitter | Contact Me

Related Posts

ASP.NET: Cannot use a leading .. to exit above the top directory

This is a really quick post, mostly for myself so the next time I have this issue I can find the answer (yes, I often search my blog before google). The ... Read more

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

XNA 3D Primer by Michael C. Neel

XNA 3D Primer by Michael C. Neel
Buy Now: [ Amazon ] [ Wrox ]

GameMarx

CodeStock

ASPInsiders Member

ETNUG Member