Asp.net dynamic addition control

Source: Internet
Author: User

In the examples, you need to use the animation creation control. Previously, in Winform, it was easy to realize that the Web took half a day to learn about it, now, remember: Based on asp. for net2.0, remember how the preceding controls can be used to automatically create a step, and how to use these methods.

In order to make the controls generated by the notebook orderly, drag a Table on the notebook surface. Then, place the control in the Cell of the Table. I am in this sort of suborder. I wonder if you have a better solution. Please try again later.

Add controls to the notebook and add the cursor in the Page_Init event.

OK, let's talk about it. The actual steps are as follows:

Create a new method named CreateControlDynamically, with the following generation:

CreateDynamic
Sub CreateControlDynamically (ByVal nCount As Integer)
For I As Integer = 1 To nCount
Dim tr As TableRow = New TableRow ()
Dim tc As TableCell = New TableCell ()
Dim myLabel As Label = New Label ()

'This parameter sets the ID of the label. For different labels, you must set different IDs.
MyLabel. ID = "lbl" & I
MyLabel. Text = "Critial" & I
Tc. Controls. Add (myLabel)
Tr. Cells. Add (tc)

Dim tc2 As TableCell = New TableCell ()
Dim mytextbox As TextBox = New TextBox ()
Mytextbox. ID = "txt" & I
Mytextbox. Text = "TextBox" & I
Tc2.Controls. Add (mytextbox)
Tr. Cells. Add (tc2)

Me. tbl. Rows. Add (tr)
Next

When calling, in Page_Init, directly press CreateControDynamically (5. Then, the source signature of aspx will also be output:

Aspx
<% @ Page Language = "VB" AutoEventWireup = "false" CodeFile = "Default. aspx. vb" Inherits = "_ Default" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Create Control Dynamically </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: Table ID = "tbl" runat = "server">
<Asp: TableRow ID = "trLbl" runat = "server">
</Asp: TableRow>
<Asp: TableRow ID = "trTbx" runat = "server">
</Asp: TableRow>
</Asp: Table>
<Br/>
<Asp: Button ID = "btnOK" runat = "server" Text = "Button"/> </div>
</Form>
</Body>
</Html>

OK, run F5 and see what it is. The 10 controls in the 5 rows and 2 columns are displayed.

So how can I use your dynamic volume creation control? To use these controls, you must first know the ID of these controls. For these controls mentioned above, when I use them, the backend operator adds the Class 1, 2, 3, 4, and 5 to the backend operator. This sub-name is also recommended when you are using it for later programming. The following code is used in the next generation:

CType (Me. FindControl ("tbx1"), TextBox). Text = "custom Text value"

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.