I have noticed some strange behavior when PlaceHolder is used to dynamicly load controls. I suspect to some kind of BUG or behaivor at design. ViewState is lost when control Text property is assigned before control is added to PlaceHolder.
Let me explain: When line of code:
PlaceHolder1.Controls.Add(lb) is before assigning Text property:lb.Text = "SOME TEXT",
system works well and ViewState is normaly read after postback. But then
PlaceHolder1.Controls.Add(lb) is after lb.Text = "SOME TEXT", ViewState fails to run correctly and lb.Text is lost after PostBack.
I use Asp.Net 3.5 (Visual Studio 2008 SP1).
Here are sample of not working code, but if you move PlaceHolder1.Controls.Add(lb) just before lb.Text assgning it will works fine.
Best wishes
Milan
Default.aspx.cs and Default.aspx are
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
Label lb = new Label();
if (!IsPostBack)
lb.Text = "SOME TEXT";
PlaceHolder1.Controls.Add(lb);
}
}
}
ViewState fails to run correctly and lb.Text is lost after PostBack
Just remember that dynamically created controls will always be lost after postback regardless if using a PlaceHolder or not. You will need to recreate them and add them back to the Placeholder on every postback. I suggest in the Page_OnInit() event or somewhere
similar. Have a look to the following for additional information:
As far as the order of instantion, assignment of properties, and being added to the Placeholder, the MSDN does it in exactly the aforementioned order. Take a look to the following for a code example:
I think the behavior you are seeing is by design and might be where you are doing the assignment. You should really have your code in the OnInit() event. The following post highlights a similar issue that you are having:
"Figure 4 illustrates the sequence of events that transpire, highlighting why the change to the Label's Text property needs to be stored in the view state."
Many thanks for your detailed answer. I under stand importance of OnInit event and same controls creation order for perserving ViewState. What I'm still wondering is how order of assigning Text propery and Parent property in Init event can have influence on
ViewState. Many thanks for your time, Milan.
milanbetter
Member
4 Points
5 Posts
PlaceHolder and ViewState Bug?
Nov 20, 2011 04:52 PM|LINK
Hello,
I have noticed some strange behavior when PlaceHolder is used to dynamicly load controls. I suspect to some kind of BUG or behaivor at design. ViewState is lost when control Text property is assigned before control is added to PlaceHolder.
Let me explain: When line of code:
PlaceHolder1.Controls.Add(lb) is before assigning Text property:lb.Text = "SOME TEXT",
system works well and ViewState is normaly read after postback. But then
PlaceHolder1.Controls.Add(lb) is after lb.Text = "SOME TEXT", ViewState fails to run correctly and lb.Text is lost after PostBack.
I use Asp.Net 3.5 (Visual Studio 2008 SP1).
Here are sample of not working code, but if you move PlaceHolder1.Controls.Add(lb) just before lb.Text assgning it will works fine.
Best wishes
Milan
Default.aspx.cs and Default.aspx are
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page { protected void Page_Init(object sender, EventArgs e) { Label lb = new Label(); if (!IsPostBack) lb.Text = "SOME TEXT"; PlaceHolder1.Controls.Add(lb); } } }<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder> </div> <asp:Button ID="btSubmit" runat="server" Text="Submit" /> </form> </body> </html>
atconway
All-Star
16846 Points
2756 Posts
Re: PlaceHolder and ViewState Bug?
Nov 21, 2011 07:48 PM|LINK
Just remember that dynamically created controls will always be lost after postback regardless if using a PlaceHolder or not. You will need to recreate them and add them back to the Placeholder on every postback. I suggest in the Page_OnInit() event or somewhere similar. Have a look to the following for additional information:
http://forums.asp.net/t/1712535.aspx/1
As far as the order of instantion, assignment of properties, and being added to the Placeholder, the MSDN does it in exactly the aforementioned order. Take a look to the following for a code example:
How to: Add PlaceHolder Web Server Controls to a Web Forms Page:
http://msdn.microsoft.com/en-us/library/wwbbhe6w(v=VS.85).aspx
I think the behavior you are seeing is by design and might be where you are doing the assignment. You should really have your code in the OnInit() event. The following post highlights a similar issue that you are having:
Dynamically Loaded Control can not maintain values at PostBack?
http://geekswithblogs.net/chrishan/archive/2007/03/27/109988.aspx
"Figure 4 illustrates the sequence of events that transpire, highlighting why the change to the Label's Text property needs to be stored in the view state."
Understanding ASP.NET View State (above excerpt):
http://msdn.microsoft.com/en-us/library/ms972976.aspx
If you really think there is a reproducible bug and its not by design, you can report it to Microsoft Connect below:
http://connect.microsoft.com/
Hope this helps!
milanbetter
Member
4 Points
5 Posts
Re: PlaceHolder and ViewState Bug?
Nov 21, 2011 11:51 PM|LINK