views:

1816

answers:

6

I am setting the CurrentMode of a FormView to insert mode using the ChangeMode method in the Page_Load event like so:

if(!Page.IsPostBack)
{
    MyFormView.ChangeMode(FormViewMode.Insert);
}

Within my FormView's insert template I have a DropDownList control with it's AutoPostBack property set to true. I also have several other DropDownList and TextBox controls within the insert template.

Whenever I change the selection of the DropDownList and a postback occurs I'm losing all the values entered into the controls. The weird thing is that if I use ChangeMode to set the FormView to insert mode anytime after the initial page load I don't have the problem. I've stepped through the the code with the debugger and everything seems to be happening correctly but sometime after my event handler for the DropDownList runs everything seems to be getting reset.

What is going on here?

Update: I noticed that my FormView was inside of a div tag with runat="server" and enableviewstate="false". Once I enabled viewstate for the container div I began seeing a slightly different behavior. The FormView still does not retain values after the first postback but now subsequent postbacks work fine and the values are retained.

Any ideas would be greatly appreciated.

A: 

Try

MyFormView.EnableViewState = true;
sshow
ViewState is enabled for the FormView control.
joshb
A: 

try to change mode on Page Init or or Prerender events

Tried both of your suggestions and no luck.
joshb
A: 

The ViewState loads before PageLoad and my guess is that by changing the Mode on PageLoad recreates another set of controls, the ones in your EditItemTemplate. If it is set by default to Edit Mode, and that you uncomment that line on PageLoad, will it maintain the values? It should if you don't switch the mode.

Maxime
A: 

FormView.ChangeMode(FormViewMode.Insert)

This must work, check Id's and use intelisense?

my example is with a add item form view..

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    AddNewItemFormView.ChangeMode(FormViewMode.Insert)
End Sub

Try to do it on page load without any "if..then's", if it works you'll know that isn't the expression but maybe the if content or construction. Remember to make the control IDs meaningful so it will be easier and less likely to do mistakes when you need to use them or call them through intelisense.

Best luck.

:)

A: 

This answer from other forum by Walter Wang [MSFT] - 26 Jan 2007 03:29 GMT

First, the problem appears to be that the FormView is told by the data source control that the data has changed, and therefore it should rebind to get the new data. This actually should not have been done since we're still in Insert mode. I have got a workaround for your reference: Inherit from FormView to create your own FormView control, override OnDataSourceViewChanged and set RequiresDataBinding to false if we're in Insert mode:

public class MyFormView : FormView { protected override void OnDataSourceViewChanged(object sender, EventArgs e) { if (this.CurrentMode == FormViewMode.Insert) { this.RequiresDataBinding = false; } else { base.OnDataSourceViewChanged(sender, e); } } }

I've tested it on my side and it seems working correctly. Please give it a try and let me know the result.

Vikcia
A: 

I am having the similar issue of viewstate not retained at EditItem template. Setting EnableViewState = true doesnt help either.

What i do is get the OnUpdated() function, and at the very end of the function, e.KeepInEditMode = true;

Hope this helps..

Elwist Ng