zoukankan      html  css  js  c++  java
  • 关于设置table样式后,不停点击按钮table中的数据会逐渐被最后一行替换的问题

    方法1:初始化htmlTable中的数据,table中有两行表头,其页面写法是:

    <table id="MyTable" runat="server">

      <tr></tr>

      <tr></tr>

    </table>

            private void initTable()
            {

            System.Data.DataTable dt = GetTonKilo();
               // int j = 0;
                foreach(DataRow dr in dt.Rows)
                {
                    HtmlTableRow tr = new HtmlTableRow();
                    myTable.Rows.Add(tr);
                    for (int i = 0; i < dr.ItemArray.Length; i++)
                    {
     
                        HtmlTableCell tc = new HtmlTableCell();
                        tc.InnerText = dr[i].ToString();
                       // if (j % 2 != 0)
                       // {
                        //    tc.BgColor = "#CCCCCC";
                       // }
                      //  else
                       // {
                          //  tc.BgColor = "#FFFFFF";
                       // }
                        tr.Cells.Add(tc);
                    }
                    myTable.Rows.Add(tr);
                   // j++;
                }
              
            }

    方法二:设置table的样式

            private void setTableStyle( )
            {
                myTable.CellPadding = 0;
                myTable.CellSpacing = 0;


               
                //行
                for (int i = 1; i <= myTable.Rows.Count;i++ )
                {

                    if(i>2)
                    {
                        if (i % 2 == 0)
                        {

                            //单元格
                            for (int j = 0; j < myTable.Rows[i-1].Cells.Count; j++)
                            {
                                myTable.Rows[i-1].Cells[j].BgColor = "#CCCCCC";
                            }

                        }
                        else
                        {
                            //单元格
                            for (int j = 0; j < myTable.Rows[i-1].Cells.Count; j++)
                            {
                                myTable.Rows[i-1].Cells[j].BgColor = "#FFFFFF";
                            }
                        }                   
                    }
                }

            }

    在页面PageLoad中先后调用方法一,方法二

            protected void Page_Load(object sender, EventArgs e)
            {                  
                initTable();
                setTableStyle();
               
            }

    不停的点击页面按钮表格中的数据会逐步被最后一行替换!为什么呢?

    现在只能在填充数据的时候,设置好样式

  • 相关阅读:
    Linux.Unix.windows的纠结史
    第一次来到博客园
    canvas的fillText参数解释
    【转】Javascript画立体玫瑰
    C#学习笔记(有C,C++,JAVA语言基础)
    推荐NHibernate新书:NHibernate 3.0 CookBook[附下载]
    NHibernate之旅系列文章导航
    存储过程
    .Net下的 ORM框架介紹
    十步让你成为更优秀的程序员
  • 原文地址:https://www.cnblogs.com/caoyuzhi/p/2853106.html
Copyright © 2011-2022 走看看