Use Literal in ASP. NET -- dynamically generate pages, asp. netliteral

Source: Internet
Author: User

Use Literal in ASP. NET -- dynamically generate pages, asp. netliteral

When adding content to a page, if it is static content, you can directly add the tag as HTML to the page without using a container. However, if it isDynamic Content, You must use the container to add the content to the page. Typical containers include Label controls, Literal controls, Panel controls, and PlaceHolder controls.


1. Control Selection: Literal

The difference between the Literal control and the Lable control is that the Literal control does not add any HTML elements to the text, while the Label control presents a span element.

The Panel and PlaceHolder controls are rendered as DIV elements, which create discrete blocks on the page, which are different from the Lable and Literal controls for Embedded rendering.


Because the text and control must be directly displayed on the page rather than any additional tag during the encoding process, the Literal control is selected.


Ii. Usage

Drag the Literal control of the toolbox to the position where you want to add dynamic content, and directly assign the code to the Text attribute of the control during runtime.

The page code is:

<Html xmlns = "http://www.w3.org/1999/xhtml"> 


3. Create a dynamic page based on the database table content
1. business logic:

Dynamic Display of the field with the value of 1 in the "S_Edit_Visible" field in the list on the page


2. Specific Code:

# Region ToTable-join table-July 26, 2015 /// <summary> /// splice table /// </summary> /// <param name = "model"> </param> public void ToTable (IList <ModelBFieldDef> model) {int num, I; bool evenFlag; StringBuilder sbTable = new StringBuilder ("<table>"); // judge the control's parity if (model. count % 2 = 0) {evenFlag = true;} else {evenFlag = false;} if (evenFlag) {num = model. count/2;} else {num = (model. count-1 )/ 2;} // load an even number of for (I = 0; I <num; I ++) {int t = 2 * I; sbTable. append ("<tr>"); sbTable. append (soleCombin (model, t, true); sbTable. append (soleCombin (model, t + 1, true); sbTable. append ("<tr>") ;}if (! EvenFlag) {sbTable. append ("<tr>"); sbTable. append (soleCombin (model, model. count-1, evenFlag); sbTable. append ("<tr>");} sbTable. append ("</table>"); this. hideTable. text = sbTable. toString ();} # endregion # region soleCombin-single composite control-July 26, 2015 /// <summary> /// single composite control /// </summary> /// <param name = "model"> Field Entity List </param> /// <param name = "I"> the row of the List </param> /// <param name = "evenFlag"> whether to load two rows of controls: true: two rows; false: one row </param> /// <returns> </returns> public string soleCombin (IList <ModelBFieldDef> model, int I, bool evenFlag) {StringBuilder sbField = new StringBuilder (""); string Field; string Type = model [I]. SFieldUitype; switch (Type) {case "String": // text box Type sbField. append ("<td class = \" left \ "style = \" width: 110px; height: 27px; \ ">"); sbField. append (model [I]. SFieldNamecn); // text name if (evenFlag) {sbField. append ("</td> <td class = \" right \ "style = \" width: 200px; height: 27px; \ "> <asp: advTextBox ID = \ "");} else {sbField. append ("</td> <td colspan = \" 3 \ "class = \" right \ "style = \" width: 510px; height: 27px; \ "> <asp: AdvTextBox ID = \" ");} sbField. append (model [I]. SFieldName); // TextBox Id sbField. append ("\" runat = \ "server \" CssClass = \ "nTxt \" AttrName = \ ""); sbField. append (model [I]. SFieldNamecn); // AttrName sbField. append ("\" InputType = \ ""); // if the verification type is null, the default value is string type if (model [I]. SFieldType = null) {sbField. append ("string");} else {sbField. append (model [I]. SFieldType);} sbField. append ("\" "); // verify whether it can be empty if (model [I]. SFieldCannull = "1" | model [I]. SFieldCannull = null) {sbField. append ("NotNull = \" true \ "");} sbField. append ("MaxLength = \" "); // enter the maximum length of the string if (model [I]. SFieldLen = null) {sbField. append ("60");} else {sbField. append (model [I]. SFieldLen);} sbField. append ("\" Width = \ "165px \" Height = \ "19px \"> </asp: AdvTextBox> "); // verify whether it can be empty if (model [I]. SFieldCannull = "1" | model [I]. SFieldCannull = null) {sbField. append ("<a style = \" color: Red; text-decoration: none; \ "> * </a>");} sbField. append ("</td>"); break; case "CheckBox": // check box type sbField. append ("<td class = \" left \ "style = \" width: 110px; height: 27px; \ ">"); sbField. append (model [I]. SFieldNamecn); // text name // determine whether it is a single column or multiple columns: evenFlag = true is multiple columns if (evenFlag) {sbField. append ("</td> <td class = \" right \ "style = \" width: 200px; height: 27px; \ "> <asp: checkBox ID = \ "");} else {sbField. append ("</td> <td colspan = \" 3 \ "class = \" right \ "style = \" width: 510px; height: 27px; \ "> <asp: CheckBox ID = \" ");} sbField. append (model [I]. SFieldName); // TextBox Id sbField. append ("\" runat = \ "server \"/> "); break; case" Radio ": // single sequence type (to be supplemented) break; case" Seclect ": // type of the drop-down box (to be supplemented) break; case "Date": // Date type (to be supplemented) break; default: break;} Field = sbField. toString (); return Field ;}# endregion


Summary: select the container to load the dynamic page based on the actual situation and the characteristics of each container.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.