动态生成控件

原创 2006年05月31日 15:00:00

Adding Controls to a Page

You can add new controls to a page by adding the controls to a page's Controls collection. You can add a control at the end of the Controls collection by using the Add() method. Alternatively, you can add a control at a specific index location in the Controls collection by using the AddAt() method.

The page in Listing 4.9, for example, constructs all its own controls. It contains a single Page_Load subroutine, which adds 30 LiteralControl controls to the page. If you open this page in your Web browser, you see the word Hello! repeated 30 times (see Figure 4.5).

Listing 4.9 ControlsAdd.aspx
<Script Runat="Server">
Sub Page_Load
  Dim intCounter As Integer
  For intCounter = 1 To 30
    Controls.Add( New LiteralControl( "Hello!" ) )
  Next
End Sub
</Script>
Figure 4.5. Self-building page.

You can remove controls from the Controls collection by using the Remove(), RemoveAt(), or Clear() method. The Remove() method removes the control that you pass to it, the RemoveAt() method removes a control at a specific index location, and the Clear() method removes all the controls from the Controls collection.

The page in Listing 4.10, for example, adds three controls and then uses the Remove() method to remove the first control.

Listing 4.10 ControlsRemove.aspx
<Script Runat="Server">
Sub Page_Load
  Dim intCounter As Integer

  For intCounter = 1 To 3
    Controls.Add( new LiteralControl( "Control" & intCounter  ) )
  Next
  Controls.Remove( Controls.Item( 0 ) )
End Sub
</Script>

The PlaceHolder Control

Typically, you don't add controls directly to a page's Controls collection. In most cases, you will want to add a control to a particular location on a page.

You can add a control to the Controls collection of any other control. For example, there is nothing wrong with adding a new control to the Controls collection of either an HTMLForm or Panel control.

However, there is a special control included in the ASP.NET Framework, called the PlaceHolder control that has the singular purpose of acting as a container for other controls.

Listing 4.11 illustrates how you can use the PlaceHolder control to dynamically add TextBox controls to a form.

Listing 4.11 PlaceHolder.aspx
<Script Runat="Server">

Sub Page_Load
  Dim intCounter As Integer
  Dim btnNewButton As Button

  For intCounter = 1 to 10
    plhPlaceHolder.Controls.Add( _
      New LiteralControl( "<p>Field " & intCounter & ": " ) )
    plhPlaceHolder.Controls.Add( _
      New TextBox )
  Next

  btnNewButton = New Button
  btnNewButton.Text = "Click Here!"
  plhPlaceHolder.Controls.Add( btnNewButton )
End Sub

</Script>

<html>
<head><title>PlaceHolder.aspx</title></html>
<body>

<form Runat="Server">

<asp:PlaceHolder
  id="plhPlaceHolder"
  Runat="Server" />

</form>

</body>
</html>

In the Page_Load subroutine in Listing 4.11, ten TextBox controls and one Button control are added to the Controls collection of a PlaceHolder control. When the page is displayed in a browser, the new TextBox controls are rendered.

Dynamically Generating Forms

Imagine that you need to create a form that has a variable number of input fields. For example, you can imagine a simple order entry form that enables you to enter a variable number of product orders. The same form enables you to submit a single order or 25 orders at a time.

You can use the methods discussed in the previous section to dynamically add form fields to a form. Listing 4.12 contains a simple order entry form. When the page first loads, it contains a single form field for the customer name and a single form field for the product.

The page also contains a button labeled Add Product Field. When you click this button, a new product form field is added to the form. You can click this button as many times as you wish to add any number of new product form fields (see Figure 4.6).

Figure 4.6. Dynamically generated form.

When you click the Add Product Field button, the AddProductFieldCount subroutine is executed. This subroutine increments the count of product fields (stored in the state bag) by one. It also adds a new text box to the page by calling the AddProductField subroutine.

Notice that the Page_Load subroutine rebuilds all the product fields every time the page is loaded. When you dynamically add a new control to a page, the control is not automatically retained between form posts. The control itself is not preserved in the page's view state. However, any text you enter into a dynamically generated form field is automatically preserved in view state.

Listing 4.12 DynamicForm.aspx
<Script Runat="Server">

Sub Page_Load
  Dim intCounter As Integer

  If Not IsPostBack Then
    ViewState( "ProductFieldCount" ) = 1
  ElseIf ViewState( "ProductFieldCount" ) > 1 Then
    For intCounter = 2 To ViewState( "ProductFieldCount" )
      AddProductField( intCounter )
    Next
  End If
End Sub

Sub AddProductFieldCount( s As Object, e As EventArgs )
  ViewState( "ProductFieldCount" ) += 1
  AddProductField( ViewState( "ProductFieldCount" ) )
End Sub

