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
;
40 namespace System
.Web
.UI
.WebControls
42 public class HyperLinkColumn
: DataGridColumn
44 PropertyDescriptor textFieldDescriptor
;
45 PropertyDescriptor urlFieldDescriptor
;
47 public HyperLinkColumn ()
51 [DefaultValue (""), WebCategory ("Misc")]
52 [WebSysDescription ("The field that gets data-bound to the NavigateUrl.")]
53 public virtual string DataNavigateUrlField
{
55 object o
= ViewState
["DataNavigateUrlField"];
62 ViewState
["DataNavigateUrlField"] = value;
67 // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
69 [DefaultValue (""), WebCategory ("Misc")]
70 [Description ("The formatting rule for the text content that gets data-bound to the NavigateUrl.")]
71 public virtual string DataNavigateUrlFormatString
{
73 object o
= ViewState
["DataNavigateUrlFormatString"];
80 ViewState
["DataNavigateUrlFormatString"] = value;
85 [DefaultValue (""), WebCategory ("Misc")]
86 [WebSysDescription ("The field that gets data-bound to the Text property.")]
87 public virtual string DataTextField
{
89 object o
= ViewState
["DataTextField"];
95 ViewState
["DataTextField"] = value;
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.")]
104 public virtual string DataTextFormatString
{
106 object o
= ViewState
["DataTextFormatString"];
113 ViewState
["DataTextFormatString"] = value;
118 [DefaultValue (""), WebCategory ("Misc")]
119 [WebSysDescription ("The URL that this hyperlink links to.")]
120 public virtual string NavigateUrl
{
122 object o
= ViewState
["NavigateUrl"];
129 ViewState
["NavigateUrl"] = value;
134 [DefaultValue (""), WebCategory ("Misc")]
135 [WebSysDescription ("The target frame in which the NavigateUrl property should be opened.")]
136 public virtual string Target
{
138 object o
= ViewState
["Target"];
145 ViewState
["Target"] = value;
150 [DefaultValue (""), WebCategory ("Misc")]
151 [WebSysDescription ("The Text for the hyperlink.")]
152 public virtual string Text
{
154 object o
= ViewState
["Text"];
161 ViewState
["Text"] = value;
166 public override void Initialize ()
168 textFieldDescriptor
= null;
169 urlFieldDescriptor
= null;
173 public override void InitializeCell (TableCell cell
, int columnIndex
, ListItemType itemType
)
175 base.InitializeCell (cell
, columnIndex
, itemType
);
177 if (itemType
!= ListItemType
.Header
&& itemType
!= ListItemType
.Footer
) {
178 HyperLink toDisplay
= new HyperLink ();
179 toDisplay
.Text
= Text
;
180 toDisplay
.NavigateUrl
= NavigateUrl
;
181 toDisplay
.Target
= Target
;
183 if (DataTextField
.Length
> 0 || DataNavigateUrlField
.Length
> 0)
184 toDisplay
.DataBinding
+= new EventHandler (OnDataBindHyperLinkColumn
);
186 cell
.Controls
.Add (toDisplay
);
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
)
227 if (dataUrlValue
== null)
231 if (DataNavigateUrlFormatString
.Length
> 0) {
232 retVal
= String
.Format (DataNavigateUrlFormatString
, dataUrlValue
);
234 retVal
= dataUrlValue
.ToString ();
240 protected virtual string FormatDataTextValue (object dataTextValue
)
242 if (dataTextValue
== null)
246 if (DataTextFormatString
.Length
> 0) {
247 retVal
= String
.Format (DataTextFormatString
, dataTextValue
);
249 retVal
= dataTextValue
.ToString ();