Пример #1
0
        protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
        {
            int numRows = base.CreateChildControls(dataSource, dataBinding);

            //no data rows created, create empty table if enabled
            if (numRows == 0 && ShowWhenEmpty)
            {
                //create table
                Table table = new Table();
                table.ID = this.ID;

                //convert the exisiting columns into an array and initialize
                DataControlField[] fields = new DataControlField[this.Columns.Count];
                this.Columns.CopyTo(fields, 0);

                if (this.ShowHeader)
                {
                    //create a new header row
                    GridViewRow headerRow = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

                    this.InitializeRow(headerRow, fields);
                    table.Rows.Add(headerRow);
                }

                //create the empty row
                GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);

                TableCell cell = new TableCell();
                cell.ColumnSpan = this.Columns.Count;
                cell.Width      = Unit.Percentage(100);
                if (!String.IsNullOrEmpty(EmptyDataText))
                {
                    cell.Controls.Add(new LiteralControl(EmptyDataText));
                }

                if (this.EmptyDataTemplate != null)
                {
                    EmptyDataTemplate.InstantiateIn(cell);
                }

                emptyRow.Cells.Add(cell);
                table.Rows.Add(emptyRow);

                if (this.ShowFooter)
                {
                    //create footer row
                    GridViewRow footerRow = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

                    this.InitializeRow(footerRow, fields);
                    table.Rows.Add(footerRow);
                }

                this.Controls.Clear();
                this.Controls.Add(table);
            }

            return(numRows);
        }
Пример #2
0
        /// <summary>
        ///   GridView数据为空时呈现的样式
        /// </summary>
        /// <param name="writer"> </param>
        protected virtual void RenderEmptyContent(HtmlTextWriter writer)
        {
            Table t = new Table();           //创建一个HTML的Table

            t.GridLines       = GridLines;   //设置Table的线型与GridView相同
            t.BorderStyle     = BorderStyle; //设置边界风格与GridView相同
            t.BorderWidth     = 0;           //??
            t.CellPadding     = 1;
            t.CellSpacing     = 1;
            t.HorizontalAlign = HorizontalAlign; //设置水平对齐风格与GridView相同
            t.Width           = Width;           //设置Table宽度与GridView相同
            t.CopyBaseAttributes(this);
            t.BorderColor   = BorderColor;       //设置边界颜色与GridView相同
            t.EnableTheming = EnableTheming;     //设置主题是否生效与GridView相同
            t.ForeColor     = ForeColor;         //设置前景色与GridView相同
            t.SkinID        = SkinID;            //设置皮肤样式与GridView相同
            t.ToolTip       = ToolTip;           //设置提示信息与GridView相同
            t.Visible       = Visible;           //设置是否可见与GridView相同
            t.Font.CopyFrom(Font);               //设置字体对象与GridView相同
            //设置Table的层叠样式表
            t.CssClass = EmptyDataTableCssClass != "" ? EmptyDataTableCssClass : CssClass;
            TableRow row = new TableRow();            //新建一个行

            row.CssClass = EmptyDataTitleRowCssClass; //设置行的与层叠样式表
            row.Height   = 25;
            //将行添加到Table中
            t.Rows.Add(row);
            //根据GridView中的字段创建Table行的单元格
            foreach (DataControlField field in Columns)
            {
                if (field.Visible)                    //如果GridView中的该字段可见
                {
                    TableCell cell = new TableCell(); //创建单元格
                    cell.Text = field.HeaderText;     //设置单元格文本
                    row.Cells.Add(cell);              //将单元格添加到行中
                }
            }
            TableRow row2 = new TableRow();              //新建第二行

            row2.CssClass = EmptyDataContentRowCssClass; //设置行的与层叠样式表
            t.Rows.Add(row2);
            TableCell msgCell = new TableCell();

            if (EmptyDataTemplate != null) //如果GridView中设置了空模板
            {
                EmptyDataTemplate.InstantiateIn(msgCell);
            }
            else
            {
                msgCell.Text = EmptyDataText;                 //设置单元格文本为空数据提示
            }
            msgCell.HorizontalAlign = HorizontalAlign.Center; //设置单元格的水平对齐
            msgCell.ColumnSpan      = Columns.Count;          //设置单元格的列合并
            row2.Cells.Add(msgCell);
            t.RenderControl(writer);                          //将Table发送给HTML呈现流
        }