Sub AddProductField( strFieldNum As String )
  Dim litLabel As LiteralControl
  Dim txtTextBox As TextBox

  ' Add Literal Control
  litLabel = New LiteralControl
  litLabel.Text = "<p><b>Product " & strFieldNum & ":</b> "
  plhProductFields.Controls.Add( litLabel )

  ' Add TextBox Control
  txtTextBox = New TextBox
  txtTextBox.ID = "txtProduct" & strFieldNum
  plhProductFields.Controls.Add( txtTextBox )
End Sub

Sub btnSubmit_Click( s As Object, e As EventArgs )
  Response.Redirect( "ThankYou.aspx" )
End Sub

</Script>

<html>
<head><title>DynamicForm.aspx</title></html>
<body>

<form Runat="Server">

<b>Customer Name:</b>
<asp:TextBox
  ID="txtCustomer"
  Runat="Server" />

<p>
<div style="background-color: yellow;padding:10px">

<b>Product 1:</b>
<asp:TextBox
  ID="txtProduct1"
  Runat="Server" />

<asp:PlaceHolder
  id="plhProductFields"
  Runat="Server" />

</div>

<p>
<asp:Button
  Text="Add Product Field"
  OnClick="AddProductFieldCount"
  Runat="Server" />

<asp:Button
  id="btnSubmit"
  Text="Submit Complete Order"
  OnClick="btnSubmit_Click"
  Runat="Server" />

</form>
</body>
</html>

JavaScript动态生成控件的方法

JavaScript动态生成控件的方法 这个是在项目里面遇到的问题,这个是一个Test,不过还是没有找到从页面映射数据到后台的方法,boss给出的方法是用json来传递数据,不过我还是不是很懂。下面...
  • qinqigang
  • qinqigang
  • 2016年10月21日 16:27
  • 288

C#如何获得动态生成的控件

在c#中很多时候都会动态生成一些控件,但又想对这些控件进行一些其他的操作。该如何获取这些控件了,其实方法很简单。 以textBox为例: foreach(System.Windows.Form...
  • zhushiq1234
  • zhushiq1234
  • 2016年07月24日 11:09
  • 1865

android studio for android learning (二十七) UI控件动态加载机制浅析

1.为了满足多样化的定制需求,传统的UI加载方式已经满足不了我们的需求,为了创建出更友好,更快速的界面,我们有时需要根据用户的需求来生成控件,android动态加载机制简析,下面介绍常见的三种方式。2...
  • yywan1314520
  • yywan1314520
  • 2016年10月05日 20:42
  • 864

asp.net 动态创建控件和移除控件

废话不多说 直接看一个小demo  前台代码 ...
  • u010586842
  • u010586842
  • 2013年11月17日 21:52
  • 2552

C#WinFrom开发系列之关于动态添加生成和删除控件的相关知识

前段时间由于事情比较多,很长时间没有进行学习和知识的总结,几天有空刚好补上! 今天要讲的是怎样实现控件的动态生成和删除: 1.首先,我们创建一个新的窗体,使用一个panel控件(控件的Dock属性设置...
  • DannyIsCoder
  • DannyIsCoder
  • 2017年05月31日 11:14
  • 1702

关于动态生成控件和动态获得控件的句柄值

从问题者的发的贴子:help me里面需要动态获得控件的句柄值,这些日子在vs.net里试了一下,总结出一点点经验,如下:1.动态生成控件:不管是在asp.net里还是在windows applica...
  • xxxhn
  • xxxhn
  • 2005年04月26日 01:41
  • 1886

如何动态生成多个相同的控件

选择一下容器控件放入form中,本例子选用的是table,并且数据全部是从DB中读取出来的.动态生成多个checkbox,然后对checkbox的事件进行相应的处理:        For Each ...
  • wangpeixian
  • wangpeixian
  • 2009年04月06日 21:44
  • 1640

delphi程序中动态生成控件的方法

程序中动态生成控件的方法分为三步,首先,定义生成的控件类型,再用Create函数生成控件,最后对控件的相关属性赋值。以TButton控件为例,步骤如下: ---- a. 定义控件类型 var Butt...
  • niray
  • niray
  • 2007年03月31日 22:31
  • 2544

BCB 动态生成控件

窗体上的所有对象与窗体本身都成为一个类:TForm1,我们就是将类成员与类成员函数写到TForm1类中,程序运行时,实际上就是通过TForm1 类生成Form1对象, 所以,Unit1.cpp...
  • u014438664
  • u014438664
  • 2015年03月29日 23:46
  • 521

【Android】Android Stdio创建App控件

activity_main.xml
  • Taily_Duan
  • Taily_Duan
  • 2016年08月12日 16:47
  • 332
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:动态生成控件
举报原因:
原因补充:

(最多只允许输入30个字)