2010-07-23 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataControlButton.cs
1 //
2 // System.Web.UI.WebControls.DataControlButton.cs
3 //
4 // Authors:
5 //      Lluis Sanchez Gual (lluis@novell.com)
6 //
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Web;
30 using System.Web.UI;
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.Drawing;
34
35 namespace System.Web.UI.WebControls
36 {
37         internal interface IDataControlButton : IButtonControl
38         {
39                 Control Container { get; set;}
40                 string ImageUrl { get;set;}
41                 bool AllowCallback { get; set;}
42                 ButtonType ButtonType { get;}
43         }
44
45         [SupportsEventValidation]
46         internal class DataControlButton : Button, IDataControlButton
47         {
48                 public static IDataControlButton CreateButton (ButtonType type, Control container, string text, string image, string command, string commandArg, bool allowCallback)
49                 {
50                         IDataControlButton btn;
51
52                         switch (type) {
53                                 case ButtonType.Link:
54                                         btn = new DataControlLinkButton ();
55                                         break;
56                                 case ButtonType.Image:
57                                         btn = new DataControlImageButton ();
58                                         btn.ImageUrl = image;
59                                         break;
60                                 default:
61                                         btn = new DataControlButton ();
62                                         break;
63                         }
64
65                         btn.Container = container;
66                         btn.CommandName = command;
67                         btn.CommandArgument = commandArg;
68                         btn.Text = text;
69                         btn.CausesValidation = false;
70                         btn.AllowCallback = allowCallback;
71
72                         return btn;
73                 }
74
75                 Control _container;
76
77                 public Control Container {
78                         get { return _container; }
79                         set { _container = value; }
80                 }
81
82                 public string ImageUrl {
83                         get { return String.Empty; }
84                         set { }
85                 }
86
87                 public bool AllowCallback {
88                         get { return ViewState.GetBool ("AllowCallback", true); }
89                         set { ViewState ["AllowCallback"] = value; }
90                 }
91
92                 public ButtonType ButtonType {
93                         get { return ButtonType.Button; }
94                 }
95
96                 public override bool UseSubmitBehavior {
97                         get { return false; }
98                         set { throw new NotSupportedException (); }
99                 }
100
101                 internal override string GetClientScriptEventReference ()
102                 {
103                         if (AllowCallback) {
104                                 ICallbackContainer ccner = Container as ICallbackContainer;
105                                 if (ccner != null)
106                                         return ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
107                         }
108                         return base.GetClientScriptEventReference ();
109                 }
110
111                 protected override PostBackOptions GetPostBackOptions ()
112                 {
113                         IPostBackContainer pcner = Container as IPostBackContainer;
114                         if (pcner != null)
115                                 return pcner.GetPostBackOptions (this);
116                         return base.GetPostBackOptions ();
117                 }
118         }
119
120         internal class DataControlLinkButton : LinkButton, IDataControlButton
121         {
122                 Control _container;
123
124                 public Control Container {
125                         get { return _container; }
126                         set { _container = value; }
127                 }
128
129                 public string ImageUrl {
130                         get { return String.Empty; }
131                         set { }
132                 }
133
134                 public bool AllowCallback {
135                         get { return ViewState.GetBool ("AllowCallback", true); }
136                         set { ViewState ["AllowCallback"] = value; }
137                 }
138
139                 public ButtonType ButtonType {
140                         get { return ButtonType.Link; }
141                 }
142
143                 protected internal override void Render (HtmlTextWriter writer)
144                 {
145                         EnsureForeColor ();
146                         if (AllowCallback) {
147                                 ICallbackContainer ccner = Container as ICallbackContainer;
148                                 if (ccner != null)
149                                         OnClientClick = ClientScriptManager.EnsureEndsWithSemicolon (OnClientClick) + ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
150                         }
151
152                         base.Render (writer);
153                 }
154
155                 void EnsureForeColor ()
156                 {
157                         if (ForeColor != Color.Empty)
158                                 return;
159
160                         for (Control parent = Parent; parent != null; parent = parent.Parent) {
161                                 WebControl wc = parent as WebControl;
162                                 if (wc != null && wc.ForeColor != Color.Empty) {
163                                         ForeColor = wc.ForeColor;
164                                         break;
165                                 }
166                                 if (parent == Container)
167                                         break;
168                         }
169                 }
170
171                 protected override PostBackOptions GetPostBackOptions ()
172                 {
173                         IPostBackContainer pcner = Container as IPostBackContainer;
174                         if (pcner != null)
175                                 return pcner.GetPostBackOptions (this);
176                         return base.GetPostBackOptions ();
177                 }
178
179         }
180
181         internal class DataControlImageButton : ImageButton, IDataControlButton
182         {
183                 Control _container;
184
185                 public Control Container {
186                         get { return _container; }
187                         set { _container = value; }
188                 }
189
190                 public bool AllowCallback {
191                         get { return ViewState.GetBool ("AllowCallback", true); }
192                         set { ViewState ["AllowCallback"] = value; }
193                 }
194
195                 public ButtonType ButtonType {
196                         get { return ButtonType.Image; }
197                 }
198
199                 internal override string GetClientScriptEventReference ()
200                 {
201                         if (AllowCallback) {
202                                 ICallbackContainer ccner = Container as ICallbackContainer;
203                                 if (ccner != null)
204                                         return ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
205                         }
206                         return base.GetClientScriptEventReference ();
207                 }
208
209                 protected override PostBackOptions GetPostBackOptions ()
210                 {
211                         IPostBackContainer pcner = Container as IPostBackContainer;
212                         if (pcner != null)
213                                 return pcner.GetPostBackOptions (this);
214                         return base.GetPostBackOptions ();
215                 }
216
217         }
218 }
219