Denis Bauer

ASP.NET, .NET Framework and Longhorn stuff

Differences between System.Web.UI.LiteralControl and System.Web.UI.WebControls.Literal

Mike Ogden pointed out the fact that the DynamicControlsPlaceholder (DCP) does not correctly recreate System.Web.UI.LiteralControl. At first I was very perplexed because I was somehow sure that I successfully tried that before. However, I must have mixed it up with the System.Web.UI.WebControls.Literal which works without problems.

That raises the question why there are two different controls and how they differ. The major difference is that the LiteralControl does not persist the Text property in ViewState, which is why it is recreated emptily by the DCP. The Literal, however, takes a LiteralControl as a child and saves its Text in ViewState:

protected override void AddParsedSubObject(object obj)
{
if (!(obj is LiteralControl))
{
throw new HttpException(HttpRuntime.FormatResourceString(
"Cannot_Have_Children_Of_Type", "Literal", obj.GetType().Name.ToString()));
}
this.Text = ((LiteralControl) obj).Text;
}

So, if you want to use literal controls with the DynamicControlsPlaceholder make sure that you use Literal instead of LiteralControl.

Posted: Feb 08 2005, 07:41 PM by DBauer | with 4 comment(s) |
Filed under:

Comments

Iván said:

Hi Denis,

You are right. LiteralControl doesn't have server-side processing and it is recreated in each request.

Hence, doesn't  persist on the page via ViewState.

See below:

msdn2.microsoft.com/.../system.web.ui.literalcontrol.aspx

# December 19, 2007 5:46 AM

Rajasekhar said:

You are right. LiteralControl doesn't have server-side processing and it is recreated in each request.

Hence, doesn't  persist on the page via ViewState.

msdn2.microsoft.com/.../system.web.ui.literalcontrol.aspx

# April 17, 2008 4:36 AM

abc said:

Mr. Rajashekhar just copied Mr. Ivan's comments.

# August 18, 2008 6:10 AM

Liz said:

We are developing a web application with VS2003.  Suddenly all of my (server) web controls have become literal controls.  this has broken my code.

If anyone has any idea as to why this would happen, please e-mail me at sing4you@hotmail.com.

Thanks.

# August 19, 2008 12:26 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)