How to dynamically generate Asp.net front-end display

Source: Internet
Author: User

Someone told me on MSN today that I don't want to use controls when using Asp.net for a website. I am very surprised. Continue to explore and find that what he was going to solve was the front-end page.

The number of controls is determined at run time, and he always thinks that those controls can only be dragged to the page. I thought about it and thought it was necessary to move it on the front-end page.

Write down the method of the State generation control, and you can leave. This is to give a rough explanation to the unknown brothers.

In the front-end code, we may see a layout level formed by tags such as Div and span. In fact, in the background,. Net also provides us with a control layer.

Times. This is more abstract. Here is a small example:

<Div id = "div1" runat = "server">

<Div id = "subdiv1">

......

</Div>

<Div id = "subdiv2">

.....

</Div>

<Div>

In the code above, we can see a layout hierarchy formed by the three Divs. div1 contains two sub-Divs, which can only be fixed. Actual

On the backend,. NET will create a control container for div1. In this container, add two sub-Divs. We can find them in the container in the backend and perform

Processing. This control container also has the concept of layering. The div1 control container contains subdiv1, subdiv2, and the two sub-divs also have their own control containers. For all block-level elements,

They all belong to their own control containers (for the concept of block-level elements and intra-row elements, you can find them on the Internet, which belongs to the concept in HTML ).

You can use this. controls.

Now let's take an example to illustrate how to dynamically read data from the database to generate a navigation bar. This navigation bar is implemented using Unordered Lists (how to change

Reference HTML content in the navigation bar ). First, let's look at the front-end code:

<Div>

<Ul id = "Ul" runat = "server">

</Ul>

 

</Div>

As you can see, the front-end code is very simple. It only adds a label for the unordered list <ul>, but it must be declared that the list is running on the server; otherwise,. NET will not be maintained.

We can't control the level of control in the background. Let's take a look at how the background code dynamically adds list elements:

Datatable T = new datatable ();

Da. Fill (t );

Foreach (datarow row in T. Rows)

{

Label Label = new label ();

Label. Text = row ["room_type"]. tostring ();

Ul. Controls. Add (New literalcontrol ("<li> "));

Ul. Controls. Add (Label );

Ul. Controls. Add (New literalcontrol ("</LI> "));

}

The background code is also very simple. First, extract the table from the database that needs to be used as the content of the navigation bar. For each element in the table, we create a static

Text Box, write the content to be displayed in the navigation bar to the static text box, and then call the unordered list control container add control element method, you can add list items

To the unordered list. You can use the preceding method to dynamically add controls to other block-level elements.

If you have any questions, please email me or MSN; zhaotiantang520@live.cn

 

 

 

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.