2 // System.Web.UI.WebControls.HyperLinkField.cs
5 // Lluis Sanchez Gual (lluis@novell.com)
7 // (C) 2005-2010 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.
31 using System.Collections;
32 using System.Collections.Specialized;
34 using System.ComponentModel;
35 using System.Security.Permissions;
36 using System.Reflection;
38 namespace System.Web.UI.WebControls
40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 public class HyperLinkField : DataControlField
44 PropertyDescriptor textProperty;
45 PropertyDescriptor[] urlProperties;
46 static string[] emptyFields;
48 public override bool Initialize (bool sortingEnabled, Control control)
50 return base.Initialize (sortingEnabled, control);
53 [EditorAttribute ("System.Web.UI.Design.WebControls.DataFieldEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
54 [TypeConverterAttribute (typeof(StringArrayConverter))]
55 [WebCategoryAttribute ("Data")]
56 [DefaultValueAttribute (null)]
57 public virtual string[] DataNavigateUrlFields {
59 object ob = ViewState ["DataNavigateUrlFields"];
62 if (emptyFields == null)
63 emptyFields = new string[0];
67 ViewState ["DataNavigateUrlFields"] = value;
72 [DefaultValueAttribute ("")]
73 [WebCategoryAttribute ("Data")]
74 public virtual string DataNavigateUrlFormatString {
76 object ob = ViewState ["DataNavigateUrlFormatString"];
82 ViewState ["DataNavigateUrlFormatString"] = value;
87 [WebCategoryAttribute ("Data")]
88 [DefaultValueAttribute ("")]
89 [TypeConverterAttribute ("System.Web.UI.Design.DataSourceViewSchemaConverter, " + Consts.AssemblySystem_Design)]
90 public virtual string DataTextField {
92 object ob = ViewState ["DataTextField"];
98 ViewState ["DataTextField"] = value;
103 [DefaultValueAttribute ("")]
104 [WebCategoryAttribute ("Data")]
105 public virtual string DataTextFormatString {
107 object ob = ViewState ["DataTextFormatString"];
113 ViewState ["DataTextFormatString"] = value;
118 [DefaultValueAttribute ("")]
119 [EditorAttribute ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
120 [UrlPropertyAttribute]
121 [WebCategoryAttribute ("Behavior")]
122 public virtual string NavigateUrl {
124 object ob = ViewState ["NavigateUrl"];
130 ViewState ["NavigateUrl"] = value;
135 [DefaultValueAttribute ("")]
136 [WebCategoryAttribute ("Behavior")]
137 [TypeConverterAttribute (typeof(TargetConverter))]
138 public virtual string Target {
140 object ob = ViewState ["Target"];
146 ViewState ["Target"] = value;
151 [LocalizableAttribute (true)]
152 [DefaultValueAttribute ("")]
153 [WebCategoryAttribute ("Appearance")]
154 public virtual string Text {
156 object ob = ViewState ["Text"];
162 ViewState ["Text"] = value;
167 public override void InitializeCell (DataControlFieldCell cell,DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
169 base.InitializeCell (cell, cellType, rowState, rowIndex);
170 if (cellType == DataControlCellType.DataCell) {
171 HyperLink link = new HyperLink ();
174 if (Target.Length > 0)
175 link.Target = Target;
177 if (DataTextField.Length > 0)
182 string [] fields = DataNavigateUrlFields;
183 if (fields.Length > 0)
186 link.NavigateUrl = NavigateUrl;
188 if (bind && cellType == DataControlCellType.DataCell && (rowState & DataControlRowState.Insert) == 0)
189 cell.DataBinding += new EventHandler (OnDataBindField);
191 link.ControlStyle.CopyFrom (ControlStyle);
193 cell.Controls.Add (link);
197 protected virtual string FormatDataNavigateUrlValue (object[] dataUrlValues)
199 if (dataUrlValues == null || dataUrlValues.Length == 0)
201 else if (DataNavigateUrlFormatString.Length > 0)
202 return string.Format (DataNavigateUrlFormatString, dataUrlValues);
204 return dataUrlValues[0].ToString ();
207 protected virtual string FormatDataTextValue (object dataTextValue)
209 if (DataTextFormatString.Length > 0)
210 return string.Format (DataTextFormatString, dataTextValue);
211 else if (dataTextValue == null)
214 return dataTextValue.ToString ();
217 void OnDataBindField (object sender, EventArgs e)
219 DataControlFieldCell cell = (DataControlFieldCell) sender;
220 HyperLink link = (HyperLink) cell.Controls [0];
221 object controlContainer = cell.BindingContainer;
222 object item = DataBinder.GetDataItem (controlContainer);
224 if (DataTextField.Length > 0) {
225 if (textProperty == null) SetupProperties (controlContainer);
226 link.Text = FormatDataTextValue (textProperty.GetValue (item));
229 string[] urlFields = DataNavigateUrlFields;
230 if (urlFields.Length > 0) {
231 if (urlProperties == null) SetupProperties (controlContainer);
232 object[] dataUrlValues = new object [urlFields.Length];
233 for (int n=0; n<dataUrlValues.Length; n++)
234 dataUrlValues [n] = urlProperties [n].GetValue (item);
235 link.NavigateUrl = FormatDataNavigateUrlValue (dataUrlValues);
239 void SetupProperties (object controlContainer)
241 object item = DataBinder.GetDataItem (controlContainer);
242 PropertyDescriptorCollection props = TypeDescriptor.GetProperties (item);
244 if (DataTextField.Length > 0) {
245 textProperty = props.Find (DataTextField, true);
246 if (textProperty == null)
247 throw new InvalidOperationException ("Property '" + DataTextField + "' not found in object of type " + item.GetType());
250 string[] urlFields = DataNavigateUrlFields;
251 if (urlFields.Length > 0) {
252 urlProperties = new PropertyDescriptor [urlFields.Length];
253 for (int n=0; n<urlFields.Length; n++) {
254 PropertyDescriptor prop = props.Find (urlFields [n], true);
256 throw new InvalidOperationException ("Property '" + urlFields [n] + "' not found in object of type " + item.GetType());
257 urlProperties [n] = prop;
262 protected override DataControlField CreateField ()
264 return new HyperLinkField ();
267 protected override void CopyProperties (DataControlField newField)
269 base.CopyProperties (newField);
270 HyperLinkField field = (HyperLinkField) newField;
271 field.DataNavigateUrlFields = DataNavigateUrlFields;
272 field.DataNavigateUrlFormatString = DataNavigateUrlFormatString;
273 field.DataTextField = DataTextField;
274 field.DataTextFormatString = DataTextFormatString;
275 field.NavigateUrl = NavigateUrl;
276 field.Target = Target;
280 public override void ValidateSupportsCallback ()