Difference between UI.LiteralControl and WebControls.Literal ?

A

Andreas Klemt

Hello,
what is the difference between UI.LiteralControl and WebControl.Literal?

Which should I use in my VB-Code and what is better?

Thanks,
Andreas
 
A

Alessandro Zifiglio

hi Andreas,
Use the System.Web.UI.WebControls.Literal control to reserve a location on
the Web page to display text. The Literal control is similar to the Label
control, except the Literal control does not allow you to apply a style to
the displayed text. You can programmatically control the text displayed in
the control by setting the Text property.


<asp:Literal id="Literal1"
Text="Text"
runat="server"/>


use the System.Web.UI.LiteralControl when developing custom controls.
Literal controls behave as text holders, meaning that you can extract text
from the literal control and remove the literal control from the parent
server control's ControlCollection through the parent's Controls property.
You can add or remove literal controls from a page or server control
programmatically using the ControlCollection.Add or ControlCollection.Remove
method, respectively.

Using the System.Web.UI.LiteralControl:

Me.Controls.Add(New LiteralControl("<h3>Value: "))

Dim Box As New TextBox
Box.Text = "0"
Me.Controls.Add(box)

Me.Controls.Add(New LiteralControl("</h3>"))

Now the differences i see btw the two are :
The System.Web.UI.LiteralControl cannot be added to the page at design time,
you cannot throw it onto your page as you can with the webcontrols. You
cannot set its properties via the propertyGrid.
What you can do is use this in code only. so you create a new literal
control and dynamically add it to a page. The best use for this I can think
of is exactly how it is being described in the docs, in that use it for
custom control development as a visual representation is not required at
this point. Moreover using literalcontrols in custom control has its
benefits --when adding static html giving you better control like when
wanting to add/remove static html using the controlCollection.add and remove
method is in its own a major benefit, in my opinion ;P


In short, if you were not dynamically adding literal controls to your page
but instead were dragging and dropping or adding it in htmlview, with more
control on where text is displayed on the page then use
System.Web.UI.WebControls.Literal --on the other hand use the
System.Web.UI.LiteralControl if you wanted to add this control onto your
page at runtime, then you need to add it to a parent control. To control
placement of literal content on your page, you might need to use a
placeholder control along with this or just add it to an existing control or
the parent control, in the page.


So to cut it down even shorter : System.Web.UI.WebControls.Literal can also
be added dynamically but if you were adding it dynamically then you dont
need a user interface for the control at design time so your choice would be
System.Web.UI.LiteralControl.

The only difference between the two controls is that
System.Web.UI.WebControls.Literal can be represented visually at design time
whereas the System.Web.UI.LiteralControl has no visual reprentation at
design time.
 
A

Andreas Klemt

Hello Alessandro,

many thanks for your answer!! It helped me alot.

Kind Regards,
Andreas
 
A

Alessandro Zifiglio

your welcome Andreas, glad i could help ;)

Andreas Klemt said:
Hello Alessandro,

many thanks for your answer!! It helped me alot.

Kind Regards,
Andreas

control would
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top