动态生成 控件 并进行遍历和操作

前台 .aspx
     < form  id ="form1"  runat ="server" >
    
< asp:Label  runat ="server"  ID ="panel"  Visible ="false" ></ asp:Label >
        
< table  border ="0"  cellpadding ="0"  cellspacing ="2"  width ="100%" >
            
< tr >
                
< td >
                    
< div  style ="text-align: left;" >
                        
< asp:Panel  ID ="pan"  runat ="server" ></ asp:Panel >
                    
</ div >
                    
< div  style ="text-align: center;padding-top:5px;" >
                        
< input  type ="button"  runat ="server"  id ="save"  style ="width:80px;border:#333333 1px solid"  value ="确定"  onserverclick ="save_ServerClick"   />
                        
< input  type ="button"  id ="reset"  value ="取消"  style ="width:80px;border:#333333 1px solid"  onclick ="javascript:window.opener=null;window.close();"   />
                    
</ div >
                
</ td >
            
</ tr >
        
</ table >
    
</ form >

.cs 后台代码
   /**/ /// <summary>
    
/// 创建 num 个TextBox
    
/// </summary>
    
/// <param name="num">个数</param>

     protected   void  CreateTextBox( int  num, string [] str)
    
{
        
for (int i = 0; i < num; i++)
        
{
            TextBox tbox 
= new TextBox();
            Label lbl 
= new Label();
            Label lb 
= new Label();
            
if (i == 0)
            
{
                lbl.Text 
= "定价:";
            }

            
else
            
{
                lbl.Text 
= "<br />定价:";
            }

            lb.Text 
= "编号:(" + str[i] + ")";
            tbox.Style.Add(
"width""100px");
            tbox.Style.Add(
"border""#333333 solid 1px");
            pan.Controls.Add(lbl);
            pan.Controls.Add(tbox);
            pan.Controls.Add(lb);
        }

    }

    
protected   void  save_ServerClick( object  sender, EventArgs e)
    
{
        
//遍历页面中Span容器中的所有控件
        foreach (Control mycontrol in Page.FindControl("pan").Controls)
        
{
            
//如果控件是TextBox或者Label
            if (mycontrol.GetType() == typeof(TextBox) || mycontrol.GetType() == typeof(Label))
            
{
                
string bookid = "";
                
string bookvalue = "";
                
if (mycontrol.GetType() == typeof(TextBox))
                
{
                    bookvalue 
= ((TextBox)mycontrol).Text.Trim();   //赋值
                }

                
else
                
{
                    
if (((Label)mycontrol).Text != "定价:")
                    
{
                        bookid 
= ((Label)mycontrol).Text.Trim();    //赋值
                    }

                }

                
if (bookid != "" && bookvalue != "")    //如果 bookid 和 bookvalue 都不为空 执行具体数据操作
                {
                    ;
                }

            }

        }

    }

转载于:https://www.cnblogs.com/sky1982/archive/2008/10/14/1311127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值