0

I've created an asp.net table and added rows to the table dynamically that is using Backend ( C# ) .

The First Column of the Rows should have Rowspan .

The rows are being added very fine and the Rowspan is also added fine but when the Postback occurs , The Rowspans gets changes automatically .

Can any one please help me to solve this problem :(

This is when the page loads First Time:

page first load This is when PostBack Occured First Time :

PostBack first time This is when Postback Occured Second Time :

PostBack second time Thanks in advance

Here is my complete code :

.aspx Code

<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# Code :

 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 Answer 1

1

just change the this line from

td_pk.RowSpan = td_pk.RowSpan + 1;

to

td_pk.RowSpan = numberOfRecords + 1;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked . But What a silly logic behind it on postback ? can you please elaborate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.