Пример #1
0
        protected override void OnDataBound(EventArgs e)
        {
            base.OnDataBound(e);
            if (!this.DesignMode)
            {
                if (this.CurrentMode == FormViewMode.Insert)
                {
                    #region Default和CarryOn赋值
                    bool crRowExist = false;
                    bool dvExist = false;
                    object[,] carValues = null;
                    object[,] defaultValues = null;
                    WebDefault def = null;
                    bool asExist = false;
                    string autoseqvalue = "";
                    string autoseqfield = "";
                    Hashtable tableautoseq = new Hashtable();
                    WebAutoSeq aus = new WebAutoSeq();
                    if (this.Page.Form != null)
                    {
                        def = (WebDefault)this.ExtendedFindChildControl(this.DataSourceID, FindControlType.DataSourceID, typeof(WebDefault));
                        List<object> preKeys = (List<object>)this.ViewState["PreAddKeys"];
                        List<object> preValues = (List<object>)this.ViewState["PreAddValues"];
                        if (def != null)
                        {
                            if (def.CarryOnActive && preKeys != null && preValues != null && preKeys.Count != 0 && preValues.Count != 0)
                            {
                                int i = preKeys.Count;
                                carValues = new object[i, 2];
                                for (int j = 0; j < i; j++)
                                {
                                    carValues[j, 0] = preKeys[j];
                                    carValues[j, 1] = preValues[j];
                                }
                                crRowExist = true;
                            }
                            if (def.DefaultActive)
                            {
                                defaultValues = def.GetDefaultValues();
                                dvExist = true;
                            }
                        }
                        //autoseq add by ccm
                        foreach (Control ctrl in this.Page.Form.Controls)
                        {
                            if (ctrl is WebAutoSeq && ((WebAutoSeq)ctrl).DataSourceID == this.DataSourceID && ((WebAutoSeq)ctrl).Active)
                            {
                                aus = (WebAutoSeq)ctrl;
                                autoseqvalue = aus.GetValue();
                                autoseqfield = aus.FieldName;
                                if (!tableautoseq.ContainsKey(autoseqfield))
                                {
                                    tableautoseq.Add(autoseqfield, autoseqvalue);
                                }
                                asExist = true;
                            }
                        }
                        //end add
                    }
                    #endregion

                    AllCtrls.Clear();
                    GetAllCtrls(this.Controls);
                    foreach (FormViewField field in this.Fields)
                    {
                        object value = null;
                        if (dvExist)
                        {
                            int i = defaultValues.Length / 2;
                            for (int j = 0; j < i; j++)
                            {
                                // 比较FieldName和需要default的FieldName,如果相同则将此Field的default值赋给value变量
                                if (field.FieldName == defaultValues[j, 0].ToString() && defaultValues[j, 1] != null && defaultValues[j, 1].ToString() != "")
                                {
                                    value = defaultValues[j, 1];
                                }
                            }
                        }
                        if (crRowExist)
                        {
                            int i = carValues.Length / 2;
                            for (int j = 0; j < i; j++)
                            {
                                if (def.Fields[field.FieldName] != null &&
                                    ((DefaultFieldItem)def.Fields[field.FieldName]).CarryOn &&
                                    field.FieldName == carValues[j, 0].ToString())
                                {
                                    value = carValues[j, 1];
                                }
                            }
                        }
                        if (asExist)
                        {
                            if (tableautoseq.ContainsKey(field.FieldName))
                            {
                                value = tableautoseq[field.FieldName];
                            }
                            //if (field.FieldName == autoseqfield)
                            //{
                            //    value = autoseqvalue;
                            //}
                        }
                        // 带key值给detail

                        if (this.Page != null && this.Page.Form != null)
                        {
                            WebDataSource datasource = this.GetObjByID(this.DataSourceID) as WebDataSource;
                            if (datasource != null && datasource.RelationValues.Contains(field.FieldName))
                            {
                                value = datasource.RelationValues[field.Name];
                            }
                        }

                        foreach (Control ctrl in AllCtrls)
                        {
                            if (ctrl.ID == field.ControlID && value != null)
                            {
                                if (ctrl is TextBox)
                                {
                                    ((TextBox)ctrl).Text = value.ToString();
                                }
                                if (ctrl is HiddenField)
                                {
                                    ((HiddenField)ctrl).Value = value.ToString();
                                }
                                if (ctrl is Label)
                                {
                                    ((Label)ctrl).Text = value.ToString();
                                }
                                if (ctrl is DropDownList)
                                {
                                    ((DropDownList)ctrl).SelectedValue = value.ToString();
                                }
                                if (ctrl is CheckBox)
                                {
                                    ((CheckBox)ctrl).Checked = Convert.ToBoolean(value);
                                    if (ctrl is WebCheckBox)
                                    {
                                        (ctrl as WebCheckBox).RefreshBindingValue();
                                    }
                                }
                                if (ctrl is WebRefValBase)
                                {
                                    ((WebRefValBase)ctrl).BindingValue = value.ToString();
                                }
                                if (ctrl is IDateTimePicker)
                                {
                                    if (((IDateTimePicker)ctrl).DateTimeType == dateTimeType.DateTime)
                                    {
                                        ((IDateTimePicker)ctrl).Text = value.ToString();
                                    }
                                    else if (((IDateTimePicker)ctrl).DateTimeType == dateTimeType.VarChar)
                                    {
                                        try
                                        {
                                            DateTime dt = Convert.ToDateTime(value);
                                            ((IDateTimePicker)ctrl).DateString = dt.ToString("yyyyMMdd");
                                        }
                                        catch
                                        {
                                            ((IDateTimePicker)ctrl).DateString = value.ToString();
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //WebSecColumns
                    List<Control> lWebSecColumns = (List<Control>)this.ExtendedFindChildControls(this.DataSourceID, FindControlType.DataSourceID, typeof(WebSecColumns));
                    //WebSecColumns webSecColumns = (WebSecColumns)this.ExtendedFindChildControl(this.DataSourceID, FindControlType.DataSourceID, typeof(WebSecColumns));
                    if (lWebSecColumns != null && lWebSecColumns.Count > 0)
                    {
                        foreach (var item in lWebSecColumns)
                        {
                            WebSecColumns webSecColumns = item as WebSecColumns;
                            foreach (WebSecControl secCtrl in webSecColumns.WebSecControls)
                            {
                                if (secCtrl.ControlParent == this.ID && secCtrl.ControlTemplateGroup == "InsertItemTemplate"
                                    && secCtrl.ControlTemplate == "InsertItemTemplate")
                                {
                                    Control sec = this.FindControl(secCtrl.ControlID);
                                    if (sec != null)
                                    {
                                        sec.Visible = webSecColumns.ColumnsVisible;
                                        Type type = sec.GetType();
                                        PropertyInfo proInfo = type.GetProperty("ReadOnly");
                                        if (proInfo != null)
                                        {
                                            proInfo.SetValue(sec, webSecColumns.ColumnsReadOnly, null);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (this.CurrentMode == FormViewMode.Edit)
                {
                    WebDataSource ds = this.GetObjByID(this.DataSourceID) as WebDataSource;
                    if (ds.AutoRecordLock)
                    {
                        object[] value = new object[ds.PrimaryKey.Length];
                        DataRow row = (this.DataItem as DataRowView).Row;
                        for (int i = 0; i < ds.PrimaryKey.Length; i++)
                        {
                            string columnName = ds.PrimaryKey[i].ColumnName;

                            if (row.Table.Columns.Contains(columnName))
                            {
                                value[i] = row[columnName];
                            }
                            else
                            {
                                throw new EEPException(EEPException.ExceptionType.ColumnValueNotFound, ds.GetType(), ds.ID, columnName, null);
                            }
                        }
                        if (!ds.AddLock("Updating", value))
                        {
                            if (OldPageIndex != -1)
                            {
                                this.PageIndex = OldPageIndex;
                                return;
                            }
                            else
                            {
                                this.ChangeMode(FormViewMode.ReadOnly);
                                WebNavigator nav = this.GetBindingNavigator();
                                if (nav != null)
                                {
                                    nav.SetState(WebNavigator.NavigatorState.Browsing);
                                    nav.SetNavState("Browsed");
                                }
                                return;
                            }
                        }
                    }

                    //WebSecColumns
                    List<Control> lWebSecColumns = (List<Control>)this.ExtendedFindChildControls(this.DataSourceID, FindControlType.DataSourceID, typeof(WebSecColumns));
                    //WebSecColumns webSecColumns = (WebSecColumns)this.ExtendedFindChildControl(this.DataSourceID, FindControlType.DataSourceID, typeof(WebSecColumns));
                    if (lWebSecColumns != null && lWebSecColumns.Count > 0)
                    {
                        foreach (var item in lWebSecColumns)
                        {
                            WebSecColumns webSecColumns = item as WebSecColumns;
                            foreach (WebSecControl secCtrl in webSecColumns.WebSecControls)
                            {
                                if (secCtrl.ControlParent == this.ID && secCtrl.ControlTemplateGroup == "EditItemTemplate"
                                    && secCtrl.ControlTemplate == "EditItemTemplate")
                                {
                                    Control sec = this.FindControl(secCtrl.ControlID);
                                    if (sec != null)
                                    {
                                        sec.Visible = webSecColumns.ColumnsVisible;
                                        Type type = sec.GetType();
                                        PropertyInfo proInfo = type.GetProperty("ReadOnly");
                                        if (proInfo != null)
                                        {
                                            proInfo.SetValue(sec, webSecColumns.ColumnsReadOnly, null);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else if (this.CurrentMode == FormViewMode.ReadOnly)
                {
                    OldPageIndex = -1;
                    //WebSecColumns
                    List<Control> lWebSecColumns = (List<Control>)this.ExtendedFindChildControls(this.DataSourceID, FindControlType.DataSourceID, typeof(WebSecColumns));
                    //WebSecColumns webSecColumns = (WebSecColumns)this.ExtendedFindChildControl(this.DataSourceID, FindControlType.DataSourceID, typeof(WebSecColumns));
                    if (lWebSecColumns != null && lWebSecColumns.Count > 0)
                    {
                        foreach (var item in lWebSecColumns)
                        {
                            WebSecColumns webSecColumns = item as WebSecColumns;
                            foreach (WebSecControl secCtrl in webSecColumns.WebSecControls)
                            {
                                if (secCtrl.ControlParent == this.ID && secCtrl.ControlTemplateGroup == "ItemTemplate"
                                    && secCtrl.ControlTemplate == "ItemTemplate")
                                {
                                    Control sec = this.FindControl(secCtrl.ControlID);
                                    if (sec != null)
                                    {
                                        sec.Visible = webSecColumns.ColumnsVisible;
                                        Type type = sec.GetType();
                                        PropertyInfo proInfo = type.GetProperty("ReadOnly");
                                        if (proInfo != null)
                                        {
                                            proInfo.SetValue(sec, webSecColumns.ColumnsReadOnly, null);
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
            if (InsertBack)
            {
                InsertBack = false;
                this.PageIndex = this.PageCount - 1;
                OnAfterInsertLocate(EventArgs.Empty);
            }
            object obj = this.GetObjByID(this.DataSourceID);
            if (obj != null && obj is WebDataSource)
            {
                WebDataSource wds = (WebDataSource)obj;
                if (wds.InnerDataSet != null)
                {
                    DataTable tab = wds.View.Table;
                    AllCtrls.Clear();
                    GetAllCtrls(this.Controls);
                    foreach (Control ctrl in AllCtrls)
                    {
                        if (ctrl is WebExpressionControl)
                        {
                            WebExpressionControl expCtrl = (WebExpressionControl)ctrl;
                            if (expCtrl.Expression != null && expCtrl.Expression != "")
                            {
                                foreach (DataColumn col in tab.Columns)
                                {
                                    if (expCtrl.Expression == col.ColumnName)
                                    {
                                        int i = this.DataItemIndex;
                                        for (int j = 0; j <= i; j++)
                                        {
                                            if (tab.Rows[j].RowState == DataRowState.Deleted)
                                            {
                                                i++;
                                            }
                                        }
                                        expCtrl.Text = tab.Rows[i][expCtrl.Expression].ToString();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #2
0
        protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            base.OnRowCreated(e);
            if (e.Row.RowType == DataControlRowType.Footer && this.ShowFooter)
            {
                #region 如果当前生成的Row是FooterRow
                int p = this.Columns.Count;
                DataControlField[] fields = new DataControlField[p];
                for (int q = 0; q < p; q++)
                {
                    fields[q] = this.Columns[q];
                }
                if (this.GridInserting)
                {
                    bool crRowExist = false;
                    bool dvExist = false;
                    object[,] carValues = null;
                    object[,] defaultValues = null;
                    WebDefault def = new WebDefault();
                    bool asExist = false;
                    string autoseqvalue = "";
                    string autoseqfield = "";
                    Hashtable tableautoseq = new Hashtable();
                    WebAutoSeq aus = new WebAutoSeq();
                    if (this.Page.Form != null)
                    {
                        def = (WebDefault)this.ExtendedFindChildControl(this.DataSourceID, FindControlType.DataSourceID, typeof(WebDefault));
                        // CarryOn
                        // 从ViewStates读出之前Insert的那一笔的Keys和Values,以便做CarryOn
                        List<object> preKeys = (List<object>)this.ViewState["PreAddKeys"];
                        List<object> preValues = (List<object>)this.ViewState["PreAddValues"];
                        if (def != null)
                        {
                            if (def.CarryOnActive && preKeys != null && preValues != null && preKeys.Count != 0 && preValues.Count != 0)
                            {
                                int i = preKeys.Count;
                                carValues = new object[i, 2];
                                for (int j = 0; j < i; j++)
                                {
                                    carValues[j, 0] = preKeys[j];
                                    carValues[j, 1] = preValues[j];
                                }
                                crRowExist = true;
                            }
                            // DefaultValue
                            if (def.DefaultActive)
                            {
                                defaultValues = def.GetDefaultValues();
                                dvExist = true;
                            }
                        }
                        //autoseq add by ccm
                        //
                        ControlCollection collection = (this.Page.Master == null) ? this.Page.Form.Controls : this.Parent.Controls;
                        foreach (Control ctrl in collection)
                        {
                            if (ctrl is WebAutoSeq && ((WebAutoSeq)ctrl).DataSourceID == this.DataSourceID && ((WebAutoSeq)ctrl).Active)
                            {
                                aus = (WebAutoSeq)ctrl;
                                autoseqvalue = aus.GetValue();
                                autoseqfield = aus.FieldName;
                                if (!tableautoseq.ContainsKey(autoseqfield))
                                {
                                    tableautoseq.Add(autoseqfield, autoseqvalue);
                                }
                                asExist = true;
                            }
                        }
                        //end add
                    }
                    #region Default和CarryOn赋值, 如果是detail,则带key值
                    int x = this.Columns.Count;
                    for (int y = 0; y < x; y++)
                    {
                        string FieldName = "";
                        // 如果當前Cell所在Column是CommandField或者Column的SortExpression和HeadText有一個為空時,跳岀本次循環
                        if (this.Columns[y] is CommandField
                            || ((this.Columns[y].SortExpression == null || this.Columns[y].SortExpression == "")
                            && (this.Columns[y].HeaderText == null || this.Columns[y].HeaderText == "")))
                        {
                            continue;
                        }
                        // 如果当前Cell所在Column是BoundField,那么FieldName为DataField属性
                        if (this.Columns[y] is BoundField)
                            FieldName = ((BoundField)this.Columns[y]).DataField;
                        // 否则FieldName为SortExpression属性
                        else
                            FieldName = this.Columns[y].SortExpression;
                        object value = null;

                        // DefaultValue
                        if (dvExist)
                        {
                            int m = defaultValues.Length / 2;
                            for (int n = 0; n < m; n++)
                            {
                                if (FieldName == defaultValues[n, 0].ToString()
                                    && defaultValues[n, 1] != null
                                    && defaultValues[n, 1].ToString() != "")
                                {
                                    value = defaultValues[n, 1];
                                }
                            }
                        }
                        // CarrayOn
                        if (crRowExist)
                        {
                            int m = carValues.Length / 2;
                            for (int n = 0; n < m; n++)
                            {
                                if (def.Fields[FieldName] != null &&
                                    ((DefaultFieldItem)def.Fields[FieldName]).CarryOn &&
                                    FieldName == carValues[n, 0].ToString())
                                {
                                    value = carValues[n, 1];
                                }
                            }
                        }
                        // AutoSeq
                        if (asExist)
                        {
                            if (tableautoseq.ContainsKey(FieldName))
                            {
                                value = tableautoseq[FieldName];
                            }
                        }
                        // 带key值给detail
                        if (this.Page != null && this.Page.Form != null)
                        {
                            WebDataSource datasource = this.GetObjByID(this.DataSourceID) as WebDataSource;
                            if (datasource != null && !string.IsNullOrEmpty(datasource.MasterDataSource)
                                && datasource.RelationValues.Contains(FieldName))
                            {
                                value = datasource.RelationValues[FieldName];
                            }
                        }
                        CellControls.Clear();
                        GetAllCellControls(e.Row.Cells[y]);
                        foreach (Control ctrl in CellControls)
                        {
                            if (value != null)
                            {
                                if (ctrl is TextBox)
                                {
                                    ((TextBox)ctrl).Text = value.ToString();
                                }
                                if (ctrl is HiddenField)
                                {
                                    ((HiddenField)ctrl).Value = value.ToString();
                                }
                                if (ctrl is Label)
                                {
                                    ((Label)ctrl).Text = value.ToString();
                                }
                                if (ctrl is DropDownList)
                                {
                                    ((DropDownList)ctrl).SelectedValue = value.ToString();
                                }
                                if (ctrl is CheckBox)
                                {
                                    ((CheckBox)ctrl).Checked = Convert.ToBoolean(value);
                                    if (ctrl is WebCheckBox)
                                    {
                                        (ctrl as WebCheckBox).RefreshBindingValue();
                                    }
                                }
                                if (ctrl is WebRefValBase)
                                {
                                    ((WebRefValBase)ctrl).BindingValue = value.ToString();
                                }
                                if (ctrl is IDateTimePicker)
                                {
                                    if (((IDateTimePicker)ctrl).DateTimeType == dateTimeType.DateTime)
                                    {
                                        ((IDateTimePicker)ctrl).Text = value.ToString();
                                    }
                                    else if (((IDateTimePicker)ctrl).DateTimeType == dateTimeType.VarChar)
                                    {
                                        try
                                        {
                                            DateTime dt = Convert.ToDateTime(value);
                                            ((IDateTimePicker)ctrl).DateString = dt.ToString("yyyyMMdd");
                                        }
                                        catch
                                        {
                                            ((IDateTimePicker)ctrl).DateString = value.ToString();
                                        }
                                    }
                                }
                                if (ctrl is Label)
                                {
                                    ((Label)ctrl).Text = value.ToString();
                                }
                            }
                        }
                    }
                    #endregion
                }
                else if (!this.GridInserting && this.TotalActive)
                {
                    int i = fields.Length;
                    for (int j = 0; j < i; j++)
                    {
                        string fieldName = "";
                        if (fields[j] is BoundField)
                        {
                            fieldName = ((BoundField)fields[j]).DataField;
                        }
                        else if (fields[j] is ExpressionField)
                        {
                            fieldName = ((ExpressionField)fields[j]).Expression;
                        }
                        else
                        {
                            fieldName = fields[j].SortExpression;
                        }
                        e.Row.Cells[j].Controls.Clear();
                        if (fieldName != "")
                        {
                            foreach (GridViewTotalItem item in this.TotalColumns)
                            {
                                if (item.FieldName == fieldName && item.ShowTotal)
                                {
                                    Label lbl = new Label();
                                    if (this.Site == null)
                                        lbl.Text = GetTotalValue(fieldName, item.TotalMode, item.Format);
                                    else
                                        lbl.Text = "Total";
                                    e.Row.Cells[j].Controls.Add(lbl);
                                }
                            }
                        }
                        else
                        {
                            if (j == 0)
                            {
                                Label lbl = new Label();
                                lbl.Text = this.TotalCaption;
                                e.Row.Cells[j].Controls.Add(lbl);
                            }
                        }
                    }
                }

                #endregion
            }
        }