2008-03-13 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 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
29 #if NET_2_0
30
31 using System;
32 using System.Web;
33 using System.Web.UI;
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
36 using System.Drawing;
37
38 namespace System.Web.UI.WebControls
39 {
40         internal interface IDataControlButton : IButtonControl
41         {
42                 Control Container { get; set;}
43                 string ImageUrl { get;set;}
44                 bool AllowCallback { get; set;}
45                 ButtonType ButtonType { get;}
46         }
47         
48         internal class DataControlButton : Button, IDataControlButton
49         {
50                 public static IDataControlButton CreateButton (ButtonType type, Control container, string text, string image, string command, string commandArg, bool allowCallback)
51                 {
52                         IDataControlButton btn;
53
54                         switch (type) {
55                         case ButtonType.Link:
56                                 btn = new DataControlLinkButton ();
57                                 break;
58                         case ButtonType.Image:
59                                 btn = new DataControlImageButton ();
60                                 btn.ImageUrl = image;
61                                 break;
62                         default:
63                                 btn = new DataControlButton ();
64                                 break;
65                         }
66
67                         btn.Container = container;
68                         btn.CommandName = command;
69                         btn.CommandArgument = commandArg;
70                         btn.Text = text;
71                         btn.CausesValidation = false;
72                         btn.AllowCallback = allowCallback;
73
74                         return btn;
75                 }
76
77                 Control _container;
78
79                 public Control Container {
80                         get { return _container; }
81                         set { _container = value; }
82                 }
83
84                 public string ImageUrl {
85                         get { return String.Empty; }
86                         set { }
87                 }
88
89                 public bool AllowCallback {
90                         get { return ViewState.GetBool ("AllowCallback", true); }
91                         set { ViewState ["AllowCallback"] = value; }
92                 }
93
94                 public ButtonType ButtonType {
95                         get { return ButtonType.Button; }
96                 }
97
98                 public override bool UseSubmitBehavior {
99                         get { return false; }
100                         set { throw new NotSupportedException (); }
101                 }
102
103                 internal override string GetClientScriptEventReference ()
104                 {
105                         if (AllowCallback) {
106                                 ICallbackContainer ccner = Container as ICallbackContainer;
107                                 if (ccner != null)
108                                         return ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
109                         }
110                         return base.GetClientScriptEventReference ();
111                 }
112
113                 protected override PostBackOptions GetPostBackOptions ()
114                 {
115                         IPostBackContainer pcner = Container as IPostBackContainer;
116                         if (pcner != null)
117                                 return pcner.GetPostBackOptions (this);
118                         return base.GetPostBackOptions ();
119                 }
120         }
121
122         internal class DataControlLinkButton : LinkButton, IDataControlButton
123         {
124                 Control _container;
125
126                 public Control Container {
127                         get { return _container; }
128                         set { _container = value; }
129                 }
130
131                 public string ImageUrl {
132                         get { return String.Empty; }
133                         set { }
134                 }
135
136                 public bool AllowCallback {
137                         get { return ViewState.GetBool ("AllowCallback", true); }
138                         set { ViewState ["AllowCallback"] = value; }
139                 }
140
141                 public ButtonType ButtonType {
142                         get { return ButtonType.Link; }
143                 }
144
145                 protected internal override void Render (HtmlTextWriter writer)
146                 {
147                         EnsureForeColor ();
148                         if (AllowCallback) {
149                                 ICallbackContainer ccner = Container as ICallbackContainer;
150                                 if (ccner != null)
151                                         OnClientClick = ClientScriptManager.EnsureEndsWithSemicolon (OnClientClick) + ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
152                         }
153
154                         base.Render (writer);
155                 }
156
157                 void EnsureForeColor ()
158                 {
159                         if (ForeColor != Color.Empty)
160                                 return;
161
162                         for (Control parent = Parent; parent != null; parent = parent.Parent) {
163                                 WebControl wc = parent as WebControl;
164                                 if (wc != null && wc.ForeColor != Color.Empty) {
165                                         ForeColor = wc.ForeColor;
166                                         break;
167                                 }
168                                 if (parent == Container)
169                                         break;
170                         }
171                 }
172
173                 protected override PostBackOptions GetPostBackOptions ()
174                 {
175                         IPostBackContainer pcner = Container as IPostBackContainer;
176                         if (pcner != null)
177                                 return pcner.GetPostBackOptions (this);
178                         return base.GetPostBackOptions ();
179                 }
180
181         }
182
183         internal class DataControlImageButton : ImageButton, IDataControlButton
184         {
185                 Control _container;
186
187                 public Control Container {
188                         get { return _container; }
189                         set { _container = value; }
190                 }
191
192                 public bool AllowCallback {
193                         get { return ViewState.GetBool ("AllowCallback", true); }
194                         set { ViewState ["AllowCallback"] = value; }
195                 }
196
197                 public ButtonType ButtonType {
198                         get { return ButtonType.Image; }
199                 }
200
201                 internal override string GetClientScriptEventReference ()
202                 {
203                         if (AllowCallback) {
204                                 ICallbackContainer ccner = Container as ICallbackContainer;
205                                 if (ccner != null)
206                                         return ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
207                         }
208                         return base.GetClientScriptEventReference ();
209                 }
210
211                 protected override PostBackOptions GetPostBackOptions ()
212                 {
213                         IPostBackContainer pcner = Container as IPostBackContainer;
214                         if (pcner != null)
215                                 return pcner.GetPostBackOptions (this);
216                         return base.GetPostBackOptions ();
217                 }
218
219         }
220 }
221
222 #endif