Merge pull request #2916 from ludovic-henry/fix-40306
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / HyperLinkColumn.cs
1 //
2 // System.Web.UI.WebControls.HyperLinkColumn.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
26 //
27
28 using System.ComponentModel;
29 using System.Data;
30 using System.Security.Permissions;
31
32 namespace System.Web.UI.WebControls {
33
34         // CAS
35         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         public class HyperLinkColumn : DataGridColumn
38         {
39                 public HyperLinkColumn ()
40                 {
41                 }
42
43                 [DefaultValue("")]
44                 [WebSysDescription ("")]
45                 [WebCategory ("Misc")]
46                 public virtual string DataNavigateUrlField {
47                         get {
48                                 return ViewState.GetString ("DataNavigateUrlField", String.Empty);
49                         }
50                         set { ViewState ["DataNavigateUrlField"] = value; }
51                 }
52
53                 [DefaultValue("")]
54                 [Description("The formatting applied to the value bound to the NavigateUrl property.")]
55                 [WebCategory ("Misc")]
56                 public virtual string DataNavigateUrlFormatString {
57                         get {
58                                 return ViewState.GetString ("DataNavigateUrlFormatString", String.Empty);
59                         }
60                         set { ViewState ["DataNavigateUrlFormatString"] = value; }
61                 }
62
63                 [DefaultValue("")]
64                 [WebSysDescription ("")]
65                 [WebCategory ("Misc")]
66                 public virtual string DataTextField {
67                         get {
68                                 return ViewState.GetString ("DataTextField", String.Empty);
69                         }
70                         set { ViewState ["DataTextField"] = value; }
71                 }
72
73                 [Description("The formatting applied to the value bound to the Text property.")]
74                 [DefaultValue("")]
75                 [WebCategory ("Misc")]
76                 public virtual string DataTextFormatString {
77                         get {
78                                 return ViewState.GetString ("DataTextFormatString", String.Empty);
79                         }
80                         set { ViewState ["DataTextFormatString"] = value; }
81                 }
82
83                 [UrlProperty]
84                 [DefaultValue("")]
85                 [WebSysDescription ("")]
86                 [WebCategory ("Misc")]
87                 public virtual string NavigateUrl {
88                         get {
89                                 return ViewState.GetString ("NavigateUrl", String.Empty);
90                         }
91                         set { ViewState ["NavigateUrl"] = value; }
92                 }
93
94                 [DefaultValue("")]
95                 [WebSysDescription ("")]
96                 [WebCategory ("Misc")]
97                 [TypeConverter ("System.Web.UI.WebControls.TargetConverter")]
98                 public virtual string Target {
99                         get {
100                                 return ViewState.GetString ("Target", String.Empty);
101                         }
102                         set { ViewState ["Target"] = value; }
103                 }
104
105                 [Localizable (true)]
106                 [DefaultValue("")]
107                 [WebSysDescription ("")]
108                 [WebCategory ("Misc")]
109                 public virtual string Text {
110                         get {
111                                 return ViewState.GetString ("Text", String.Empty);
112                         }
113                         set { ViewState ["Text"] = value; }
114                 }
115
116                 protected virtual string FormatDataNavigateUrlValue (object value)
117                 {
118                         string format = DataNavigateUrlFormatString;
119                         if (format == "")
120                                 format = null;
121
122                         return DataBinder.FormatResult (value, format);
123                 }
124
125                 protected virtual string FormatDataTextValue (object value)
126                 {
127                         string format = DataTextFormatString;
128                         if (format == "")
129                                 format = null;
130
131                         return DataBinder.FormatResult (value, format);
132                 }
133
134                 public override void Initialize ()
135                 {
136                         base.Initialize ();
137                 }
138
139                 void ItemDataBinding (object sender, EventArgs args)
140                 {
141                         TableCell cell = (TableCell)sender;
142                         HyperLink ctrl = (HyperLink)cell.Controls[0];
143                         DataGridItem item = (DataGridItem)cell.NamingContainer;
144
145                         if (DataNavigateUrlField != "")
146                                 ctrl.NavigateUrl = FormatDataNavigateUrlValue (DataBinder.Eval (item.DataItem, DataNavigateUrlField));
147                         else
148                                 ctrl.NavigateUrl = NavigateUrl;
149
150                         if (DataTextField != "")
151                                 ctrl.Text = FormatDataTextValue (DataBinder.Eval (item.DataItem, DataTextField));
152                         else
153                                 ctrl.Text = Text;
154
155                         ctrl.Target = Target;
156                 }
157
158                 public override void InitializeCell (TableCell cell, int column_index, ListItemType item_type)
159                 {
160                         base.InitializeCell (cell, column_index, item_type);
161
162                         switch (item_type)
163                         {
164                         case ListItemType.Separator: 
165                         case ListItemType.Pager:
166                         case ListItemType.Footer:
167                         case ListItemType.Header: {
168                                 // Base handles header and footer, dunno about the others
169                                 return;
170                         }
171                         case ListItemType.Item:
172                         case ListItemType.EditItem:
173                         case ListItemType.AlternatingItem:
174                                 cell.DataBinding += new EventHandler(ItemDataBinding);
175                                 cell.Controls.Add (new HyperLink ());
176                                 break;
177                         }
178                 }
179         }
180 }