2 // System.Web.UI.WebControls.HyperLinkField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.Collections.Specialized;
35 using System.ComponentModel;
36 using System.Security.Permissions;
37 using System.Reflection;
39 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public class HyperLinkField : DataControlField
45 PropertyDescriptor textProperty;
46 PropertyDescriptor[] urlProperties;
47 static string[] emptyFields;
49 public override bool Initialize (bool sortingEnabled, Control control)
51 return base.Initialize (sortingEnabled, control);
54 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
55 [TypeConverterAttribute (typeof(StringArrayConverter))]
56 [WebCategoryAttribute ("Data")]
57 [DefaultValueAttribute (null)]
58 public virtual string[] DataNavigateUrlFields {
60 object ob = ViewState ["DataNavigateUrlFields"];
61 if (ob != null) return (string[]) ob;
62 if (emptyFields == null) emptyFields = new string[0];
66 ViewState ["DataNavigateUrlFields"] = value;
71 [DefaultValueAttribute ("")]
72 [WebCategoryAttribute ("Data")]
73 public virtual string DataNavigateUrlFormatString {
75 object ob = ViewState ["DataNavigateUrlFormatString"];
76 if (ob != null) return (string) ob;
80 ViewState ["DataNavigateUrlFormatString"] = value;
85 [WebCategoryAttribute ("Data")]
86 [DefaultValueAttribute ("")]
87 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
88 public virtual string DataTextField {
90 object ob = ViewState ["DataTextField"];
91 if (ob != null) return (string) ob;
95 ViewState ["DataTextField"] = value;
100 [DefaultValueAttribute ("")]
101 [WebCategoryAttribute ("Data")]
102 public virtual string DataTextFormatString {
104 object ob = ViewState ["DataTextFormatString"];
105 if (ob != null) return (string) ob;
109 ViewState ["DataTextFormatString"] = value;
114 [DefaultValueAttribute ("")]
115 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
116 [UrlPropertyAttribute]
117 [WebCategoryAttribute ("Behavior")]
118 public virtual string NavigateUrl {
120 object ob = ViewState ["NavigateUrl"];
121 if (ob != null) return (string) ob;
125 ViewState ["NavigateUrl"] = value;
130 [DefaultValueAttribute ("")]
131 [WebCategoryAttribute ("Behavior")]
132 [TypeConverterAttribute (typeof(TargetConverter))]
133 public virtual string Target {
135 object ob = ViewState ["Target"];
136 if (ob != null) return (string) ob;
140 ViewState ["Target"] = value;
145 [LocalizableAttribute (true)]
146 [DefaultValueAttribute ("")]
147 [WebCategoryAttribute ("Appearance")]
148 public virtual string Text {
150 object ob = ViewState ["Text"];
151 if (ob != null) return (string) ob;
155 ViewState ["Text"] = value;
160 public override void InitializeCell (DataControlFieldCell cell,
161 DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
163 base.InitializeCell (cell, cellType, rowState, rowIndex);
164 if (cellType == DataControlCellType.DataCell) {
165 HyperLink link = new HyperLink ();
168 if (Target.Length > 0)
169 link.Target = Target;
171 if (DataTextField.Length > 0)
176 string [] fields = DataNavigateUrlFields;
177 if (fields.Length > 0)
180 link.NavigateUrl = NavigateUrl;
182 if (bind && cellType == DataControlCellType.DataCell && (rowState & DataControlRowState.Insert) == 0)
183 cell.DataBinding += new EventHandler (OnDataBindField);
185 link.ControlStyle.CopyFrom (ControlStyle);
187 cell.Controls.Add (link);
191 protected virtual string FormatDataNavigateUrlValue (object[] dataUrlValues)
193 if (dataUrlValues == null || dataUrlValues.Length == 0)
195 else if (DataNavigateUrlFormatString.Length > 0)
196 return string.Format (DataNavigateUrlFormatString, dataUrlValues);
198 return dataUrlValues[0].ToString ();
201 protected virtual string FormatDataTextValue (object dataTextValue)
203 if (DataTextFormatString.Length > 0)
204 return string.Format (DataTextFormatString, dataTextValue);
205 else if (dataTextValue == null)
208 return dataTextValue.ToString ();
211 void OnDataBindField (object sender, EventArgs e)
213 DataControlFieldCell cell = (DataControlFieldCell) sender;
214 HyperLink link = (HyperLink) cell.Controls [0];
215 object controlContainer = cell.BindingContainer;
216 object item = DataBinder.GetDataItem (controlContainer);
218 if (DataTextField.Length > 0) {
219 if (textProperty == null) SetupProperties (controlContainer);
220 link.Text = FormatDataTextValue (textProperty.GetValue (item));
223 string[] urlFields = DataNavigateUrlFields;
224 if (urlFields.Length > 0) {
225 if (urlProperties == null) SetupProperties (controlContainer);
226 object[] dataUrlValues = new object [urlFields.Length];
227 for (int n=0; n<dataUrlValues.Length; n++)
228 dataUrlValues [n] = urlProperties [n].GetValue (item);
229 link.NavigateUrl = FormatDataNavigateUrlValue (dataUrlValues);
233 void SetupProperties (object controlContainer)
235 object item = DataBinder.GetDataItem (controlContainer);
236 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (item);
238 if (DataTextField.Length > 0) {
239 textProperty = props [DataTextField];
240 if (textProperty == null)
241 new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + item.GetType());
244 string[] urlFields = DataNavigateUrlFields;
245 if (urlFields.Length > 0) {
246 urlProperties = new PropertyDescriptor [urlFields.Length];
247 for (int n=0; n<urlFields.Length; n++) {
248 PropertyDescriptor prop = props [urlFields [n]];
250 new InvalidOperationException ("Property '" + urlFields [n] + "' not found in object of type " + item.GetType());
251 urlProperties [n] = prop;
256 protected override DataControlField CreateField ()
258 return new HyperLinkField ();
261 protected override void CopyProperties (DataControlField newField)
263 base.CopyProperties (newField);
264 HyperLinkField field = (HyperLinkField) newField;
265 field.DataNavigateUrlFields = DataNavigateUrlFields;
266 field.DataNavigateUrlFormatString = DataNavigateUrlFormatString;
267 field.DataTextField = DataTextField;
268 field.DataTextFormatString = DataTextFormatString;
269 field.NavigateUrl = NavigateUrl;
270 field.Target = Target;
274 public override void ValidateSupportsCallback ()