| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- //
- // System.Web.UI.WebControls.BoundField.cs
- //
- // Authors:
- // Lluis Sanchez Gual ([email protected])
- //
- // (C) 2005 Novell, Inc (http://www.novell.com)
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // 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;
- using System.ComponentModel;
- using System.Security.Permissions;
- using System.Reflection;
- 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;
-
- [DefaultValueAttribute (true)]
- [WebSysDescription ("")]
- [WebCategoryAttribute ("Behavior")]
- public virtual bool ConvertEmptyStringToNull {
- get {
- object ob = ViewState ["ConvertEmptyStringToNull"];
- if (ob != null) return (bool) ob;
- return true;
- }
- set {
- ViewState ["ConvertEmptyStringToNull"] = value;
- OnFieldChanged ();
- }
- }
- [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 "";
- }
- set {
- ViewState ["DataField"] = value;
- OnFieldChanged ();
- }
- }
- [DefaultValueAttribute ("")]
- [WebSysDescription ("")]
- [WebCategoryAttribute ("Data")]
- public virtual string DataFormatString {
- get {
- object ob = ViewState ["DataFormatString"];
- if (ob != null) return (string) ob;
- return "";
- }
- set {
- ViewState ["DataFormatString"] = value;
- OnFieldChanged ();
- }
- }
- [DefaultValueAttribute ("")]
- [WebCategoryAttribute ("Behavior")]
- public virtual string NullDisplayText {
- get {
- object ob = ViewState ["NullDisplayText"];
- if (ob != null) return (string) ob;
- return "";
- }
- set {
- ViewState ["NullDisplayText"] = value;
- OnFieldChanged ();
- }
- }
- [DefaultValueAttribute (false)]
- [WebSysDescription ("")]
- [WebCategoryAttribute ("Behavior")]
- public bool ReadOnly {
- get {
- object val = ViewState ["ReadOnly"];
- return val != null ? (bool) val : false;
- }
- set {
- ViewState ["ReadOnly"] = value;
- OnFieldChanged ();
- }
- }
- [DefaultValueAttribute (true)]
- [WebSysDescription ("")]
- [WebCategoryAttribute ("HtmlEncode")]
- public virtual bool HtmlEncode {
- get {
- object val = ViewState ["HtmlEncode"];
- return val != null ? (bool) val : true;
- }
- set {
- ViewState ["HtmlEncode"] = true;
- OnFieldChanged ();
- }
- }
-
- public override void ExtractValuesFromCell (IOrderedDictionary dictionary,
- DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
- {
- bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
- if (editable && !ReadOnly) {
- if (cell.Controls.Count > 0) {
- TextBox box = (TextBox) cell.Controls [0];
- dictionary [DataField] = box.Text;
- }
- } else if (includeReadOnly) {
- dictionary [DataField] = cell.Text;
- }
- }
-
- public override void InitializeCell (DataControlFieldCell cell,
- DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
- {
- base.InitializeCell (cell, cellType, rowState, rowIndex);
- if (cellType == DataControlCellType.DataCell) {
- InitializeDataCell (cell, rowState);
- if ((rowState & DataControlRowState.Insert) == 0)
- cell.DataBinding += new EventHandler (OnDataBindField);
- }
- }
-
- public virtual void InitializeDataCell (DataControlFieldCell cell, DataControlRowState rowState)
- {
- bool editable = (rowState & (DataControlRowState.Edit | DataControlRowState.Insert)) != 0;
- if (editable && !ReadOnly) {
- TextBox box = new TextBox ();
- cell.Controls.Add (box);
- }
- }
-
- protected virtual bool SupportsHtmlEncode {
- get { return true; }
- }
-
- 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);
- else
- res = value.ToString ();
-
- if (encode) return HttpUtility.HtmlEncode (res);
- else return res;
- }
-
- protected virtual object GetValue (Control controlContainer)
- {
- if (DesignMode)
- return GetDesignTimeValue ();
- else
- return GetBoundValue (controlContainer);
- }
-
- protected virtual object GetDesignTimeValue ()
- {
- return GetBoundValue (Control);
- }
-
- object GetBoundValue (Control controlContainer)
- {
- 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);
- }
- }
-
- protected virtual void OnDataBindField (object sender, EventArgs e)
- {
- DataControlFieldCell cell = (DataControlFieldCell) sender;
- if (cell.Controls.Count > 0) {
- TextBox box = (TextBox) cell.Controls [0];
- object val = GetValue (cell.BindingContainer);
- box.Text = val != null ? val.ToString() : "";
- }
- else
- cell.Text = FormatDataValue (GetValue (cell.BindingContainer), SupportsHtmlEncode && HtmlEncode);
- }
-
- 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;
- }
- }
- }
- #endif
|