Dynamic control based on ASP.NET

zhaozj2021-02-16  58

Summary:

In the web design, there are many occasions, the page controls should be dynamically created, which can only be dynamically created, which can increase the flexibility of the page, but bring some trouble to the programmer, such as using dynamically created controls, how to use, It is the problem that requires solving, this article is based on ASP.NET, briefly introduces how the page control is dynamically created, and also describes how to use these dynamically created controls.

Preface:

Since Microsoft launched .NET Framework in 2000, the Internet programmers have brought huge gospels, .NET Framework greatly simplifies the complicated web page design, and the page effect that is visible is greatly simplified. The programmer's work tasks, code behind technology separates the page with code, reduces the disorderless state of the web page, where a large number of controls can be used seamlessly, 95% of browsers on the Internet are Microsoft's Internet Explorer, programmers can use these controls without worrying about browser compatibility, this article begins with Microsoft's .NET and COM controls, give specific examples how to create controls on the page, and how to come Use these dynamically created controls. The specific language is VB.NET.

Microsoft control

The dynamic data browsing mode currently used for browsers mainly has the following, (1). Use the Java Applet. (2). Use controls that can be used in the browser. The two methods have their own advantages and disadvantages, using Java Applet, the page is slow, the programming is complex, the reusability is notble, this way is less using; more and more programmers tend to use the control. Most of us use the Web Control and HTML Control and some data operation controls in the Web.

In general, in a web design, it is usually used to place the space to the page, and then set the properties and then program the use in the background program. This way is very convenient, visible, and it is also very convenient to reference these placed controls, but in some cases, we have to dynamically create controls, such as some fields in a database, For users of different identities, the number of fields see is different; and the user of different identities is different for a control of a control, we cannot use a standard when designing the page design, the best The way is dynamically creating controls. For different situations, the number of controls placed on the page is also controlled by the program. Then reference these created controls through the program.

2. Create control

For programmers, use the example to speak is the best way to provide a solution, I will place some tags (label) and text boxes on the page, and the heads are placed in the box from DataSet. Data, and I put these text boxes into the corresponding cell of a table, you can achieve an orderly placement, first add a table to the page. Then create different columns and rows as needed, as follows for dynamically create these text boxes:

DIM I as integer

For i = 0 to introwcount - 1

DIM R as tableRow = new tableerow ()

DIM C1 as Tablecell = New TableCell ()

DIM Mylabel1 as label = new label () 'Set this label ID, for different Label, must be set to different IDs, I set it into a combination of prefix LBL1 plus field names, such as LBL1XLMC, etc.

Mylabel1.id = "LBL1" & TRIM ("Field"). Rows (i) .Item ("Column_name")). Tostring ()

'The following conditional statement is to determine whether the value in the field in the field is empty, and the Label different text is given according to 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 ("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 ("Field"). ROWS (i) .Item ("Column_enable")) = "Yes"

mytextbox.enabled = true

Else

mytextbox.enabled = false

END IF

C2.Controls.add (MyTextBox)

R.cells.Add (C2)

Table3.Rows.Add (r)

NEXT

This successfully added two columns to the table, as for the number of variables INTROUNT, where INTROWCOUNT is the number of records obtained when the database is taken.

3. Use controls created dynamically

When using these controls, you must first know the ID of these controls. For these controls mentioned above, since the ID name is not, it must be used according to conditions. For example, I want to find the ID of the corresponding text box, you can implement the following code:

DIM TXT AS TEXTBOX

TXT = Me.FindControl ("txt" & oledsfield.tables ("field"). Rows (i) .Item_

("Column_name"))))

This will find this control of the text box to use, and then you can use the corresponding function.

4. Skinding When using dynamically created controls, it is difficult to operate these dynamically created controls. This article gives a code, this example, the example is passed under .NET 2002 Windows 2000. At the same time, a method of web design is also given, you can contact me by l_yx123@sina.com.cn, welcome criticism.

转载请注明原文地址:https://www.9cbs.com/read-22108.html

New Post(0)