asp.net动态添加控件

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  Default3 : System.Web.UI.Page
{
    
public bool IsDynamicLoadControl
    
{
        
get
        
{
            
object dynamic = ViewState["IsDynamicLoadControl"];
            
return dynamic == null ? false : true;
        }

        
set
        
{
            ViewState[
"IsDynamicLoadControl"= value;
        }

    }

    
protected override void LoadViewState(object savedState)
    
{
        
base.LoadViewState(savedState);
        
if (IsDynamicLoadControl)
        
{
            LoadTextBox();
        }

    }


    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            LoadTextBox();
        }

    }

    
private void LoadTextBox()
    
{
        
for (int i = 0; i < 10; i++)
        
{
            TextBox input 
= new TextBox();
            input.ID 
= "input" + i.ToString();
            
this.form1.Controls.Add(input);
        }

        IsDynamicLoadControl 
= true;
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        TextBox tb 
= this.FindControl("input0"as TextBox;
        Page.ClientScript.RegisterStartupScript( 
this.GetType(),""string.Format("<script>alert('{0}')</script>", tb.Text));
    }

}

 

你可能感兴趣的:(asp.net)