asp.net-based web Dynamic control creation

Source: Internet
Author: User
asp.net|web| Create | dynamic | controls


Absrtact: In the web design, there are many occasions, the page's control to dynamically create or even dynamically create, which can increase the flexibility of the page, but to the programmer brought some trouble, such as the use of dynamically created control, how to use, are required to solve the problem, Based on ASP.net, this article briefly describes how the controls on the page are created dynamically, as well as how to use these dynamically created controls.

Objective:

Since Microsoft launched the. NET Framework in 2000, it has brought great benefits to programmers based on Internet programming, and the. NET Framework greatly simplifies the complexity of Web page design, Visible can get the page effect greatly simplifies the task of the page design programmer, code behind technology to separate the page from the code, reducing the disorderly state of the Web page, which a large number of reusable controls can be used seamlessly by the browser, due to the Internet 95% Browsers are Microsoft Internet Explorer, and programmers can safely use these controls without worrying about browser compatibility. Starting with Microsoft's. NET and COM controls, this article gives specific examples of how to dynamically create controls on a page and how to use these dynamically created controls. The specific language used is vb.net.

1. Microsoft controls

There are two kinds of dynamic data browsing methods used in browsers, (1). Use Java applets. (2). Use a control that can be used in a browser. Both methods have advantages and disadvantages, using Java applets, the page execution is slow, the programming is complex, the reusability is not very small, this way is now used less; more and more programmers prefer to use controls. Most of the time we are using. NET Web control and HTML controls, as well as some of the data manipulation actions on the web.

In general, in web design, you usually use the drag-and-drop way to place the space on the page, and then set the properties on it, and then programmatically use it in the background program. This is a convenient way to operate, can be seen, while referencing these placed controls is also convenient, but in some cases, we have to dynamically create controls, such as the one by one database in some fields, for different identities of users, see the number of fields is not the same And there is a different identity for the user to a control of the state is not the same, we can no longer use a standard page design, the best way is to dynamically create the control, for different situations, through the program to control, the number of controls placed on the page also through the program to control. The created controls are then referenced by a program.
2. Creating controls

For programmers, using examples to speak is the best way to provide solutions, and I will dynamically place labels and text boxes (textbox) on the page, where the data from the dataset is placed. At the same time I put these text boxes into the corresponding cells in a table, you can achieve an orderly placement, first in the page to add a table. Then create a different number of columns and rows as needed, following the code for creating these text boxes dynamically:

Dim I as Integer
For i = 0 to IntRowCount-1
Dim r as TableRow = New TableRow ()
Dim C1 as TableCell = New TableCell ()
Dim myLabel1 as Label = New label ()

' Here set the ID of the label, for different label, must be set to a different ID, where I set the prefix LBL1 plus the combination of field names, such as LBL1XLMC.

Mylabel1.id = "Lbl1" & Trim (Oledsfield.tables ("Field"). Rows (i). Item ("column_name")). ToString ()
 
' The following conditional statement is to determine whether the value of the field in the database is NULL, giving the label a different text depending on the condition.

If oledsfield.tables ("Field"). Rows (i). Item ("Column_title"). gettype.tostring = "System.DBNull" Then
Mylabel1.text = ""
Else
Mylabel1.text = Trim (Oledsfield.tables ("Field"). Rows (i). Item ("Column_title"))
End If

C1. Controls.Add (MYLABEL1)
R.cells.add (C1)
Dim C2 as TableCell = New TableCell ()
Dim MyTextBox as TextBox = New textbox ()
Mytextbox.id = "Txt" & Trim (Oledsfield.tables ("Field"). Rows (i). Item ("column_name")). ToString ()
If oledsresult.tables ("result"). Rows (0). Item (i). gettype.tostring = "System.DBNull" Then
MyTextBox. Text = ""
Else
MyTextBox. Text = Trim (oledsresult.tables ("result"). Rows (0). Item (i))
End If

If Trim (Oledsfield.tables ("Field"). Rows (i). Item ("column_enable") = "Yes" Then
MyTextBox. Enabled = True
Else
MyTextBox. Enabled = False
End If

C2. Controls.Add (MyTextBox)
R.cells.add (C2)
TABLE3.ROWS.ADD (R)
Next

This succeeds in adding two columns to the table, as the number of rows is determined by the variable introwcount, where Introwcount the number of records that are obtained when the database is fetched

3. Using dynamically created controls

When using these controls, you must first know the IDs of those controls, and for the controls mentioned above, they must be used in accordance with the condition because the ID name is indeterminate. For example, I would like to find the corresponding text box ID, the following code can be implemented:

Dim txt as TextBox
txt = Me.findcontrol ("TXT" & Oledsfield.tables ("Field"). Rows (i). Item ("column_name"))

This will find the control of the text box you want to use, and then you can use the appropriate functionality.

4. Summary

The difficulty with dynamically creating controls is how to manipulate these dynamically created controls. This article gives the code, the example of this article in the. NET 2002+windows 2000 debugging pass.




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.