Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 962
joshb Avatar asked Apr 15 '09 19:04

joshb


1 Answers

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.

like image 110
Vikcia Avatar answered Nov 18 '22 18:11

Vikcia