X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem.Web%2FSystem.Web.UI.WebControls%2FBoundField.cs;h=8f067835b7533cb476d588ed2649d532d8e51654;hb=c1d81649cc1d16ee47bd6fb951e220d8aba6a1d0;hp=fff91dfa77770b529a0aff17da6dbcab360042b3;hpb=9bbfdd1a0e9bebb8fc8437529bc051a7568db4e4;p=mono.git diff --git a/mcs/class/System.Web/System.Web.UI.WebControls/BoundField.cs b/mcs/class/System.Web/System.Web.UI.WebControls/BoundField.cs index fff91dfa777..8f067835b75 100644 --- a/mcs/class/System.Web/System.Web.UI.WebControls/BoundField.cs +++ b/mcs/class/System.Web/System.Web.UI.WebControls/BoundField.cs @@ -4,7 +4,7 @@ // Authors: // Lluis Sanchez Gual (lluis@novell.com) // -// (C) 2005 Novell, Inc (http://www.novell.com) +// (C) 2005-2010 Novell, Inc (http://www.novell.com) // // @@ -28,7 +28,6 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // -#if NET_2_0 using System.Collections; using System.Collections.Specialized; using System.Web.UI; @@ -36,39 +35,41 @@ using System.ComponentModel; using System.Security.Permissions; using System.Reflection; -namespace System.Web.UI.WebControls { - +namespace System.Web.UI.WebControls +{ [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] public class BoundField : DataControlField { public static readonly string ThisExpression = "!"; - PropertyDescriptor boundProperty; + //PropertyDescriptor boundProperty; + + [DefaultValueAttribute (false)] + [WebSysDescription ("")] + [WebCategoryAttribute ("Behavior")] + public virtual bool ApplyFormatInEditMode { + get { return ViewState.GetBool ("ApplyFormatInEditMode", false); } + set { ViewState ["ApplyFormatInEditMode"] = value; } + } [DefaultValueAttribute (true)] + [WebSysDescription ("")] [WebCategoryAttribute ("Behavior")] public virtual bool ConvertEmptyStringToNull { - get { - object ob = ViewState ["ConvertEmptyStringToNull"]; - if (ob != null) return (bool) ob; - return true; - } + get { return ViewState.GetBool ("ConvertEmptyStringToNull", true); } set { ViewState ["ConvertEmptyStringToNull"] = value; OnFieldChanged (); } } - [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")] + [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)] + [WebSysDescription ("")] [WebCategoryAttribute ("Data")] [DefaultValueAttribute ("")] public virtual string DataField { - get { - object ob = ViewState ["DataField"]; - if (ob != null) return (string) ob; - return ""; - } + get { return ViewState.GetString ("DataField", String.Empty); } set { ViewState ["DataField"] = value; OnFieldChanged (); @@ -76,55 +77,63 @@ namespace System.Web.UI.WebControls { } [DefaultValueAttribute ("")] + [WebSysDescription ("")] [WebCategoryAttribute ("Data")] public virtual string DataFormatString { - get { - object ob = ViewState ["DataFormatString"]; - if (ob != null) return (string) ob; - return ""; - } + get { return ViewState.GetString ("DataFormatString", String.Empty); } set { ViewState ["DataFormatString"] = value; OnFieldChanged (); } } + [WebSysDescription ("")] + [WebCategoryAttribute ("Appearance")] + public override string HeaderText { + get { return ViewState.GetString ("HeaderText", String.Empty); } + set { + ViewState["HeaderText"] = value; + OnFieldChanged (); + } + } + [DefaultValueAttribute ("")] [WebCategoryAttribute ("Behavior")] public virtual string NullDisplayText { - get { - object ob = ViewState ["NullDisplayText"]; - if (ob != null) return (string) ob; - return ""; - } + get { return ViewState.GetString ("NullDisplayText", String.Empty); } set { ViewState ["NullDisplayText"] = value; OnFieldChanged (); } } - [WebCategoryAttribute ("Behavior")] [DefaultValueAttribute (false)] - public bool ReadOnly { - get { - object val = ViewState ["ReadOnly"]; - return val != null ? (bool) val : false; - } + [WebSysDescription ("")] + [WebCategoryAttribute ("Behavior")] + public virtual bool ReadOnly { + get { return ViewState.GetBool ("ReadOnly", false); } set { ViewState ["ReadOnly"] = value; OnFieldChanged (); } } - [WebCategoryAttribute ("HtmlEncode")] [DefaultValueAttribute (true)] + [WebSysDescription ("")] + [WebCategoryAttribute ("HtmlEncode")] public virtual bool HtmlEncode { - get { - object val = ViewState ["HtmlEncode"]; - return val != null ? (bool) val : true; - } + get { return ViewState.GetBool ("HtmlEncode", true); } set { - ViewState ["HtmlEncode"] = true; + ViewState ["HtmlEncode"] = value; + OnFieldChanged (); + } + } + + [DefaultValue (true)] + public virtual bool HtmlEncodeFormatString { + get { return ViewState.GetBool ("HtmlEncodeFormatString", true); } + set { + ViewState ["HtmlEncodeFormatString"] = value; OnFieldChanged (); } } @@ -132,33 +141,47 @@ namespace System.Web.UI.WebControls { public override void ExtractValuesFromCell (IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly) { - if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) { + bool editable = IsEditable (rowState); + if (editable) { if (cell.Controls.Count > 0) { - TextBox box = cell.Controls [0] as TextBox; + TextBox box = (TextBox) cell.Controls [0]; dictionary [DataField] = box.Text; } - } else if (includeReadOnly) { + } else if (includeReadOnly) dictionary [DataField] = cell.Text; - } } - + + public override bool Initialize (bool enableSorting, Control control) + { + return base.Initialize (enableSorting, control); + } + public override void InitializeCell (DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex) { base.InitializeCell (cell, cellType, rowState, rowIndex); - if (cellType == DataControlCellType.DataCell) + if (cellType == DataControlCellType.DataCell) { InitializeDataCell (cell, rowState); - cell.DataBinding += new EventHandler (OnDataBindField); + if ((rowState & DataControlRowState.Insert) == 0) + cell.DataBinding += new EventHandler (OnDataBindField); + } } - public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState) + protected virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState) { - if ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) { + bool editable = IsEditable (rowState); + if (editable) { TextBox box = new TextBox (); cell.Controls.Add (box); + box.ToolTip = HeaderText; } } - + + internal bool IsEditable (DataControlRowState rowState) + { + return ((rowState & DataControlRowState.Edit) != 0 && !ReadOnly) || ((rowState & DataControlRowState.Insert) != 0 && InsertVisible); + } + protected virtual bool SupportsHtmlEncode { get { return true; } } @@ -166,15 +189,29 @@ namespace System.Web.UI.WebControls { protected virtual string FormatDataValue (object value, bool encode) { string res; - if (value == null || (value.ToString().Length == 0 && ConvertEmptyStringToNull)) - res = NullDisplayText; - else if (DataFormatString.Length > 0) - res = string.Format (DataFormatString, value); + bool htmlEncodeFormatString = HtmlEncodeFormatString; + string stringValue = (value != null) ? value.ToString () : String.Empty; + if (value == null || (stringValue.Length == 0 && ConvertEmptyStringToNull)) { + if (NullDisplayText.Length == 0) { + encode = false; + res = " "; + } else + res = NullDisplayText; + } else { + string format = DataFormatString; + if (!String.IsNullOrEmpty (format)) { + if (!encode || htmlEncodeFormatString) + res = String.Format (format, value); + else + res = String.Format (format, encode ? HttpUtility.HtmlEncode (stringValue) : stringValue); + } else + res = stringValue; + } + + if (encode && htmlEncodeFormatString) + return HttpUtility.HtmlEncode (res); else - res = value.ToString (); - - if (encode) return HttpUtility.HtmlEncode (res); - else return res; + return res; } protected virtual object GetValue (Control controlContainer) @@ -187,35 +224,85 @@ namespace System.Web.UI.WebControls { protected virtual object GetDesignTimeValue () { - return GetBoundValue (Control); + return "Databound"; } - + object GetBoundValue (Control controlContainer) { + object dataItem = DataBinder.GetDataItem (controlContainer); + if (dataItem == null) + throw new HttpException ("A data item was not found in the container. The container must either implement IDataItemContainer, or have a property named DataItem."); + if (DataField == ThisExpression) - return controlContainer.ToString (); - else { - IDataItemContainer dic = (IDataItemContainer) controlContainer; - if (boundProperty == null) { - boundProperty = TypeDescriptor.GetProperties (dic.DataItem) [DataField]; - if (boundProperty == null) - new InvalidOperationException ("Property '" + DataField + "' not found in object of type " + dic.DataItem.GetType()); - } - return boundProperty.GetValue (dic.DataItem); - } + return dataItem; + else if (DataField == string.Empty) + return null; + + return DataBinder.GetPropertyValue (dataItem, DataField); + } + + protected override void LoadViewState (object state) + { + // Why override? + base.LoadViewState (state); } protected virtual void OnDataBindField (object sender, EventArgs e) { - DataControlFieldCell cell = (DataControlFieldCell) sender; - if (cell.Controls.Count > 0) { - TextBox box = cell.Controls [0] as TextBox; - object val = GetValue (cell.BindingContainer); - box.Text = val != null ? val.ToString() : ""; + Control container = (Control) sender; + Control controlContainer = container.BindingContainer; + if (!(controlContainer is INamingContainer)) + throw new HttpException ("A DataControlField must be within an INamingContainer."); + object val = GetValue (controlContainer); + TextBox box = sender as TextBox; + + if (box == null) { + var cell = sender as DataControlFieldCell; + if (cell != null) { + ControlCollection controls = cell.Controls; + int ccount = controls != null ? controls.Count : 0; + if (ccount == 1) + box = controls [0] as TextBox; + if (box == null) { + cell.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode); + return; + } + } } + + if (box == null) + throw new HttpException ("Bound field " + DataField + " contains a control that isn't a TextBox. Override OnDataBindField to inherit from BoundField and add different controls."); + if (ApplyFormatInEditMode) + box.Text = FormatDataValue (val, SupportsHtmlEncode && HtmlEncode); else - cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode); + box.Text = val != null ? val.ToString() : NullDisplayText; } + + protected override DataControlField CreateField () + { + return new BoundField (); + } + + protected override void CopyProperties (DataControlField newField) + { + base.CopyProperties (newField); + BoundField field = (BoundField) newField; + field.ConvertEmptyStringToNull = ConvertEmptyStringToNull; + field.DataField = DataField; + field.DataFormatString = DataFormatString; + field.NullDisplayText = NullDisplayText; + field.ReadOnly = ReadOnly; + field.HtmlEncode = HtmlEncode; + } + + // MSDN: The ValidateSupportsCallback method is a helper method used to determine + // whether the controls contained in a BoundField object support callbacks. + // This method has been implemented as an empty method (a method that does not contain + // any code) to indicate that callbacks are supported. + public override void ValidateSupportsCallback () + { + } + } } -#endif +