• 后台自动生成功能强大的复杂控件


         在系统开发过程中,经常遇到复杂数据的绑定问题。平时的数据绑定,我们习惯了使用Table、Repeater、DataList、GridView对数据进行绑定,尽管这些控件功能强大,基本可以满足我们平时的绑定需求。但是这些控件进行绑定的时候需要确定一个模板(ItemTemplete),模板确定了,显示数据的样式也就确定了,灵活性不是很强。

         在我们从事开发的过程中,经常遇到一些复杂的数据绑定,如果采用以往的数据绑定方案,去寻找那样一个合适的模板并不容易,有的时候几乎是办不到的。针对这种情况,我采用了从后台生成复杂控件进行绑定。在页面放一个控件容器,用于存放后台生成的控件集合。
         在面向对象开发过程中,一定要清晰的认识到,一切皆为对象,所以一切控件也皆为对象。我们在后台进行操作,其实也就是操作控件对象,在显示页面显示也是这些控件对象。只要我们对这些控件对象进行有效的管理、合理的编排,对其属性进行深入的发掘,你就很容易的生成各种各样、功能强大的控件。

         下面是我做的一个问卷调查系统的统计页面,以这个作为例子,希望给朋友们带来一些帮助。

         后台具体代码:   

     private void BindData()
        
    {
            
    try
            
    {
                Label nLabel;
                DataTable nTable;
                DataRow sRow;
                DataRow tRow;
                DataRow nCell;

                
    //遍历所有的问题
                List<Question> qList = QuestionManager.GetInstance().getList(PaperID);
                List
    <Answer> aList;
                
    foreach (Question nQues in qList)
                
    {
                    nLabel 
    = new Label();
                    
    //样式设置
                    nLabel.Style.Add(HtmlTextWriterStyle.FontSize, "14pt");
                    nLabel.Style.Add(HtmlTextWriterStyle.Color, 
    "Red");
                    nLabel.Style.Add(HtmlTextWriterStyle.FontWeight, 
    "bold");
                    nLabel.Text 
    = nQues.Num.ToString() + "" + nQues.Title;
                    Panel1.Controls.Add(nLabel);
                    Panel1.Controls.Add(
    new LiteralControl("<br/>"));


                    nTable 
    = new DataTable();
                    sRow 
    = nTable.NewRow();
                    tRow 
    = nTable.NewRow();
                    nTable.Columns.Add(
    new DataColumn("选项"));
                    sRow[
    0= "小计";
                    tRow[
    0= "比例";

                    
    //统计数据
                    DataTable sTable = ReplyManager.GetInstance().GetGroupData(nQues.GlobalID);
                    
    //该问题所有回答人数
                    int count = ReplyManager.GetInstance().GetCount(nQues.GlobalID);
                    
    //该问题所有答案
                    aList = AnswerManager.GetInstance().getList(nQues.GlobalID);
                    
    for (int p = 0; p < aList.Count; p++)
                    
    {
                        nTable.Columns.Add(
    new DataColumn(SubString(aList[p].Title)));
                        sRow[p 
    + 1= "0";
                        tRow[p 
    + 1= "0.00%";
                        
    foreach (DataRow irow in sTable.Rows)
                        
    {
                            
    int tempCount = Bmc.CLUtility.getConvertIntValue(irow["Count"]);
                            
    double result = 0;
                            
    if (tempCount > 0)
                            
    {
                                result 
    = System.Math.Round((double)tempCount / count, 4);
                            }

                            
    if (Bmc.CLUtility.getConvertIntValue(irow["F_AnswerID"]) == aList[p].GlobalID)
                            
    {
                                sRow[p 
    + 1= tempCount.ToString();
                                tRow[p 
    + 1= result.ToString("p");
                            }

                        }

                    }

                    nTable.Columns.Add(
    new DataColumn("有效答卷数"));
                    
    int totalCells = nTable.Columns.Count;
                    sRow[totalCells 
    - 1= count.ToString();

                    nTable.Rows.Add(sRow);
                    nTable.Rows.Add(tRow);
                    GridView grid 
    = new GridView();
                    grid.DataSource 
    = nTable.DefaultView;
                    grid.DataBind();
                    
    //样式设置
                    grid.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
                    grid.Style.Add(HtmlTextWriterStyle.BorderColor, 
    "blue");
                    grid.RowStyle.BorderColor 
    = System.Drawing.Color.Blue;
                    grid.ControlStyle.BorderColor 
    = System.Drawing.Color.Blue;
                    Panel1.Controls.Add(grid);
                    Panel1.Controls.Add(
    new LiteralControl("<br/>"));
                }

            }

            
    catch (Exception ex)
            
    {
                Bmc.CLUtility.ShowMessage(
    this.Page, "绑定出错,错误信息:" + ex.ToString());
            }

        }

         页面显示如下:

    还有一个生成问题的页面。这个还真够看出些复杂 

    Code

    效果图:

     

  • 相关阅读:
    每周总结
    5月2日学习日志
    5月1日学习日志
    4月30日学习日志
    4月29日学习日志
    4月28日学习日志
    4月27日学习日志
    每周总结
    vue滚动插件BetterScroll
    vue 获取页面高度
  • 原文地址:https://www.cnblogs.com/yank/p/1257262.html
Copyright © 2020-2023  润新知