Coding For Fun
宠辱不惊,看庭前花开花落~ 去留无意,任天上云卷云舒

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;


using IBatisNet.Common;
using IBatisNet.DataMapper;
using IBatisNet.Common.Utilities;
using DAO;
using Model;

public partial class DynamicControl_two : System.Web.UI.Page
{
    private MainProcess[] main;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ViewState["MP"] == null)
        {
            main = new MainProcess[1];
            main[0] = new MainProcess();
           // main[0].addProcess(new MainProcess.Process());
            ViewState["MP"] = main;
            //Response.Write("<script language=javascript>alert('null')</script>");
            addMainProcess();
        }
        else
        {
              //Response.Write("<script language=javascript>alert('Load')</script>");
            this.Container.Controls.Clear();
            addMainProcess();
        }
    }
    private void addMainProcess()
    {
        main = (MainProcess[])ViewState["MP"];
        for (int num = 1; num < main.Length+ 1; num++)
        {
            Table table = new Table();
            table.ID = "_table" + num;
            TableRow trHead = new TableRow();
            TableCell tcHead = new TableCell();

            //add table head
            Label lblHead = new Label();
            lblHead.ID = "_lblHead" + num;
            lblHead.Text = "MainProcess: " + num;

            Button btnDeleteMainProcess = new Button();
            btnDeleteMainProcess.ID = "_btnDeleteMainProcess" + num;
            btnDeleteMainProcess.Text = "DeleteMainProcess";
            btnDeleteMainProcess.Click += new EventHandler(btnDeleteMainProcess_Click);
            btnDeleteMainProcess.CommandName = "_deleteMainProcess" + num;

            Button btnAddProcess = new Button();
            btnAddProcess.ID = "_btnAddProcess" + num;
            btnAddProcess.Text = "AddProcess";
            btnAddProcess.Click += new EventHandler(btnAddProcess_Click);
            btnAddProcess.CommandName = "_addProcess" + num;

            tcHead.Controls.Add(lblHead);
            tcHead.Controls.Add(btnDeleteMainProcess);
            tcHead.Controls.Add(btnAddProcess);
            trHead.Controls.Add(tcHead);
            table.Controls.Add(trHead);
           
       
            for (int i = 1; i < main[num-1].ProList.Length+1; i++)
            {              
                table.Controls.Add(addProcess(num,i));
            }
            this.Container.Controls.Add(table);
        }
    }

    private TableRow addProcess(int mainIndex,int proIndex)
    {
        string location = mainIndex.ToString() + proIndex.ToString();
        //add table row
        TableRow tri = new TableRow();
        TableCell tci = new TableCell();

        Label lblProcessName = new Label();
        lblProcessName.ID = "_lblProName" + location;
        lblProcessName.Text = "Process" + proIndex + ":";

        TextBox txtProcessName = new TextBox();
        txtProcessName.ID = "_txtProcessName" + location;

        Label lblPrincipal = new Label();
        lblPrincipal.ID = "_lblPrincipal" + location;
        lblPrincipal.Text = "Principal:";

        DropDownList ddlPrincipal = new DropDownList();
        ddlPrincipal.ID = "_ddlPrincipal" + location;
        //bindDDL(ddlPrincipal);

        Button btnDeleteProcess = new Button();
        btnDeleteProcess.ID = "_btnDeleteProcess" + location;
        btnDeleteProcess.Text = "DeleteProcess" + proIndex;
        btnDeleteProcess.Click += new EventHandler(btnDeleteProcess_Click);
        btnDeleteProcess.CommandName = "_deleteProcess" + location;


        tci.Controls.Add(lblProcessName);
        tci.Controls.Add(txtProcessName);
        tci.Controls.Add(lblPrincipal);
        tci.Controls.Add(ddlPrincipal);
        tci.Controls.Add(btnDeleteProcess);

        tri.Controls.Add(tci);
        return tri;
    }

    protected void btnAddMainProcess_Click(object sender, EventArgs e)
    {
       // Response.Write("<script language=javascript>alert('addMain')</script>");
        main = (MainProcess[])ViewState["MP"];
        MainProcess[] temp = new MainProcess[main.Length + 1];
        for (int i = 0; i < main.Length; i++)
        {
            temp[i] = main[i];
        }
        temp[main.Length] = new MainProcess();
        ViewState["MP"] = temp;
    }  

    void btnDeleteMainProcess_Click(object sender, EventArgs e)
    {
       // Response.Write("<script language=javascript>alert('DeleteMain')</script>");
        main = (MainProcess[])ViewState["MP"];
        string flag = ((Button)sender).CommandName;
        int index =int.Parse(flag.Substring(flag.Length-1,1))-1;

        MainProcess[] temp = new MainProcess[main.Length - 1];
        for (int i = 0; i < main.Length; i++)
        {
            if (i < index) temp[i] = main[i];
            else if (i > index) temp[i - 1] = main[i];
        }
        ViewState["MP"] = temp;
    }

    void btnAddProcess_Click(object sender, EventArgs e)
    {
        string flag = ((Button)sender).CommandName;
        int index = int.Parse(flag.Substring(flag.Length - 1, 1)) - 1;
        main = (MainProcess[])ViewState["MP"];
        main[index].addProcess(new MainProcess.Process());
        ViewState["MP"] = main;
    }
    void btnDeleteProcess_Click(object sender, EventArgs e)
    {
        string flag = ((Button)sender).CommandName;
        string location = flag.Substring(flag.Length - 2, 2);
        int mpIndex = int.Parse(flag.Substring(flag.Length - 2, 1)) - 1;
        int proIndex = int.Parse(flag.Substring(flag.Length - 1, 1)) - 1;
        main = (MainProcess[])ViewState["MP"];
        main[mpIndex].removeProcess(proIndex);
        ViewState["MP"] = main;
    }
    private void readData()
    {
        main = (MainProcess[])ViewState["MP"];
        for (int i = 0; i < main.Length; i++)
        {
            Table tabel = (Table)Container.FindControl("_table" + (i+1));
            for (int row = 1; row < tabel.Rows.Count; row++)
            {
               // main[i].ProList[row].ProName = ((TextBox)tabel.Rows[row].Cells[0].FindControl("_txtProcessName" + (i+1).ToString() + (row+1))).Text;
                main[i].ProList[row-1].ProName = ((TextBox)tabel.Rows[row].Cells[0].Controls[1]).Text;
                Response.Write(main[i].ProList[row-1].ProName + "<br>");
            }
        }
        ViewState["MP"] = main;
    }
    private void bindData()
    {

    }
    private void bindDDL(DropDownList ddl)
    {
        ddl.DataSource = Mapper.Instance().QueryForList("SelectArticalDetail", null);
        ddl.DataTextField = "Author_Name";
        ddl.DataValueField = "Detail_ID";
        ddl.DataBind();
        ddl.Items.Insert(0, new ListItem("", "-1"));
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        readData();
    }
}

posted on 2007-08-25 17:27  Flynn  阅读(314)  评论(0编辑  收藏  举报