How to add controls dynamically in asp.net

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Xml.Serialization;
using  System.IO;
using  System.Runtime.Serialization;
using  System.Xml;
using  System.Web.Security;
public   partial   class  Default2 : System.Web.UI.Page
{
    
protected   void  Page_Load( object  sender, EventArgs e)
    {

    }
    
protected   override   void  LoadViewState( object  savedState)
    {
        
base .LoadViewState(savedState);
        Panel1.Controls.Clear();
        
int  rows  =   int .Parse(ViewState[ " Rows " ].ToString());
        
if  (ViewState[ " Rows " !=   null )
        {
            Table tb 
=   new  Table();
            tb.ID 
=   " AllRows " ;
            TableRow tr 
=   null ;
            
for  ( int  r  =   0 ; r  <  rows; r ++ )
            {
                tr 
=   new  TableRow();
                
for  ( int  c  =   0 ; c  <   3 ; c ++ )
                {
                    TableCell tc 
=   new  TableCell();
                    tc.ID 
=   " tc_ "   +  r.ToString()  +   " _ "   +  c.ToString();
                    tr.Cells.Add(tc);
                }
                TextBox txt 
=   new  TextBox();
                txt.ID 
=   " TextBox_ "   +  r.ToString()  +   " _0 " ;
                tr.Cells[
0 ].Controls.Add(txt);

                Button bt 
=   new  Button();
                bt.ID 
=   " bt_ "   +  r.ToString()  +   " _Del " ;
                bt.CommandArgument 
=  r.ToString();
                bt.Text 
=   " Delete current row " ;
                bt.Click 
+=   new  EventHandler(bt_Click);
                tr.Cells[
1 ].Controls.Add(bt);
                tb.Rows.Add(tr);
            }
            Panel1.Controls.Add(tb);
            
//  this.Panel1.Controls.Add(tb);
        }
    }

    
void  bt_Click( object  sender, EventArgs e)
    {
        Button bttemp 
=  sender  as  Button;
        Table result 
=  Panel1.FindControl( " AllRows " as  Table;
        result.Rows.RemoveAt(
int .Parse(bttemp.CommandArgument));
        
int  rows  =  ViewState[ " Rows " ==   null   ?   1  :  int .Parse(ViewState[ " Rows " ].ToString());
        ViewState[
" Rows " =  (rows  -   1 ).ToString();      
    }
    
protected   void  Button1_Click( object  sender, EventArgs e)
    {
        Panel1.Controls.Clear();
        
int  rows  =  ViewState[ " Rows " ==   null   ?   1  :  int .Parse(ViewState[ " Rows " ].ToString())  +   1 ;
        Table tb 
=   new  Table();
        tb.ID 
=   " AllRows " ;
        TableRow tr 
=   null ;
        
for  ( int  r  =   0 ; r  <  rows; r ++ )
        {
            tr 
=   new  TableRow();
            
for  ( int  c  =   0 ; c  <   3 ; c ++ )
            {
                TableCell tc 
=   new  TableCell();
                tc.ID 
=   " tc_ "   +  r.ToString()  +   " _ "   +  c.ToString();
                tr.Cells.Add(tc);
            }
            TextBox txt 
=   new  TextBox();
            txt.ID 
=   " TextBox_ "   +  r.ToString()  +   " _0 " ;
            tr.Cells[
0 ].Controls.Add(txt);

            Button bt 
=   new  Button();
            bt.ID 
=   " bt_ "   +  r.ToString()  +   " _Del " ;
            bt.Click 
+=   new  EventHandler(bt_Click);
            bt.CommandArgument 
=  r.ToString();
            bt.Text 
=   " Delete current row " ;

            tr.Cells[
1 ].Controls.Add(bt);
            tb.Rows.Add(tr);
        }
        Panel1.Controls.Add(tb);
        ViewState[
" Rows " =  rows.ToString();
    }
    
protected   void  Button2_Click( object  sender, EventArgs e)
    {
        Table result 
=  Panel1.FindControl( " AllRows " as  Table;
        
foreach  (TableRow tr  in  result.Rows)
        {
            Response.Write((tr.Cells[
0 ].Controls[ 0 as  TextBox).Text + " <br> " );
        }
    }
}
 
//
< form  id ="form1"  runat ="server" >
    
< div >   
        
< asp:Panel  ID ="Panel1"  runat ="server" >
        
</ asp:Panel >
        
< asp:Button  ID ="Button1"  runat ="server"  onclick ="Button1_Click"  Text ="Add Row"  
            Width
="75px"   />   
        
< asp:Button  ID ="Button2"  runat ="server"  Text ="Results"  
            onclick
="Button2_Click"   />    
    
</ div >
    
</ form >

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