动态生成带格式页面

来源:互联网 发布:卡夫卡变形记 知乎 编辑:程序博客网 时间:2024/06/05 00:40

  int[] count = null;//存放每一组考核标题中对应的考核项数量.它的Length就是标题数量
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet2TableAdapters.ScaleTitleTableAdapter st = new DataSet2TableAdapters.ScaleTitleTableAdapter();
        DataSet2.ScaleTitleDataTable dt = new DataSet2.ScaleTitleDataTable();
        DataSet3.DetailScaleDataTable dsdt = new DataSet3.DetailScaleDataTable();
        DataSet3TableAdapters.DetailScaleTableAdapter dsst = new DataSet3TableAdapters.DetailScaleTableAdapter();
        dt = st.GetScaleTitle();

        TableRow rowHead = null;
        TableRow rowTR = null;
        TableRow rowFoot = null;
        int j = 1;//标题号
        count = new int[dt.Rows.Count];
        foreach (DataSet2.ScaleTitleRow row in dt.Rows)
        {
            rowHead = this.createHead(row[1].ToString());//标题行
            this.Table.Rows.Add(rowHead);
            dsdt = dsst.GetDataByID(int.Parse(row[0].ToString()));
            int i = 1; //行号
            count[j-1] = dsdt.Rows.Count;
            foreach(DataSet3.DetailScaleRow row1 in dsdt.Rows)
            {
                rowTR = this.createTR(row1[1].ToString(),i,j,row1[0].ToString());//考核项内容行
                this.Table.Rows.Add(rowTR);
                i++;
            }
            rowFoot = this.createFoot(j);//数量统计行
            this.Table.Rows.Add(rowFoot);
            j++;
        }
        TableRow LastFoot = this.createFoot(j); //最后一行的总数量
        this.Table.Controls.Add(LastFoot);  

    }

    private TableRow createHead(string head)
    {
        TableRow row = new TableRow();
        TableCell[] cell = new TableCell[7];
        for (int i = 0; i < 7; i++)
        {
            cell[i] = new TableCell();
        }
        cell[0].Text = head;
        cell[0].Width = 500;
        for (int i = 1; i < 6; i++)
        {
            cell[i].Text = i.ToString();
            cell[i].Width = 30;
            cell[i].HorizontalAlign = HorizontalAlign.Center;
        }
        cell[6].Text = "Notes,examples";
        cell[6].HorizontalAlign = HorizontalAlign.Right;
        for (int i = 0; i < 7; i++)
        {
            cell[i].Font.Size = 17;
            cell[i].Font.Bold = true;
            row.Cells.Add(cell[i]);
        }
        return row;
    }

    private TableRow createTR(string tr,int r,int p,string trid)
    {
        TableRow row = new TableRow();
        TableCell[] cell = new TableCell[7];
        for (int i = 0; i < 7; i++)
        {
            cell[i] = new TableCell();
        }
        cell[0].Text = tr;
        cell[0].Width = 500;
        RadioButton[] rdo = new RadioButton[5];
        for (int i = 1; i < 6; i++)
        {
            rdo[i - 1] = new RadioButton();
            rdo[i - 1].ID = "rdo"+"_"+p+"*"+r+"_"+i;//r为行号.i为列号
            rdo[i - 1].GroupName = "score"+p+r;
            rdo[i - 1].AutoPostBack = true;
            rdo[i - 1].EnableViewState = true;
            rdo[i - 1].CheckedChanged += new EventHandler(_Default_CheckedChanged);
            cell[i].Width = 30;
            cell[i].HorizontalAlign = HorizontalAlign.Center;
            cell[i].Controls.Add(rdo[i-1]);
        }
        TextBox tb = new TextBox();
        tb.ID = "tb" + p+r;
        tb.Width = 300;
        cell[6].Controls.Add(tb);
        for (int i = 0; i < 7; i++)
        {
            cell[i].Font.Size = 10;
            row.Cells.Add(cell[i]);
        }
        return row;
    }

    void _Default_CheckedChanged(object sender, EventArgs e)
    {
        int sum = 0;
        string rdoid = ((RadioButton)sender).ID;
        int idf = int.Parse(rdoid.Substring(rdoid.IndexOf('_') + 1, rdoid.IndexOf('*') - rdoid.IndexOf('_') - 1));//标题号
        int score = 0;
        for (int i = 0; i < count[idf-1]; i++)
        {
            for (int j = 1; j < 6; j++)
            {
                if (((RadioButton)Table.FindControl("rdo" + "_" + idf + "*" + (i + 1) + "_" + j)).Checked == true)
                {
                    score += j;
                }
            }
        }
        string lbname = "lb" + idf;
        ((Label)Table.FindControl(lbname)).Text = score.ToString();
        int x;
        for (x = 1; x <= count.Length; x++)
        {
            sum += int.Parse(((Label)Table.FindControl("lb" + x)).Text);
        }
        ((Label)Table.FindControl("lb" + x)).Text = sum.ToString();
    }

    private TableRow createFoot(int j)
    {
        TableRow row = new TableRow();
        TableCell[] cell = new TableCell[7];
        for (int i = 0; i < 7; i++)
        {
            cell[i] = new TableCell();
        }
        cell[0].Text = "Overall estimation";
        cell[0].Width = 500;
        cell[1].Text = "";
        cell[1].Width = 30;
        cell[2].Text = "";
        cell[2].Width = 30;
        cell[3].Text = "";
        cell[3].Width = 30;
        cell[4].Text = "";
        cell[4].Width = 30;
        cell[5].Text = "";
        cell[5].Width = 30;
        Label l = new Label();
        l.ID = "lb" + j;
        l.Text = "0";
        l.Width = 30;
        cell[6].Controls.Add(l);
        cell[6].HorizontalAlign = HorizontalAlign.Right;
        for (int i = 0; i < 7; i++)
        {
            cell[i].BackColor = System.Drawing.Color.Silver;
            cell[i].Font.Bold = true;
            row.Cells.Add(cell[i]);
        }
        //Response.Write("<script>alert('" + l.ID + "');</script>");
        return row;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        this.clearall();
    }
    private void clearall()
    {
        int i;
        for (i = 1; i <= count.Length; i++)
        {
            ((Label)Table.FindControl("lb" + i)).Text = "0";//把页面上统计数量的标签全部清空
            for (int x = 1; x <= count[i - 1]; x++)
            {
                ((TextBox)Table.FindControl("tb" + i + x)).Text = "";
                for (int j = 1; j < 6; j++)
                {
                    ((RadioButton)Table.FindControl("rdo" + "_" + i + "*" + x + "_" + j)).Checked = false;
                }
            }
        }
        ((Label)Table.FindControl("lb" + i)).Text = "0";//最后一个总的清空.
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.saveTodatabase();
        this.clearall();
    }

    private void saveTodatabase()
    {
       
    }