What am I doing wrong when databinding a FormView to a List?

muzpikw

New Member
Here's the scenario:I have a class with properties:\[code\]public class MyData{ public string One { get; set; } public string Two { get; set; }}\[/code\]I have a FormView:\[code\]<asp:FormView ...> <EditItemTemplate> <asp:DynamicEntity ...> <asp:Button ...> </EditItemTemplate><asp:EditItemTemplate>\[/code\]I want to bind a \[code\]List<MyData>\[/code\] to the FormView. Here's what I'm trying:\[code\] private List<MyData> m_list; protected void Page_Init(object sender, EventArgs e) { m_list = Session["list"] as List<MyData>; if (m_list == null) m_list = new List<MyData> { new MyData { One = "A" }, new MyData { One = "B" }, new MyData { One = "C" } }; MyFormView.EnableDynamicData(typeof(MyData)); MyFormView.DataSource = m_list; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { MyFormView.DataBind(); } } protected void MyFormView_OnItemUpdating(object sender, FormViewUpdateEventArgs e) { var data = http://stackoverflow.com/questions/15593758/m_list[((FormView)sender).PageIndex]; data.One = e.NewValues["One"] as string; data.Two = e.NewValues["Two"] as string; } protected void MyFormView_OnPageIndexChanging(object sender, FormViewPageEventArgs e) { var formview = sender as FormView; if (formview != null) { formview.PageIndex = e.NewPageIndex; formview.DataBind(); } } protected void Page_PreRender(object sender, EventArgs e) { MyFormView.DataBind(); } protected void Page_PreRenderComplete(object sender, EventArgs e) { Session["list"] = m_list; }\[/code\]I see the form with 3 pages. Each page has the "One" field populated with either A, B, or C. When I enter a value for one of the fields and hit submit the \[code\]e.NewValues\[/code\] in \[code\]MyFormView_OnItemUpdating\[/code\] shows null for each value and updates the item in the list with null values.I feel like I'm missing a basic concept here. Any ideas?
 
Back
Top