zoukankan      html  css  js  c++  java
  • c#通过数据集生成浏览页面

    在做项目时,因为有时候要查看某一个订单的详细信息,不知道如何显示!

    于是做了一个公共方法,生成一个html表格,如图:

     

     公共方法代码:

        public class htmlTable
        {
            
    public htmlTable ()
            {
                Title 
    = "信息";
                Type 
    = "0";
                MarkField 
    = "";
                MarkRowSpan 
    = "3";
                Width 
    ="100%";
                FieldMap 
    = new DataTable("FieldMap");
                Data 
    = new DataTable("Data");
            }
            
    public string Title {get;set;}
            
    public string  Type {get;set;} //0为主表 1为明细表
            public string MarkField { getset; }  //需要大框,比如备注
            public string MarkRowSpan { getset; }  //大框的高度,以行数计算
            public string Width { getset; }
            
            
    public DataTable FieldMap { getset; }  //字段对照,如果没有,则以字段列表名为准
            public DataTable Data { getset; }   //数据

        }

             //返回一个html表格

            public static string GetHtmlTable(htmlTable ht)
            {
                
    if (ht.Data == null || ht.FieldMap == null)
                {
                    
    return "";
                }
                
    if (ht.Data.Rows.Count ==0)
                {
                    
    return "";
                }
                
    //ht.FieldHide = ht.FieldHide.ToLower();

                
    //处理字段对应
                
    //string map="";
                Dictionary <string,string> dic=new Dictionary<string,string> ();
                
    for (int i=0;i<ht.FieldMap.Rows.Count ;i++)
                {
                    DataRow dr
    =ht.FieldMap.Rows[i];
                    
    //map+=dr["columnname"].ToString ()  + "," + dr["ColumnDesc"].ToString () + "|"; 
                    if (!string.IsNullOrEmpty(dr["columnname"].ToString()) && !string.IsNullOrEmpty(dr["ColumnDesc"].ToString()))
                    {
                        dic.Add (dr[
    "columnname"].ToString (),dr["ColumnDesc"].ToString ());
                    }
                    
                }
                StringBuilder sbd 
    = new StringBuilder();
                sbd.AppendLine(
    "<table width=\"" + ht.Width + "\" name=\"mytable\" cellspacing=\"0\" >");

                
    #region 主体
                
    if (ht.Type == "0")
                {
                    
    if (ht.Data.Rows.Count == 0)
                        
    return "";

                    
    //生成标题
                    sbd.AppendLine("<tr>");
                    sbd.AppendLine(
    "<th scope=\"row\" class=\"title\" colspan=2>" + ht.Title + "</th>");
                    sbd.AppendLine(
    "</tr>");

                    
    //循环字段
                    DataRow dr = ht.Data.Rows[0];
                    
    for (int i = 0;i< ht.Data.Columns.Count; i++)
                    {
                        
    string colName = ht.Data.Columns[i].ColumnName;
                        
    string colValue = dr[colName].ToString();
                        
    string colMapValue = "";
                        
    if (dic.Keys.Contains (colName) )
                        {
                            colMapValue 
    = dic[colName];
                        }
                        
    else
                        {
                            colMapValue 
    = colName;
                        }
                        
    ////先判断是否要隐藏
                        //if (ht.FieldHide.IndexOf(colName.ToLower()) >= 0 || ht.FieldHide.IndexOf(colMapValue.ToLower()) >= 0)
                        
    //{
                        
    //    continue;
                        
    //}
                        
    //生成一行
                        sbd.AppendLine("<tr>");
                        sbd.AppendLine(
    "<th scope=\"row\" width=100  class=\"specalt\">" + colMapValue + "</th>");
                        sbd.AppendLine(
    "<td class=\"alt\" >" + colValue  +  "</td>");                    
                        sbd.AppendLine(
    "</tr>");

                    }
                    
                    

                }
                
    #endregion 

                
    #region 明细
                
    else if (ht.Type == "1")
                {
     

                    
    //判断有多少列
                    int cols = ht.Data.Columns.Count;

                    
    //生成标题
                    sbd.AppendLine("<tr>");
                    sbd.AppendLine(
    "<th scope=\"row\"  class=\"title\" colspan=" + cols + ">" + ht.Title + "</th>");
                    sbd.AppendLine(
    "</tr>");

                    
    //生成列表头
                    sbd.AppendLine("<tr>");
                    
    for (int i = 0; i < cols; i++)
                    {
                        
    string colName = ht.Data.Columns[i].ColumnName;
                        
    string colMapValue = "";
                        
    if (dic.Keys.Contains(colName))
                        {
                            colMapValue 
    = dic[colName];
                        }
                        
    else
                        {
                            colMapValue 
    = colName;
                        }
                        sbd.AppendLine(
    "<th scope=\"col\"  class=\"specalt\" >" + colMapValue + "</th>");                    
                    }
                    sbd.AppendLine(
    "</tr>");

                    
    //生成数据                
                    for (int i = 0; i < ht.Data.Rows.Count; i++)
                    {
                        DataRow dr 
    = ht.Data.Rows[i];

       
                        
    //生成一行
                        sbd.AppendLine("<tr>");
                        
    for (int j = 0; j < cols; j++)
                        {
                            
    string colValue = dr[j].ToString();                        
                            sbd.AppendLine(
    "<td class=\"alt\" >" + colValue + "</td>");

                        }
                        sbd.AppendLine(
    "</tr>");
                    }

        
                }
                
    #endregion


                sbd.AppendLine(
    "</table>");
                
    return sbd.ToString ();
            }
            
            
            
    /// <summary>返回多个表格
            
    ///  
            
    /// </summary>
            
    /// <param name="htl"></param>        
            
    /// <returns></returns>
            public static string GetHtmlTable(List< htmlTable> htl)
            {
                
    if (htl.Count == 0return "0";
                StringBuilder sb 
    = new StringBuilder();
                
    for (int i = 0; i < htl.Count; i++)
                {
                    sb.AppendLine(GetHtmlTable(htl[i]));
                }
                
    return sb.ToString();
            }
            
    public static string GetHtmlTable(DataSet ds,string type)
            {
                List
    <htmlTable> htl = new List<htmlTable>();
                
    int count=ds.Tables.Count ;
                
    string[] t = type.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
                
    for (int i = 0; i < t.Length; i++)
                {
                    
    string[] p = t[i].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    
    if (p.Length != 4)
                        
    continue;
                    htmlTable ht 
    = new htmlTable();
                    ht.Type 
    = p[0];
                    
    int t1=Convert.ToInt32(p[1]);
                    
    int t2=Convert.ToInt32(p[2]);
                    
    if (t1 > count || t2 > count)
                        
    continue;
                    ht.Data 
    = ds.Tables[t1];
                    ht.FieldMap 
    = ds.Tables[t2];
                    ht.Title 
    = p[3];
                    htl.Add(ht);

                }

                
    return GetHtmlTable(htl);
            }

     调用:

    tb = CommonInfo.GetHtmlTable(ds, "0,0,1,运输信息|0,2,3,派车信息");

    样式表:

     
    /* CSS Document */
    #mytable 
    {
    padding
    : 0;
    margin
    : 0;
    }

    th.title 
    {
    font
    : bold 16px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    color
    : #4f6b72;
    border-right
    : 1px solid #C1DAD7;
    border-bottom
    : 1px solid #C1DAD7;
    border-top
    : 1px solid #C1DAD7;
    border-left
    : 0px;
    letter-spacing
    : 2px;
    text-transform
    : uppercase;
    text-align
    : left;
    padding
    : 6px 6px 6px 12px;
    background
    : #f5fada url(bg_heade1r.jpg) no-repeat;
    }


    td.alt 
    {
    border-right
    : 1px solid #C1DAD7;
    border-bottom
    : 1px solid #C1DAD7;
    font-size
    :12px;
    background
    :#fff;
    color
    : #797268;
    padding
    : 6px 6px 6px 12px;
    }

    th.specalt 
    {
    border-right
    : 1px solid #C1DAD7;
    border-bottom
    : 1px solid #C1DAD7;
    border-top
    : 0px solid #C1DAD7;
    border-left
    : 1px solid #C1DAD7;
     letter-spacing
    : 0px;
    text-transform
    : uppercase;
    text-align
    : left;
    padding
    : 6px 6px 6px 12px;
    background
    : #f5fafa ;
    font
    : 12px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
    color
    : #797268;
    }
    /*---------for IE 5.x bug* #F5FADA/
    html>body td{ font-size:11px;}
     
  • 相关阅读:
    android progressbar 水平进度条
    jquery 下拉自动加载
    jquery ajax
    input 数字,字母汉字的限制方法(转帖)
    Jquery checkbox
    js运用6
    js运用5
    js运用4
    js运用3
    js运用2
  • 原文地址:https://www.cnblogs.com/szyicol/p/1788707.html
Copyright © 2011-2022 走看看