Controls not retaining values after postback when FormView set to insert mode
Asked Answered
R

6

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.

Rose answered 15/4, 2009 at 19:28 Comment(0)
A
2

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.

Agonized answered 4/3, 2010 at 14:9 Comment(1)
Include the link to the source if you can.Centrosome
K
0

Try

MyFormView.EnableViewState = true;
Kalahari answered 15/4, 2009 at 19:44 Comment(1)
ViewState is enabled for the FormView control.Rose
M
0

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.

Metre answered 17/4, 2009 at 15:35 Comment(0)
U
0

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.

:)

Undersell answered 2/7, 2009 at 20:23 Comment(0)
H
0

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

Hyaena answered 5/8, 2010 at 3:9 Comment(2)
If you have a NEW question, please ask it by clicking the Ask Question button. If you have sufficient reputation, you may upvote the question. Alternatively, "star" it as a favorite and you will be notified of any new answers.Mangonel
KeepInEditMode only makes the formview to remain in edit mode after a button with an Update command is clicked It has nothing to do with retaining non-committed-yet and newly input data msdn.microsoft.com/en-us/library/… "By default, the FormView control returns to the mode specified by the DefaultMode property after an update operation. Use the KeepInEditMode property to specify whether the FormView control should remain in edit mode. "Nicknickel
E
-1

try to change mode on Page Init or or Prerender events

Eulogize answered 15/4, 2009 at 20:22 Comment(1)
Tried both of your suggestions and no luck.Rose

© 2022 - 2024 — McMap. All rights reserved.