Beispiel #1
0
        //-----------------------------  Local Events --------------------------------------//
        //Build the grid
        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {
            //The IStateManager is not used, therefor the grid has to be
            //build again in case of a PostBack (therefor the dataBinding property
            //is not checked)

            //Disbale viewstate, otherwise the datasource/binding is lost after a
            //postback and I have not implemented the viewstate (alle element  are lost)
            this.EnableViewState = false;

            int count = 0;  //Count the controls (return value)

            if (dataSource == null)
            {
                //No records found
                Label lb = new Label();
                lb.Text = _noRecordsText;
                if (_emptyGridCssClass != null) lb.CssClass = _emptyGridCssClass;
                Controls.Add(lb);
                count++;
            }
            else
            {
                //Records were found, build the grid
                Table tb = new Table();
                tb.CellPadding = 0;
                tb.CellSpacing = 0;
                if (_cssClass != null) tb.CssClass = CssClass;

                //Build header -----------------------------------------------------------
                TableHeaderRow thr = new TableHeaderRow();
                thr.TableSection = TableRowSection.TableHeader;
                TableHeaderCell thc;

                foreach (GridField gf in _gridFields)
                {
                    //A header cell for each field
                    thc = new TableHeaderCell();
                    if (gf.CssClass != null) thc.CssClass = gf.CssClass;
                    thc.Attributes["scope"] = "col";
                    if (gf.HeaderText != null) thc.Text = gf.HeaderText;
                    thr.Cells.Add(thc);
                }

                //Special columns
                if (_showDeleteColumn)
                {
                    thc = new TableHeaderCell();
                    thc.Text = "Delete";
                    thc.Style["text-align"] = "center";
                    thr.Cells.Add(thc);
                }

                if (_showEditColumn)
                {
                    thc = new TableHeaderCell();
                    thc.Text = "Edit";
                    thc.Style["text-align"] = "center";
                    thr.Cells.Add(thc);
                }

                if (_showOrderColumn)
                {
                    thc = new TableHeaderCell();
                    thc.Text = "Order";
                    thc.Style["text-align"] = "center";
                    thr.Cells.Add(thc);
                }

                tb.Rows.Add(thr);

                //Body -----------------------------------------------------------------------
                TableRow tr;
                TableCell tc;
                Image img;
                string rowData = "";
                string rowCancel = "";  //cancel row action (delete)

                //Build each new row in WITGridRow (instead of directly adding it
                //to the table's row). Trigger the event RowDataBound to enable
                //the programmer to change or read the information before the grid is build.
                //Then build the grid.

                int row = 0;
                foreach (DataRowView dataitem in dataSource)
                {
                    //New custom row for RowDataBound-event
                    WITGridRow wgr = new WITGridRow(dataitem, row);

                    //Build per (data)row

                    tr = new TableRow();
                    tr.TableSection = TableRowSection.TableBody;
                    if (_highLightSelectedRow)
                    {
                        tr.Attributes["onmouseover"] = "this.className='" + _highlightCssClass + "';";
                        tr.Attributes["onmouseout"] = "this.className=''";
                        rowData = "row_" + row.ToString();
                        if (_dataKeyName != "") rowData += "_" + dataitem[_dataKeyName].ToString();
                        tr.Attributes["onclick"] = "javascript:" + this.Page.ClientScript.GetPostBackEventReference(this, rowData);
                    }

                    //Regular items
                    foreach (GridField gf in _gridFields)
                    {
                        //Build per column (data field)
                        tc = new TableCell();
                        count++;
                        tc.CssClass = gf.CssClass;

                        if (gf is CheckBoxField)  //Put checkbox before bound because checkbox is bound
                        {
                            CheckBoxField cbf = (CheckBoxField)gf;
                            if (cbf.DataField == null) throw (new NullReferenceException("The data field for '" + cbf.ID + "' is not defined."));
                            CheckBox cb = new CheckBox();
                            cb.Enabled = false;
                            cb.Checked = Convert.ToBoolean(((DataRowView)dataitem).Row[cbf.DataField]);
                            cb.CssClass = _checkboxCssClass;
                            cb.ID = cbf.ID;
                            cb.Text = "";
                            tc.Style["text-align"] = "center";
                            tb.EnableViewState = false;
                            tc.Controls.Add(cb);
                        }
                        else if (gf is BoundField)
                        {
                            BoundField bf = (BoundField)gf;
                            if (bf.DataField == null) throw (new NullReferenceException("The data field for '" + bf.ID + "' is not defined."));
                            tc.ID = gf.ID;
                            tc.Text = ((DataRowView)dataitem).Row[bf.DataField].ToString();
                        }
                        wgr.Cells.Add(tc);
                    }

                    //Special columns

                    if (_showDeleteColumn)
                    {
                        tc = new TableCell();
                        count++;
                        tc.Width = 50;
                        tc.Style["text-align"] = "center";
                        img = new Image();
                        img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BCC.Controls.CollectionGrid.Resources.delete.gif");
                        rowData = "delete_" + row.ToString();
                        rowCancel = "cancel_" + row.ToString();  //to avoid a row click when the user cancels the delete
                        if (_dataKeyName != "") rowData += "_" + dataitem[_dataKeyName].ToString();
                        img.Attributes["onclick"] = "javascript:if (confirm('" + _confirmDeleteText + "')) { " + this.Page.ClientScript.GetPostBackEventReference(this, rowData) + " } " +
                                                                " else " + this.Page.ClientScript.GetPostBackEventReference(this, rowCancel);
                        tc.Controls.Add(img);
                        wgr.Cells.Add(tc);
                    }

                    if (_showEditColumn)
                    {
                        tc = new TableCell();
                        count++;
                        tc.Width = 30;
                        tc.Style["text-align"] = "center";
                        img = new Image();
                        img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BCC.Controls.CollectionGrid.Resources.pencil.gif");
                        rowData = "edit_" + row.ToString();
                        if (_dataKeyName != "") rowData += "_" + dataitem[_dataKeyName].ToString();
                        img.Attributes["onclick"] = "javascript:" + this.Page.ClientScript.GetPostBackEventReference(this, rowData);
                        tc.Controls.Add(img);
                        wgr.Cells.Add(tc);

                    }

                    if (_showOrderColumn)
                    {
                        //Up
                        tc = new TableCell();
                        count++;
                        tc.Width = 60;
                        tc.Style["text-align"] = "center";
                        img = new Image();
                        img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BCC.Controls.CollectionGrid.Resources.up.gif");
                        rowData = "up_" + row.ToString();
                        if (_dataKeyName != "") rowData += "_" + dataitem[_dataKeyName].ToString();
                        img.Attributes["onclick"] = "javascript:" + this.Page.ClientScript.GetPostBackEventReference(this, rowData);
                        tc.Controls.Add(img);
                        wgr.Cells.Add(tc);

                        //Down
                        img = new Image();
                        img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "BCC.Controls.CollectionGrid.Resources.down.gif");
                        rowData = "down_" + row.ToString();
                        if (_dataKeyName != "") rowData += "_" + dataitem[_dataKeyName].ToString();
                        img.Attributes["onclick"] = "javascript:" + this.Page.ClientScript.GetPostBackEventReference(this, rowData);
                        tc.Controls.Add(img);
                        wgr.Cells.Add(tc);
                    }

                    //Trigger OnRowDataBound
                    if (RowDataBound != null)
                    {
                        //RowDataBound(this, new GridViewRowEventArgs(gvr));
                        RowDataBound(this, new WITGridRowEventArgs(wgr));
                    }

                    //Add the (changed) information to the table.
                    while (wgr.Cells.Count > 0)
                    {
                        //Adding the cell will empty the cell collection
                        tr.Cells.Add(wgr.Cells[0]);
                    }
                    tb.Rows.Add(tr);

                    row++;

                }
                Controls.Add(tb);
            }
            return count;
        }
 //Constructor
 public WITGridRowEventArgs(WITGridRow row)
 {
     _row = row;
 }