Пример #3
0
 protected override void PerformDataBinding(System.Collections.IEnumerable data)
 {
     base.PerformDataBinding(data);
     if (data == null && this.DataItem != null)
     {
         data = new object[] {
             this.DataItem
         }
     }
     ;
     else if (data == null)
     {
         return;
     }
     foreach (var i in data)
     {
         _dataItemContainer.DataItem = i;
         if (i == null && EmptyDataTemplate != null)
         {
             EmptyDataTemplate.InstantiateIn(_dataItemContainer);
         }
         else
         {
             ItemTemplate.InstantiateIn(_dataItemContainer);
         }
         _dataItemContainer.DataBind();
         if (i != null)
         {
             OrderedDictionary keyTable = this.KeyTable;
             if (keyTable != null)
             {
                 keyTable.Clear();
                 string[] dataKeyNamesInternal = this.DataKeyNamesInternal;
                 if (dataKeyNamesInternal != null && dataKeyNamesInternal.Length != 0)
                 {
                     foreach (string str in dataKeyNamesInternal)
                     {
                         object propertyValue = DataBinder.GetPropertyValue(i, str);
                         keyTable.Add(str, propertyValue);
                     }
                     _dataKey = new DataKey(keyTable);
                 }
             }
         }
     }
     this.ExtractRowValues(this.BoundFieldValues, false);
 }
Пример #4
0
 protected override void CreateChildControls()
 {
     if (Items == null || Items.Count() == 0)
     {
         if (EmptyDataTemplate != null)
         {
             EmptyDataTemplate.InstantiateIn(this);
         }
     }
     else if (GenerateMethod != null)
     {
         int i = 0;
         foreach (object n in Items)
         {
             Controls.Add(GenerateMethod(i++, n));
         }
     }
     ChildControlsCreated = true;
 }
Пример #5
0
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // Dummy control to which we parent all the data item controls
            Control containerControl = new Control();

            IEnumerable dataItems = ViewData.Eval(Name) as IEnumerable;
            bool        hasData   = false;

            if (dataItems != null)
            {
                int index = 0;
                foreach (object dataItem in dataItems)
                {
                    hasData = true;
                    RepeaterItem repeaterItem = new RepeaterItem(index, dataItem)
                    {
                        ViewData = new ViewDataDictionary(dataItem),
                    };
                    ItemTemplate.InstantiateIn(repeaterItem);
                    containerControl.Controls.Add(repeaterItem);

                    index++;
                }
            }

            if (!hasData)
            {
                // If there was no data, instantiate the EmptyDataTemplate
                Control emptyDataContainer = new Control();
                EmptyDataTemplate.InstantiateIn(emptyDataContainer);
                containerControl.Controls.Add(emptyDataContainer);
            }

            Controls.Add(containerControl);

            containerControl.DataBind();
        }
Пример #6
0
 protected override void CreateChildControls()
 {
     if (Items == null || Items.Count() == 0)
     {
         if (EmptyDataTemplate != null)
         {
             EmptyDataTemplate.InstantiateIn(this);
         }
     }
     else
     {
         Controls.Clear();
         foreach (var i in Items)
         {
             var r = new TableRow();
             foreach (var c in Columns)
             {
                 r.Controls.Add(c.RenderColumn(i));
             }
             Controls.Add(r);
         }
     }
     ChildControlsCreated = true;
 }
Пример #7
0
        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {
            int rows = base.CreateChildControls(dataSource, dataBinding);

            // no data rows created, create empty table if enabled
            if (rows == 0 && (ShowFooterWhenEmpty || ShowHeaderWhenEmpty))
            {
                // create the table
                Table table = CreateChildTable();

                Controls.Clear();
                Controls.Add(table);

                DataControlField[] fields;
                if (AutoGenerateColumns)
                {
                    var source = new PagedDataSource {
                        DataSource = dataSource
                    };

                    ICollection autoGeneratedColumns = CreateColumns(source, true);
                    fields = new DataControlField[autoGeneratedColumns.Count];
                    autoGeneratedColumns.CopyTo(fields, 0);
                }
                else
                {
                    fields = new DataControlField[Columns.Count];
                    Columns.CopyTo(fields, 0);
                }

                TableRowCollection newRows = table.Rows;
                if (ShowHeaderWhenEmpty)
                {
                    // create a new header row
                    _headerRow = CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
                    InitializeRow(_headerRow, fields, newRows);
                }

                // create the empty row
                GridViewRow             emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
                TableCell               cell     = new TableCell();
                List <DataControlField> f        = fields.Where(dataControlField => dataControlField.Visible).ToList();
                cell.ColumnSpan = f.Count;
                //cell.Width = Unit.Percentage(100);

                // respect the precedence order if both EmptyDataTemplate
                // and EmptyDataText are both supplied ...
                if (EmptyDataTemplate != null)
                {
                    EmptyDataTemplate.InstantiateIn(cell);
                }
                else if (!string.IsNullOrEmpty(EmptyDataText))
                {
                    cell.Controls.Add(new LiteralControl(EmptyDataText));
                }

                emptyRow.Cells.Add(cell);
                GridViewRowEventArgs e = new GridViewRowEventArgs(emptyRow);
                OnRowCreated(e);

                newRows.Add(emptyRow);

                emptyRow.DataBind();
                OnRowDataBound(e);
                emptyRow.DataItem = null;

                if (ShowFooterWhenEmpty && ShowFooter)
                {
                    // create footer row
                    _footerRow = CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);
                    InitializeRow(_footerRow, fields, newRows);
                    newRows.Remove(emptyRow);
                }
            }

            return(rows);
        }