ASP. NET literal control usage

Source: Internet
Author: User

You can add a literal Web Server Control to the page when you want to set text programmatically without adding additional HTML tags. The literal control is useful when you want to dynamically add text to a page without adding any elements that do not belong to the dynamic text. For example, you can use the literal control to display the HTML read from a file or stream.

Note:
To display static text, you can use HTML to render it. The literal control is not required. The literal control is used only when the text needs to be rendered programmatically.

Add literal controls to the web forms page
Type the <asp: literal> element into the page.

The following example shows a simple page that displays the title news at runtime. The body of the page (including the literal control) is similar to the following code:

<Body>
<Form runat = "server">
<H1> <asp: literal id = "headline" runat = server/> </Form>
</Body>

Alternatively, set the mode attribute to transform, passthrough, or encode. The mode attribute specifies how the control handles the markup you add to the control.

Add the code to the page to set the text attribute of the control at runtime.

The following example shows how to set the text and encoding of the literal control programmatically. This page contains a set of radio buttons that allow users to select between the encoded text and the transmitted text.

Note:
If you are setting the text attribute to text from untrusted sources, set the mode attribute of the control to encode so that the tag does not form an executable tag.

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">
Protected void page_load (Object sender, eventargs E)
{
// Literal1.text = "This <B> text </B> is inserted dynamically .";

Literal1.text = "<script language = 'javascript '> alert ('you have won the lottery! '); <"+"/SCRIPT> ";
If (radioencode. Checked = true)
{

Literal1.mode = literalmode. encode;
}
If (radiopassthrough. Checked = true)
{
Literal1.mode = literalmode. passthrough;
}
}
</SCRIPT>

<HTML>
<Head id = "head1" runat = "server">
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Br/>
<Asp: radiobutton id = "radioencode" runat = "server" groupname = "literalmode" Checked = "true"
TEXT = "encode" autopostback = "true"/>
<Br/>
<Asp: radiobutton id = "radiopassthrough" runat = "server" groupname = "literalmode" text = "passthrough"
Autopostback = "true"/>
<Br/>
<Br/>
<Asp: literal id = "literal1" runat = "server"> </ASP: literal> & nbsp; </div>
</Form>
</Body>
</Html>

Jump out of the form when you select passthrough and output as is when you select encode

Displays static content on the page and allows you to operate it programmatically.

<Asp: literal
Enabletheming = "True | false"
Enableviewstate = "True | false"
Id = "string"
Mode = "transform | passthrough | encode"
Ondatabinding = "databinding event handler"
Ondisposed = "disposed event handler"
Oninit = "init event handler"
Onload = "load event handler"
Onprerender = "prerender event handler"
OnUnload = "Unload event handler"
Runat = "server"
Skinid = "string"
TEXT = "string"
Visible = "True | false"
/>
Use the literal control to display static text on the web forms page. Unlike the label control, literal does not allow you to apply styles to its content. Text is not HTML encoded before it is displayed in the literal control. This allows you to embed scripts in HTML tags in text. If the value of the control is input by the user, verify the value to prevent security vulnerabilities. The following example shows how to use the literal control to display static text. <% @ Page Language = "C #" autoeventwireup = "true" %>

<HTML>
<Head>

<SCRIPT runat = "server">

Void buttonclick (Object sender, eventargs E)
{
Literal1.text = "Welcome to ASP. NET !! ";
}

</SCRIPT>

</Head>
<Body>
<Form runat = "server">
<H3> literal example

<Asp: literal id = "literal1"
TEXT = "Hello world !! "
Runat = "server"/>

<Br>

<Asp: button id = "button1"
TEXT = "Change literal text"
Onclick = "buttonclick"
Runat = "server"/>

</Form>
</Body>
</Html>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.