C#增加连续的动态控件并取值的例子

  • 功能

    • 每点一次页面上的增加控件的Button, 增加一个TextBox到Panel中。点击取值的Button,可以获取前面增加的所有TextBox的值说明动态增加的控件,需要在PostBack时重新加载一次,在重复加载时,我们只需要指点控件的ID,其它属性可以不设定了。

CS CODE

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  AddTextBoxSample : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        ReLoadAddedTextBoxs();
    }

    
// 重新加载已经加载过的TextBox
     private   void  ReLoadAddedTextBoxs()
    {
            
for  ( int  i  =   1 ; i  <=  AddedTextBoxIds.Count; i ++ )
            {
                TextBox tb 
=   new  TextBox();
                tb.ID 
=  i.ToString();
                Panel1.Controls.Add(tb);
            }
    }

    
private   const   string  c_ids  =   " TextBoxIds " ;
    
///   <summary>
    
///  纪录下动态增加的TextBox的ID值
    
///   </summary>
     private  ArrayList AddedTextBoxIds
    {
        
get
        {
            
if  (ViewState[c_ids]  ==   null )
                ViewState[c_ids] 
=   new  ArrayList();

            
return  ViewState[c_ids]  as  ArrayList;
        }

        
set  { ViewState[c_ids]  =  value; }
    }

    
protected   void  BT_AddControl_Click( object  sender, EventArgs e)
    {
        
// 获取现在要增加的TextBox的ID索引
         string  id  =  Convert.ToString(AddedTextBoxIds.Count  +   1 );
        
        TextBox newBox 
=   new  TextBox();
        newBox.Width 
=  Unit.Pixel( 200 );
        newBox.ID 
=  id;
        newBox.Text 
=   " 现在是增加的第 "   +  id  +   " 个TextBox " ;

        Panel1.Controls.Add(newBox);
        AddedTextBoxIds.Add(id);    
// 纪录下新增加的TextBoxID,在下次PostBack时重新加载
    }
    
protected   void  BT_GetControlsValue_Click( object  sender, EventArgs e)
    {
        
for  ( int  i  =   1 ; i  <=  AddedTextBoxIds.Count; i ++ )
        {
            TextBox tb 
=  Panel1.FindControl(i.ToString())  as  TextBox;
            
if  (tb  !=   null )
            { 
                
// 这里是加载的TextBox的值
                
// tb.Text....
                
// tb.ID.....

            }
        }
    }
}

HTML CODE

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " AddTextBoxSample.aspx.cs "  Inherits = " AddTextBoxSample "   %>

<! 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 > 增加动态的TextBox,并取值的例子 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
        
< asp:Panel  ID ="Panel1"  runat ="server"  Height ="278px"  Width ="549px" >
        
</ asp:Panel >
        
< asp:Button  ID ="BT_AddControl"  runat ="server"  OnClick ="BT_AddControl_Click"  Text ="增加一个TextBoxControl"   />
        
< asp:Button  ID ="BT_GetControlsValue"  runat ="server"  OnClick ="BT_GetControlsValue_Click"
            Text
="取得动态加载的TextBox的值"   /></ div >
    
</ form >
</ body >
</ html >
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值