2 // System.Web.UI.WebControls.HyperLinkColumn.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // (C) Gaurav Vaish (2002)
11 // (C) 2003 Andreas Nahr
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.ComponentModel;
\r
38 using System.Web.UI;
\r
40 namespace System.Web.UI.WebControls
\r
42 public class HyperLinkColumn: DataGridColumn
\r
44 PropertyDescriptor textFieldDescriptor;
\r
45 PropertyDescriptor urlFieldDescriptor;
\r
47 public HyperLinkColumn ()
\r
51 [DefaultValue (""), WebCategory ("Misc")]
52 [WebSysDescription ("The field that gets data-bound to the NavigateUrl.")]
\r
53 public virtual string DataNavigateUrlField {
\r
55 object o = ViewState ["DataNavigateUrlField"];
\r
58 return String.Empty;
\r
62 ViewState ["DataNavigateUrlField"] = value;
\r
67 // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
\r
69 [DefaultValue (""), WebCategory ("Misc")]
70 [Description ("The formatting rule for the text content that gets data-bound to the NavigateUrl.")]
\r
71 public virtual string DataNavigateUrlFormatString {
\r
73 object o = ViewState ["DataNavigateUrlFormatString"];
\r
76 return String.Empty;
\r
80 ViewState ["DataNavigateUrlFormatString"] = value;
\r
85 [DefaultValue (""), WebCategory ("Misc")]
86 [WebSysDescription ("The field that gets data-bound to the Text property.")]
\r
87 public virtual string DataTextField {
\r
89 object o = ViewState ["DataTextField"];
\r
92 return String.Empty;
\r
95 ViewState ["DataTextField"] = value;
\r
100 // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
102 [DefaultValue (""), WebCategory ("Misc")]
103 [Description ("The formatting rule for the text content that gets data-bound to the Text property.")]
\r
104 public virtual string DataTextFormatString {
\r
106 object o = ViewState ["DataTextFormatString"];
\r
109 return String.Empty;
\r
113 ViewState ["DataTextFormatString"] = value;
\r
114 OnColumnChanged ();
\r
118 [DefaultValue (""), WebCategory ("Misc")]
119 [WebSysDescription ("The URL that this hyperlink links to.")]
\r
120 public virtual string NavigateUrl {
\r
122 object o = ViewState ["NavigateUrl"];
\r
125 return String.Empty;
\r
129 ViewState ["NavigateUrl"] = value;
\r
130 OnColumnChanged ();
\r
134 [DefaultValue (""), WebCategory ("Misc")]
135 [WebSysDescription ("The target frame in which the NavigateUrl property should be opened.")]
\r
136 public virtual string Target {
\r
138 object o = ViewState ["Target"];
\r
141 return String.Empty;
\r
145 ViewState ["Target"] = value;
\r
146 OnColumnChanged ();
\r
150 [DefaultValue (""), WebCategory ("Misc")]
151 [WebSysDescription ("The Text for the hyperlink.")]
\r
152 public virtual string Text {
\r
154 object o = ViewState ["Text"];
\r
157 return String.Empty;
\r
161 ViewState ["Text"] = value;
\r
162 OnColumnChanged ();
\r
166 public override void Initialize ()
\r
168 textFieldDescriptor = null;
\r
169 urlFieldDescriptor = null;
\r
170 base.Initialize ();
\r
173 public override void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)
\r
175 base.InitializeCell (cell, columnIndex, itemType);
\r
177 if (itemType != ListItemType.Header && itemType != ListItemType.Footer) {
\r
178 HyperLink toDisplay = new HyperLink ();
\r
179 toDisplay.Text = Text;
\r
180 toDisplay.NavigateUrl = NavigateUrl;
\r
181 toDisplay.Target = Target;
\r
183 if (DataTextField.Length > 0 || DataNavigateUrlField.Length > 0)
\r
184 toDisplay.DataBinding += new EventHandler (OnDataBindHyperLinkColumn);
\r
186 cell.Controls.Add (toDisplay);
\r
190 private void OnDataBindHyperLinkColumn (object sender, EventArgs e)
192 HyperLink link = (HyperLink) sender;
193 object item = ((DataGridItem) link.NamingContainer).DataItem;
195 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (item);
196 if (textFieldDescriptor == null)
197 textFieldDescriptor = properties.Find (DataTextField, true);
199 if (urlFieldDescriptor == null)
200 urlFieldDescriptor = properties.Find (DataNavigateUrlField, true);
202 if (DataTextField.Length > 0 && textFieldDescriptor == null && !DesignMode)
203 throw new HttpException (HttpRuntime.FormatResourceString (
204 "Field_Not_Found", DataTextField));
206 if (DataNavigateUrlField.Length > 0 && urlFieldDescriptor == null && !DesignMode)
207 throw new HttpException (HttpRuntime.FormatResourceString (
208 "Field_Not_Found", DataNavigateUrlField));
210 if (textFieldDescriptor != null) {
211 link.Text = FormatDataTextValue (textFieldDescriptor.GetValue (item));
216 if (urlFieldDescriptor != null) {
217 link.NavigateUrl = FormatDataNavigateUrlValue (urlFieldDescriptor.GetValue (item));
221 if (DataNavigateUrlField.Length != 0 && DesignMode)
222 link.NavigateUrl = "url";
225 protected virtual string FormatDataNavigateUrlValue (object dataUrlValue)
\r
227 if (dataUrlValue == null)
\r
228 return String.Empty;
\r
231 if (DataNavigateUrlFormatString.Length > 0) {
\r
232 retVal = String.Format (DataNavigateUrlFormatString, dataUrlValue);
\r
234 retVal = dataUrlValue.ToString ();
\r
240 protected virtual string FormatDataTextValue (object dataTextValue)
\r
242 if (dataTextValue == null)
\r
243 return String.Empty;
\r
246 if (DataTextFormatString.Length > 0) {
\r
247 retVal = String.Format (DataTextFormatString, dataTextValue);
\r
249 retVal = dataTextValue.ToString ();
\r