Sunday, October 19, 2008 1:25 PM djsolid

LiteralControl in Markup!!!

I always had the question if I could add a LiteralControl via Markup code. Let's say something like that :

<div class="header">
    <asp:LiteralControl runat="server" ID="ltrHeader"></asp:LiteralControl>
</div>
<div class="sub-header">
    <asp:LiteralControl runat="server" ID="ltrSubHeader" Text="Some Text" ></asp:LiteralControl>
</div>

Well It IS possible and simple to be honest. Just Register the control at the top of the page like that :

<%@ Register Assembly="System.Web" Namespace="System.Web.UI" TagPrefix="asp" %>

You might have a warning that "Element 'LiteralControl' is not a known element." but it will compile just fine and you can set the Text programmatically!

Hope that helps anyone who wants to use a LiteralControl over Literal!

Enjoy!

Filed under: , , ,

Comments

# re: LiteralControl in Markup!!!

Sunday, October 19, 2008 8:01 AM by Bob

Ummm.. For one you don't need to register that assembly in the page itself.. because its registered in the web config by default...

Also <asp:Literal is the same dam thing as a Literal Control....

# re: LiteralControl in Markup!!!

Sunday, October 19, 2008 8:19 AM by djsolid

1. You register that assemlby ONLY and I repeat ONLY if you want to use the LiteralControl. Also at web.config is the same namespace but another assembly registered.

2. No. They are not the same. weblogs.asp.net/.../369294.aspx

# re: LiteralControl in Markup!!!

Sunday, October 19, 2008 8:35 AM by Bob

LOL..

Turn off viewstate on the literal control via the EnableViewState="false" and you have a LiteralControl without all the headaches.

# re: LiteralControl in Markup!!!

Sunday, October 19, 2008 9:57 AM by Stefa Sedich

I am with Bob here, ViewState of and go with a literal seems much easier. Hell if you are soo worried about viewstate why not just put a property on your view then:

<%= this.Property %>

Even simpler.

Leave a Comment

(required) 
(required) 
(optional)
(required)