List<Column> colList = new List<Column>();    ColumnManager cm = new ColumnManager();    string dbname = null;protected void Button5_Click(object sender, EventArgs e)
    {
        if (colList == null) 
        {
            colList = new List<Column>();
        }        int count = 0;        for (int i = 0; i < this.ListBox2.Items.Count; i++) 
        {
            if (this.txtColName.Text.Trim().Equals(this.ListBox2.Items[i].Text)) 
            {
                count++;
            }
                      
        }        if (count == 0)
        {
            Column column = new Column();
            column.Name = this.txtColName.Text;
            column.DbType = this.DropDownList1.Text.ToString();
            this.ListBox2.Items.Add(column.Name);
            colList.Add(column);
        }
        else 
        {
            Response.Write("<script language='javascript' type='text/javascript'>alert('此表已包含此字段')</script>");
        }        
    }protected void Button4_Click(object sender, EventArgs e)
    {
        IList<Column> all = new List<Column>();
        
        。        if (colList.Count > 0) 
        {
            for (int i = 0; i < colList.Count; i++) 
            {
                all.Add(colList[i]);
            }
        }        string sql = "USE " + dbname + " " +
                     "GO " +
                     "CREATE TABLE " + tablename + " " +
                     "( ";
        for (int i = 0; i < all.Count; i++) 
        {
            Column column=(Column)all[i];
            sql += column.Name + " " + column.DbType+", ";
        }
        sql = sql.Substring(0, sql.Length - 2) + " )";
                        
    }
问题是:Button5的Click事件触发后colList有值,再触发Button4的Click事件时colList就没值了;
我试过让colList在Button5的Click的事件里面实例化,但是还是就没有效果

解决方案 »

  1.   

    你点Button5 页面提交了。刷新了页面,再点Button4的时候又是一次新的页面周期啊。
      

  2.   

    Button提交后页面回发导致colList重新实例化,如果想保存colList的值,那就放到ViewState中,
      

  3.   

    List<Column> colList = new List<Column>();
    点击Button4 要实例化一次 所以是空的
      

  4.   

    你将List<Column> colList = new List<Column>();改为List<Column> colList;试试
      

  5.   

    if (colList == null) 
            {
                colList = new List<Column>();
            }然后
    Button4中判断
    if (colList == null) 
            {
                colList = new List<Column>();
            }
      

  6.   

    这个很显然啦。button4里重新实例化了下个collist,当然是空的。
      

  7.   

    1楼说的对 button4点击就刷新了页面 文本框的值自然清空了 点button5取出来的自然是NULL
      

  8.   

    页面刷新了colList当然会清空的,使用ViewState将colList保存起来.
      

  9.   


    List<Column> colList = new List<Column>();你点击button4的时候,页面重新加载,上面这句话执行,然后button5不执行
    等于你只是对colList进行了实例,所以为NULL