2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HyperLinkColumn.cs
1 //
2 // System.Web.UI.WebControls.HyperLinkColumn.cs
3 //
4 // Authors:
5 //   Gaurav Vaish (gvaish@iitk.ac.in)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 // (C) Gaurav Vaish (2002)
11 // (C) 2003 Andreas Nahr
12 //\r
13
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34 \r
35 using System;\r
36 using System.ComponentModel;\r
37 using System.Web;\r
38 using System.Web.UI;\r
39 \r
40 namespace System.Web.UI.WebControls\r
41 {\r
42         public class HyperLinkColumn: DataGridColumn\r
43         {\r
44                 PropertyDescriptor textFieldDescriptor;\r
45                 PropertyDescriptor urlFieldDescriptor;\r
46 \r
47                 public HyperLinkColumn ()\r
48                 {\r
49                 }\r
50
51                 [DefaultValue (""), WebCategory ("Misc")]
52                 [WebSysDescription ("The field that gets data-bound to the NavigateUrl.")]\r
53                 public virtual string DataNavigateUrlField {\r
54                         get {\r
55                                 object o = ViewState ["DataNavigateUrlField"];\r
56                                 if (o != null)\r
57                                         return (string) o;\r
58                                 return String.Empty;\r
59                         }\r
60 \r
61                         set {\r
62                                 ViewState ["DataNavigateUrlField"] = value;\r
63                                 OnColumnChanged ();\r
64                         }\r
65                 }
66
67                 // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here\r
68
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
72                         get {\r
73                                 object o = ViewState ["DataNavigateUrlFormatString"];\r
74                                 if (o != null)\r
75                                         return (string) o;\r
76                                 return String.Empty;\r
77                         }\r
78 \r
79                         set {\r
80                                 ViewState ["DataNavigateUrlFormatString"] = value;\r
81                                 OnColumnChanged ();\r
82                         }\r
83                 }\r
84
85                 [DefaultValue (""), WebCategory ("Misc")]
86                 [WebSysDescription ("The field that gets data-bound to the Text property.")]\r
87                 public virtual string DataTextField {\r
88                         get {\r
89                                 object o = ViewState ["DataTextField"];\r
90                                 if (o != null)\r
91                                         return (string) o;\r
92                                 return String.Empty;\r
93                         }\r
94                         set {\r
95                                 ViewState ["DataTextField"] = value;\r
96                                 OnColumnChanged ();\r
97                         }\r
98                 }\r
99
100                 // LAMESPEC should use WebSysDescription as all others do, but MS uses Description here
101
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
105                         get {\r
106                                 object o = ViewState ["DataTextFormatString"];\r
107                                 if (o != null)\r
108                                         return (string) o;\r
109                                 return String.Empty;\r
110                         }\r
111 \r
112                         set {\r
113                                 ViewState ["DataTextFormatString"] = value;\r
114                                 OnColumnChanged ();\r
115                         }\r
116                 }\r
117
118                 [DefaultValue (""), WebCategory ("Misc")]
119                 [WebSysDescription ("The URL that this hyperlink links to.")]\r
120                 public virtual string NavigateUrl {\r
121                         get {\r
122                                 object o = ViewState ["NavigateUrl"];\r
123                                 if (o != null)\r
124                                         return (string) o;\r
125                                 return String.Empty;\r
126                         }\r
127 \r
128                         set {\r
129                                 ViewState ["NavigateUrl"] = value;\r
130                                 OnColumnChanged ();\r
131                         }\r
132                 }\r
133
134                 [DefaultValue (""), WebCategory ("Misc")]
135                 [WebSysDescription ("The target frame in which the NavigateUrl property should be opened.")]\r
136                 public virtual string Target {\r
137                         get {\r
138                                 object o = ViewState ["Target"];\r
139                                 if (o != null)\r
140                                         return (string) o;\r
141                                 return String.Empty;\r
142                         }\r
143 \r
144                         set {\r
145                                 ViewState ["Target"] = value;\r
146                                 OnColumnChanged ();\r
147                         }\r
148                 }\r
149
150                 [DefaultValue (""), WebCategory ("Misc")]
151                 [WebSysDescription ("The Text for the hyperlink.")]\r
152                 public virtual string Text {\r
153                         get {\r
154                                 object o = ViewState ["Text"];\r
155                                 if (o != null)\r
156                                         return (string) o;\r
157                                 return String.Empty;\r
158                         }\r
159 \r
160                         set {\r
161                                 ViewState ["Text"] = value;\r
162                                 OnColumnChanged ();\r
163                         }\r
164                 }\r
165 \r
166                 public override void Initialize ()\r
167                 {\r
168                         textFieldDescriptor = null;\r
169                         urlFieldDescriptor  = null;\r
170                         base.Initialize ();\r
171                 }\r
172 \r
173                 public override void InitializeCell (TableCell cell, int columnIndex, ListItemType itemType)\r
174                 {\r
175                         base.InitializeCell (cell, columnIndex, itemType);\r
176 \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
182 \r
183                                 if (DataTextField.Length > 0 || DataNavigateUrlField.Length > 0)\r
184                                         toDisplay.DataBinding += new EventHandler (OnDataBindHyperLinkColumn);\r
185 \r
186                                 cell.Controls.Add (toDisplay);\r
187                         }\r
188                 }\r
189 \r
190                 private void OnDataBindHyperLinkColumn (object sender, EventArgs e)
191                 {
192                         HyperLink link = (HyperLink) sender;
193                         object item = ((DataGridItem) link.NamingContainer).DataItem;
194
195                         PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (item);
196                         if (textFieldDescriptor == null)
197                                 textFieldDescriptor = properties.Find (DataTextField, true);
198
199                         if (urlFieldDescriptor == null)
200                                 urlFieldDescriptor = properties.Find (DataNavigateUrlField, true);
201
202                         if (DataTextField.Length > 0 && textFieldDescriptor == null && !DesignMode)
203                                 throw new HttpException (HttpRuntime.FormatResourceString (
204                                                                 "Field_Not_Found", DataTextField));
205
206                         if (DataNavigateUrlField.Length > 0 && urlFieldDescriptor == null && !DesignMode)
207                                 throw new HttpException (HttpRuntime.FormatResourceString (
208                                                                 "Field_Not_Found", DataNavigateUrlField));
209
210                         if (textFieldDescriptor != null) {
211                                 link.Text = FormatDataTextValue (textFieldDescriptor.GetValue (item));
212                         } else {
213                                 link.Text = Text;
214                         }
215
216                         if (urlFieldDescriptor != null) {
217                                 link.NavigateUrl = FormatDataNavigateUrlValue (urlFieldDescriptor.GetValue (item));
218                                 return;
219                         }
220                         
221                         if (DataNavigateUrlField.Length != 0 && DesignMode)
222                                 link.NavigateUrl = "url";
223                 }
224
225                 protected virtual string FormatDataNavigateUrlValue (object dataUrlValue)\r
226                 {\r
227                         if (dataUrlValue == null)\r
228                                 return String.Empty;\r
229 \r
230                         string retVal;\r
231                         if (DataNavigateUrlFormatString.Length > 0) {\r
232                                 retVal = String.Format (DataNavigateUrlFormatString, dataUrlValue);\r
233                         } else {\r
234                                 retVal = dataUrlValue.ToString ();\r
235                         }\r
236 \r
237                         return retVal;\r
238                 }\r
239 \r
240                 protected virtual string FormatDataTextValue (object dataTextValue)\r
241                 {\r
242                         if (dataTextValue == null)\r
243                                 return String.Empty;\r
244 \r
245                         string retVal;\r
246                         if (DataTextFormatString.Length > 0) {\r
247                                 retVal = String.Format (DataTextFormatString, dataTextValue);\r
248                         } else {\r
249                                 retVal = dataTextValue.ToString ();\r
250                         }\r
251 \r
252                         return retVal;\r
253                 }\r
254         }\r
255 }\r
256 \r