在asp.net表格控件

时间:2016-09-20 04:11:35

标签: c# asp.net

我创建了一个asp.net表,并在使用Backend(C#)的表动态中添加了行。

行的第一列应具有 Rowspan

行的添加非常精细,Rowspan也可以正常添加,但当回发时, Rowspans会自动更改

任何人都可以帮我解决这个问题:(

这是第一次加载页面时

page first load 这是第一次 PostBack Occured:

PostBack first time 这是 Postback Occured Second Time:

PostBack second time 提前致谢

这是我的完整代码:

.aspx代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .tbl {
        border:1px solid #000000;
        }
         .tbl td,th {
        border:1px solid #000000;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:Table runat="server" ID="tbl_fund" CssClass="tbl"></asp:Table>
        <asp:Button runat="server" Text="Do Post Back" />
    </div>
    </form>
</body>
</html>

C#代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        PrintFundChanges();
    }
    public void PrintFundChanges()
    {
        //***************************Header Started*****************************
        TableRow tr_head = new TableRow();
        TableHeaderCell th1 = new TableHeaderCell();

        th1.Text = "CNIC";
        TableHeaderCell th2 = new TableHeaderCell();
        th2.Text = "Field Name";
        TableHeaderCell th3 = new TableHeaderCell();
        th3.Text = "Updated Value";
        TableHeaderCell th4 = new TableHeaderCell();
        th4.Text = "Current Value";

        TableHeaderCell th5 = new TableHeaderCell();
        CheckBox chk_head = new CheckBox();
        chk_head.ID = "chk_fund";
        chk_head.CheckedChanged += new EventHandler(Chk_Fund_CheckedChanged);
        chk_head.Text = "Approve / Disapprove";
        chk_head.AutoPostBack = true;
        th5.Controls.Add(chk_head);

        TableHeaderCell th6 = new TableHeaderCell();
        th6.Text = "Table Name";
        th6.Style.Add("display", "none");
        TableHeaderCell th7 = new TableHeaderCell();
        th7.Text = "Column Name";
        th7.Style.Add("display", "none");
        TableHeaderCell th8 = new TableHeaderCell();
        th8.Text = "pk_code";
        th8.Style.Add("display", "none");
        TableHeaderCell th9 = new TableHeaderCell();
        th9.Text = "pk2_code";
        th9.Style.Add("display", "none");
        TableHeaderCell th10 = new TableHeaderCell();
        th10.Text = "action_type";
        th10.Style.Add("display", "none");

        th1.Style.Add("width", "20%");
        th2.Style.Add("width", "20%");
        th3.Style.Add("width", "20%");
        th4.Style.Add("width", "20%");

        tr_head.Cells.Add(th1);
        tr_head.Cells.Add(th2);
        tr_head.Cells.Add(th3);
        tr_head.Cells.Add(th4);
        tr_head.Cells.Add(th5);
        tr_head.Cells.Add(th6);
        tr_head.Cells.Add(th7);
        tr_head.Cells.Add(th8);
        tr_head.Cells.Add(th9);
        tr_head.Cells.Add(th10);
        tbl_fund.Rows.Add(tr_head);

        //****************************Header Ended*******************************
        //****************************Print Updated Data Started*************************

        //Getting distinct primary key data
        //DataTable dt_DistinctPk = dt_FundUpdatedFields.DefaultView.ToTable(true, "pk_code");
        DataTable dt_DistinctPk = new DataTable();
        dt_DistinctPk.Columns.Add(new DataColumn("pk_code", typeof(string)));
        DataRow dr = dt_DistinctPk.NewRow();
        dr["pk_code"] = "123";
        dt_DistinctPk.Rows.Add(dr);
        dr = dt_DistinctPk.NewRow();
        dr["pk_code"] = "456";
        dt_DistinctPk.Rows.Add(dr);

        for (int i = 0; i < dt_DistinctPk.Rows.Count; i++)
        {
            TableCell td_pk = new TableCell();
            td_pk.Text = "<b style='font-size:13px !important;'>" + dt_DistinctPk.Rows[i]["pk_code"].ToString() + "</b>";
            //int numberOfRecords = dt_FundUpdatedFields.Select("pk_code = " + dt_DistinctPk.Rows[i]["pk_code"].ToString()).Length;
            int numberOfRecords =5;
            td_pk.RowSpan = numberOfRecords;
            bool IsAddAtOnce = true;

             for (int j = 0; j < 5; j++)
             {            
                    string field_name_lable = "Column name here";                    
                    TableRow tr = new TableRow();
                    TableCell td1 = new TableCell();
                    td1.Text = field_name_lable;
                    TableCell td2 = new TableCell();
                    TableCell td3 = new TableCell();
                    td2.Text = "New Value here";
                    td3.Text = "Old Value here";                    
                    TableCell td4 = new TableCell();
                    CheckBox chk = new CheckBox();
                    chk.ID = "chk_" +j;
                    //td4.Controls.Add(chk);
                    TableCell td5 = new TableCell();
                    td5.Text = "tbl_name";
                    td5.Style.Add("display","none");                 
                    TableCell td6 = new TableCell();
                    td6.Text = "field_name";
                    td6.Style.Add("display", "none");
                    TableCell td7 = new TableCell();
                    td7.Text = j.ToString();
                    td7.Style.Add("display", "none");
                    TableCell td8 = new TableCell();
                    td8.Text = j.ToString();
                    td8.Style.Add("display", "none");
                    TableCell td9 = new TableCell();
                    td9.Text = "1";
                    td9.Style.Add("display", "none");

                if (IsAddAtOnce)
                    {
                        tr.Cells.Add(td_pk);
                        IsAddAtOnce = false;
                    }
                    tr.Cells.Add(td1);
                    tr.Cells.Add(td2);
                    tr.Cells.Add(td3);
                    tr.Cells.Add(td4);
                    tr.Cells.Add(td5);
                    tr.Cells.Add(td6);
                    tr.Cells.Add(td7);
                    tr.Cells.Add(td8);
                    tr.Cells.Add(td9);
                    tbl_fund.Rows.Add(tr);
                if (j ==5 - 1)//Last Termination of Loop
                {
                    //Printing Attachments                 

                            td_pk.RowSpan = td_pk.RowSpan + 1;
                            td1 = new TableCell();
                            td1.Text = "CNIC Attachment";
                            td2 = new TableCell();
                            Image img_new = new Image();
                           // img_new.ImageUrl = "ImgHandler.ashx?typ=Newfund&emp=" + Requestor + "&pk=" + dt_FundUpdatedAttachments.Rows[k]["pk_code"].ToString();

                            td2.Controls.Add(img_new);
                            td3 = new TableCell();
                            Image img_old = new Image();
                            //img_old.ImageUrl = "ImgHandler.ashx?typ=Currfund&emp=" + Requestor + "&pk=" + dt_FundUpdatedAttachments.Rows[k]["pk_code"].ToString();

                            td3.Controls.Add(img_old);
                            td4 = new TableCell();
                            chk = new CheckBox();
                            //chk.ID = "chk_" + dt_FundUpdatedAttachments.Rows[k]["tbl_name"].ToString().ToUpper() + "_" + dt_FundUpdatedAttachments.Rows[k]["field_name"].ToString().ToUpper() + "_" + dt_FundUpdatedAttachments.Rows[k]["pk_code"].ToString().ToUpper() + "_";
                            td4.Controls.Add(chk);
                            td5 = new TableCell();
                            td5.Text = "tbl_name";
                            td5.Style.Add("display", "none");
                            td6 = new TableCell();
                            td6.Text ="field_name";
                            td6.Style.Add("display", "none");
                            td7 = new TableCell();
                            td7.Text = j.ToString();
                            td7.Style.Add("display", "none");
                            td8 = new TableCell();
                            td8.Text = "";
                            td8.Style.Add("display", "none");
                            td9 = new TableCell();
                            td9.Text = "1";
                            td9.Style.Add("display", "none");
                            tr = new TableRow();
                            tr.Cells.Add(td1);
                            tr.Cells.Add(td2);
                            tr.Cells.Add(td3);
                            tr.Cells.Add(td4);
                            tr.Cells.Add(td5);
                            tr.Cells.Add(td6);
                            tr.Cells.Add(td7);
                            tr.Cells.Add(td8);//pk2_code
                            tr.Cells.Add(td9);//action_type
                            tbl_fund.Rows.Add(tr);

                }
            }
        }

        ////****************************Print New Data Ended***************************

            TableRow tr_blank = new TableRow();
            TableCell td_blank = new TableCell();
            td_blank.Text = "Deleted Data";
            td_blank.Style.Add("font-weight", "bold !important");
            td_blank.Style.Add("font-size", " 16px !important");
            td_blank.Style.Add("background-color", "#e3e3e3 !important");
            td_blank.ColumnSpan = 10;
            tr_blank.Cells.Add(td_blank);
            tbl_fund.Rows.Add(tr_blank);


        ////****************************Print Deleted Data Started***************************

        //****************************Print Deleted Data Ended***************************

    }
    protected void Chk_Fund_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        bool IsChecked = chk.Checked;
        for (int i = 1; i < tbl_fund.Rows.Count; i++)
        {
            string pk_code = "";
            string tbl_name = "";
            string col_name = "";
            string pk2_code = "";
            int CellsCount = tbl_fund.Rows[i].Cells.Count;
            /*In this table rowspan is used for printing cnic number,
             so the row who has rowspan will have cnic column in first termination ,
             while in second termination the cell count will decrease because the
             row before the current row will having the rowspan.
             */
            if (CellsCount == 10)
            {
                tbl_name = tbl_fund.Rows[i].Cells[5].Text;
                col_name = tbl_fund.Rows[i].Cells[6].Text;
                pk_code = tbl_fund.Rows[i].Cells[7].Text;
                pk2_code = tbl_fund.Rows[i].Cells[8].Text;
            }
            else if (CellsCount == 9)
            {
                tbl_name = tbl_fund.Rows[i].Cells[4].Text;
                col_name = tbl_fund.Rows[i].Cells[5].Text;
                pk_code = tbl_fund.Rows[i].Cells[6].Text;
                pk2_code = tbl_fund.Rows[i].Cells[7].Text;
            }

            if (tbl_name != "")//if it is not a blank/header row
            {
                CheckBox ck = new CheckBox();
                string chk_id = "chk_" + tbl_name + "_" + col_name + "_" + pk_code + "_" + pk2_code;
                ck = (CheckBox)tbl_fund.Rows[i].Cells[4].FindControl(chk_id);//chk_tblname_colname_pk
                ck.Checked = IsChecked;
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

只需从

更改此行
td_pk.RowSpan = td_pk.RowSpan + 1;

td_pk.RowSpan = numberOfRecords + 1;