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 using System.ComponentModel;
\r
17 using System.Web.UI;
\r
19 namespace System.Web.UI.WebControls
\r
21 public class HyperLinkColumn: DataGridColumn
\r
23 PropertyDescriptor textFieldDescriptor;
\r
24 PropertyDescriptor urlFieldDescriptor;
\r
26 public HyperLinkColumn ()
\r
30 [DefaultValue (""), WebCategory ("Misc")]
31 [WebSysDescription ("The field that gets data-bound to the NavigateUrl.")]
\r
32 public virtual string DataNavigateUrlField {
\r
34 object o = ViewState ["DataNavigateUrlField"];
\r
37 return String.Empty;
\r
41 ViewState ["DataNavigateUrlField"] = value;
\r
46 // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
\r
48 [DefaultValue (""), WebCategory ("Misc")]
49 [Description ("The formatting rule for the text content that gets data-bound to the NavigateUrl.")]
\r
50 public virtual string DataNavigateUrlFormatString {
\r
52 object o = ViewState ["DataNavigateUrlFormatString"];
\r
55 return String.Empty;
\r
59 ViewState ["DataNavigateUrlFormatString"] = value;
\r
64 [DefaultValue (""), WebCategory ("Misc")]
65 [WebSysDescription ("The field that gets data-bound to the Text property.")]
\r
66 public virtual string DataTextField {
\r
68 object o = ViewState ["DataTextField"];
\r
71 return String.Empty;
\r
74 ViewState ["DataTextField"] = value;
\r
79 // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
81 [DefaultValue (""), WebCategory ("Misc")]
82 [Description ("The formatting rule for the text content that gets data-bound to the Text property.")]
\r
83 public virtual string DataTextFormatString {
\r
85 object o = ViewState ["DataTextFormatString"];
\r
88 return String.Empty;
\r
92 ViewState ["DataTextFormatString"] = value;
\r
97 [DefaultValue (""), WebCategory ("Misc")]
98 [WebSysDescription ("The URL that this hyperlink links to.")]
\r
99 public virtual string NavigateUrl {
\r
101 object o = ViewState ["NavigateUrl"];
\r
104 return String.Empty;
\r
108 ViewState ["NavigateUrl"] = value;
\r
109 OnColumnChanged ();
\r
113 [DefaultValue (""), WebCategory ("Misc")]
114 [WebSysDescription ("The target frame in which the NavigateUrl property should be opened.")]
\r
115 public virtual string Target {
\r
117 object o = ViewState ["Target"];
\r
120 return String.Empty;
\r
124 ViewState ["Target"] = value;
\r
125 OnColumnChanged ();
\r
129 [DefaultValue (""), WebCategory ("Misc")]
130 [WebSysDescription ("The Text for the hyperlink.")]
\r
131 public virtual string Text {
\r
133 object o = ViewState ["Text"];
\r
136 return String.Empty;
\r
140 ViewState ["Text"] = value;
\r
141 OnColumnChanged ();
\r
145 public override void Initialize ()
\r
147 textFieldDescriptor = null;
\r
148 urlFieldDescriptor = null;
\r
149 base.Initialize ();
\r
152 public override void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)
\r
154 base.InitializeCell (cell, columnIndex, itemType);
\r
156 if (itemType != ListItemType.Header && itemType != ListItemType.Footer) {
\r
157 HyperLink toDisplay = new HyperLink ();
\r
158 toDisplay.Text = Text;
\r
159 toDisplay.NavigateUrl = NavigateUrl;
\r
160 toDisplay.Target = Target;
\r
162 if (DataTextField.Length > 0 || DataNavigateUrlField.Length > 0)
\r
163 toDisplay.DataBinding += new EventHandler (OnDataBindHyperLinkColumn);
\r
165 cell.Controls.Add (toDisplay);
\r
169 private void OnDataBindHyperLinkColumn (object sender, EventArgs e)
171 HyperLink link = (HyperLink) sender;
172 object item = ((DataGridItem) link.NamingContainer).DataItem;
174 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (item);
175 if (textFieldDescriptor == null)
176 textFieldDescriptor = properties.Find (DataTextField, true);
178 if (urlFieldDescriptor == null)
179 urlFieldDescriptor = properties.Find (DataNavigateUrlField, true);
181 if (DataTextField.Length > 0 && textFieldDescriptor == null && !DesignMode)
182 throw new HttpException (HttpRuntime.FormatResourceString (
183 "Field_Not_Found", DataTextField));
185 if (DataNavigateUrlField.Length > 0 && urlFieldDescriptor == null && !DesignMode)
186 throw new HttpException (HttpRuntime.FormatResourceString (
187 "Field_Not_Found", DataNavigateUrlField));
189 if (textFieldDescriptor != null) {
190 link.Text = FormatDataTextValue (textFieldDescriptor.GetValue (item));
195 if (urlFieldDescriptor != null) {
196 link.NavigateUrl = FormatDataNavigateUrlValue (urlFieldDescriptor.GetValue (item));
198 link.NavigateUrl = "url";
202 protected virtual string FormatDataNavigateUrlValue (object dataUrlValue)
\r
204 if (dataUrlValue == null)
\r
205 return String.Empty;
\r
208 if (DataNavigateUrlFormatString.Length > 0) {
\r
209 retVal = String.Format (DataNavigateUrlFormatString, dataUrlValue);
\r
211 retVal = dataUrlValue.ToString ();
\r
217 protected virtual string FormatDataTextValue (object dataTextValue)
\r
219 if (dataTextValue == null)
\r
220 return String.Empty;
\r
223 if (DataTextFormatString.Length > 0) {
\r
224 retVal = String.Format (DataTextFormatString, dataTextValue);
\r
226 retVal = dataTextValue.ToString ();
\r