在asp.net表控件中为动态行设置行距 [英] Setting Rowspan for Dynamic Rows in asp.net Table Control

查看:114
本文介绍了在asp.net表控件中为动态行设置行距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个asp.net表,并使用Backend(C#)动态地向表中添加了行.

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

行的第一列应具有 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 :(

这是页面首次加载的时间:

这是第一次发生回发:

这是第二次回发一次:

预先感谢

这是我完整的代码:

.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;
            }

        }
    }

推荐答案

只需从

td_pk.RowSpan = td_pk.RowSpan + 1;

td_pk.RowSpan = numberOfRecords + 1;

这篇关于在asp.net表控件中为动态行设置行距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