New test.
[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                 [DefaultValue("")]
84                 [WebSysDescription ("")]
85                 [WebCategory ("Misc")]
86                 public virtual string NavigateUrl {
87                         get {
88                                 return ViewState.GetString ("NavigateUrl", String.Empty);
89                         }
90                         set { ViewState ["NavigateUrl"] = value; }
91                 }
92
93                 [DefaultValue("")]
94                 [WebSysDescription ("")]
95                 [WebCategory ("Misc")]
96                 public virtual string Target {
97                         get {
98                                 return ViewState.GetString ("Target", String.Empty);
99                         }
100                         set { ViewState ["Target"] = value; }
101                 }
102
103                 [DefaultValue("")]
104                 [WebSysDescription ("")]
105                 [WebCategory ("Misc")]
106                 public virtual string Text {
107                         get {
108                                 return ViewState.GetString ("Text", String.Empty);
109                         }
110                         set { ViewState ["Text"] = value; }
111                 }
112
113                 protected virtual string FormatDataNavigateUrlValue (object value)
114                 {
115                         string format = DataNavigateUrlFormatString;
116                         if (format == "")
117                                 format = null;
118
119                         return DataBinder.FormatResult (value, format);
120                 }
121
122                 protected virtual string FormatDataTextValue (object value)
123                 {
124                         string format = DataTextFormatString;
125                         if (format == "")
126                                 format = null;
127
128                         return DataBinder.FormatResult (value, format);
129                 }
130
131                 public override void Initialize ()
132                 {
133                         base.Initialize ();
134                 }
135
136                 void ItemDataBinding (object sender, EventArgs args)
137                 {
138                         TableCell cell = (TableCell)sender;
139                         HyperLink ctrl = (HyperLink)cell.Controls[0];
140                         DataGridItem item = (DataGridItem)cell.NamingContainer;
141
142                         if (DataNavigateUrlField != "")
143                                 ctrl.NavigateUrl = FormatDataNavigateUrlValue (DataBinder.Eval (item.DataItem, DataNavigateUrlField));
144                         else
145                                 ctrl.NavigateUrl = NavigateUrl;
146
147                         if (DataTextField != "")
148                                 ctrl.Text = FormatDataTextValue (DataBinder.Eval (item.DataItem, DataTextField));
149                         else
150                                 ctrl.Text = Text;
151
152                         ctrl.Target = Target;
153                 }
154
155                 public override void InitializeCell (TableCell cell, int column_index, ListItemType item_type)
156                 {
157                         base.InitializeCell (cell, column_index, item_type);
158
159                         switch (item_type)
160                         {
161                         case ListItemType.Separator: 
162                         case ListItemType.Pager:
163                         case ListItemType.Footer:
164                         case ListItemType.Header: {
165                                 // Base handles header and footer, dunno about the others
166                                 return;
167                         }
168                         case ListItemType.Item:
169                         case ListItemType.EditItem:
170                         case ListItemType.AlternatingItem:
171                                 cell.DataBinding += new EventHandler(ItemDataBinding);
172                                 cell.Controls.Add (new HyperLink ());
173                                 break;
174                         }
175                 }
176         }
177 }