ASP.NET中用哪种方式表格化数据

ASP.NET中用哪种方式表格化数据

    在开发ASP.NET站点项目中,经常用表格化的方式显示数据。最常用的可能就是DataGrid绑定DataSet数据的方式。在做过的软件项目中,有3种表格化数据的典型处理方式。
    1、DataGrid绑定数据源。这种方式大家用的最多,但是DataGrid与ADO.NET完美的绑定方式,还是让人不爽。清一色的DataGrid风格很难适应不同项目的特殊风格,而且在DataGrid上做出的个性化处理也会非常的麻烦。
    2、用XML(数据)+XSL(样式单)。大家能理解,DataSet绑定到DataGrid的实现机理不过如此。如图所示,实现这样的一个表格,开发人员可以尽情的设计XSL的风格样式。

Example.gif

    3、直接将数据绘制到HTML。这个方式有些土,但有些时候却非常有效,先看实现的代码。下述代码是实现上图所示的表格。
 1 None.gif < table  style ="WIDTH: 100%; BORDER-COLLAPSE: collapse; HEIGHT: 10px" >
 2 None.gif                     < tr >
 3 None.gif                         < td  align ="center" >
 4 None.gif                             < TABLE  id ="tblContainer"  class ="MsoNormalTable"  style ="WIDTH: 380px; BORDER-COLLAPSE: collapse; HEIGHT: 10px"
 5 None.gif                                cellSpacing ="0"  cellPadding ="6"  border ="1"  runat ="server"  bordercolor ="#99cccc" >
 6 None.gif                                 < tr >
 7 None.gif                                     < td  colspan ="2"  align ="center" >
 8 None.gif                                         < P >< FONT  size ="3" >< STRONG >< FONT  face ="宋体" > 综合管理部人员职务 </ FONT ></ STRONG ></ FONT ></ P >
 9 None.gif                                     </ td >
10 None.gif                                 </ tr >
11 None.gif                                 < tr >
12 None.gif                                     < td  align ="center"  bgcolor ="#003399" >< FONT  size ="2"  color ="#ffffff" >< STRONG > 人员姓名 </ STRONG ></ FONT ></ td >
13 None.gif                                     < td  align ="center"  bgcolor ="#003399" >< FONT  size ="2"  color ="#ffffff" >< STRONG > 部门职务 </ STRONG ></ FONT ></ td >
14 None.gif                                 </ tr >
15 None.gif                             </ TABLE >
16 None.gif                         </ td >
17 None.gif                     </ tr >
18 None.gif                 </ table >

直接用ASP.NET WebControls的Add方法,将Label添加到HTML的Cell中。
  1 None.gif public   class  WebForm2 : System.Web.UI.Page
  2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
  3InBlock.gif    
  4InBlock.gif        struct PersonRole
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  6InBlock.gif            public string name;
  7InBlock.gif            public string role;
  8ExpandedSubBlockEnd.gif        }

  9InBlock.gif
 10InBlock.gif        protected System.Web.UI.HtmlControls.HtmlTable tblContainer;
 11InBlock.gif        public string strAuditItemID = "A899B637-AC47-42EB-9B61-A61C9C880DDC";
 12InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 13ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 14InBlock.gif            // 在此处放置用户代码以初始化页面
 15InBlock.gif            if(Request.QueryString["AuditItemID"!= null)
 16ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 17InBlock.gif                strAuditItemID =  Request.QueryString["AuditItemID"].ToString();
 18ExpandedSubBlockEnd.gif            }

 19InBlock.gif
 20InBlock.gif            GetTeamMember(strAuditItemID);
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif
 23ContractedSubBlock.gifExpandedSubBlockStart.gif        Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
 24InBlock.gif        override protected void OnInit(EventArgs e)
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26InBlock.gif            //
 27InBlock.gif            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 28InBlock.gif            //
 29InBlock.gif            InitializeComponent();
 30InBlock.gif            base.OnInit(e);
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif        
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 34InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 35InBlock.gif        /// 此方法的内容。
 36ExpandedSubBlockEnd.gif        /// </summary>

 37InBlock.gif        private void InitializeComponent()
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 39InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 40InBlock.gif
 41ExpandedSubBlockEnd.gif        }

 42ExpandedSubBlockEnd.gif        #endregion

 43InBlock.gif
 44InBlock.gif        private void GetTeamMember(string AuditItemID)
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            string strMaster, strTeamLeader, strPM;
 47InBlock.gif            ArrayList al = GetTeamMemberName(AuditItemID, out strMaster, out strTeamLeader, out strPM);
 48InBlock.gif
 49InBlock.gif            foreach(PersonRole pr in al)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 51InBlock.gif                HtmlTableCell cell=new HtmlTableCell();    
 52InBlock.gif                cell.Align = "Center";
 53InBlock.gif                Label lbl = new Label();
 54InBlock.gif                lbl.Text = pr.name;
 55InBlock.gif                lbl.Font.Size = 9;
 56InBlock.gif
 57InBlock.gif                cell.Controls.Add(lbl); 
 58InBlock.gif                HtmlTableRow row=new HtmlTableRow();
 59InBlock.gif                row.Cells.Add(cell);
 60InBlock.gif
 61InBlock.gif                HtmlTableCell cellRole = new HtmlTableCell();
 62InBlock.gif                cellRole.Align = "Center";
 63InBlock.gif                Label lblRole = new Label();
 64InBlock.gif                lblRole.Text = pr.role;
 65InBlock.gif                lblRole.Font.Size = 9;
 66InBlock.gif
 67InBlock.gif                cellRole.Controls.Add(lblRole);
 68InBlock.gif                row.Cells.Add(cellRole);
 69InBlock.gif
 70InBlock.gif                tblContainer.Rows.Add(row);                        
 71ExpandedSubBlockEnd.gif            }

 72ExpandedSubBlockEnd.gif        }

 73InBlock.gif
 74InBlock.gif        private ArrayList GetTeamMemberName(string AuditItemID, out string strMasterName, out string strTeamLeader,out string strPM)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            ArrayList al = new ArrayList();
 77InBlock.gif            strMasterName = "";
 78InBlock.gif            strTeamLeader = "";
 79InBlock.gif            strPM = "";
 80InBlock.gif            
 81InBlock.gif            PersonRole pr;
 82InBlock.gif            pr.name = "张三";
 83InBlock.gif            pr.role = "总经理";
 84InBlock.gif            al.Add(pr);
 85InBlock.gif            
 86InBlock.gif            pr.name = "李四";
 87InBlock.gif            pr.role = "副总经理";
 88InBlock.gif            al.Add(pr);
 89InBlock.gif
 90InBlock.gif            pr.name = "王五";
 91InBlock.gif            pr.role = "科员";
 92InBlock.gif            al.Add(pr);
 93InBlock.gif
 94InBlock.gif            pr.name = "赵六";
 95InBlock.gif            pr.role = "科员";
 96InBlock.gif            al.Add(pr);
 97InBlock.gif
 98InBlock.gif            return al;
 99ExpandedSubBlockEnd.gif        }

100ExpandedBlockEnd.gif    }

    在绘制页面的时候,用哪种方式应该是仁者见仁、智者见智。在站点开发中,这3种方式都有典型的应用,特别是第三种,我发现在解决部分页面处理的性能问题中应用的非常有效。况且可以自动化的Layout页面控件,个性化处理比DataGrid中重写Render更加容易。
   

转载于:https://www.cnblogs.com/firmwolf/archive/2006/01/05/311341.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值