2006-12-31 Igor Zelmanovich <igorz@mainsoft.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 class DataControlButton: Button
41         {
42                 Control container;
43                 
44                 public DataControlButton (Control container)
45                 {
46                         this.container = container;
47                         CausesValidation = false;
48                 }
49                 
50                 public DataControlButton (Control container, string text, string image, string command, string commandArg, bool allowCallback)
51                         : this (container)
52                 {
53                         Text = text;
54                         ImageUrl = image;
55                         CommandName = command;
56                         CommandArgument = commandArg;
57                         AllowCallback = allowCallback;
58                 }
59                 
60                 public Control Container {
61                         get { return container; }
62                         set { container = value; }
63                 }
64
65                 public string ImageUrl {
66                         get {
67                                 object o = ViewState["iu"];
68                                 if (o != null) return (string) o;
69                                 return String.Empty;
70                         }
71                         set {
72                                 ViewState["iu"] = value;
73                         }
74                 }
75                 
76                 public bool AllowCallback {
77                         get {
78                                 object o = ViewState["ac"];
79                                 if (o != null) return (bool) o;
80                                 return true;
81                         }
82                         set {
83                                 ViewState["ac"] = value;
84                         }
85                 }
86                 
87                 public virtual ButtonType ButtonType {
88                         get {
89                                 object ob = ViewState ["ButtonType"];
90                                 if (ob != null) return (ButtonType) ob;
91                                 return ButtonType.Link;
92                         }
93                         set {
94                                 ViewState ["ButtonType"] = value;
95                         }
96                 }
97
98                 protected internal override void Render (HtmlTextWriter writer)
99                 {
100                         if (CommandName.Length > 0 && ButtonType == ButtonType.Link) {
101                                 EnsureForeColor ();
102                         }
103
104                         if (CommandName.Length > 0 || CommandArgument.Length > 0 || ButtonType == ButtonType.Button)
105                         {
106                                 string postScript = null;
107                                 string callScript = null;
108                                 PostBackOptions ops = null;
109
110                                 IPostBackContainer pcner = container as IPostBackContainer;
111                                 if (pcner != null) {
112                                         ops = pcner.GetPostBackOptions (this);
113                                         ops.RequiresJavaScriptProtocol = ButtonType == ButtonType.Link;
114                                 }
115                                 else
116                                         ops = GetPostBackOptions ();
117
118                                 postScript = Page.ClientScript.GetPostBackEventReference (ops, !Page.IsCallback);
119                                 
120                                 if (AllowCallback) {
121                                         ICallbackContainer ccner = container as ICallbackContainer;
122                                         if (ccner != null)
123                                                 callScript = ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
124                                 }
125                         
126                                 ControlStyle.AddAttributesToRender (writer);
127                                 
128                                 if (ButtonType == ButtonType.Image) {
129                                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
130                                         writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
131                                         if (callScript != null)
132                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
133                                         else
134                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
135                                         if (Text.Length > 0)
136                                                 writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
137                                         writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
138                                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
139                                         writer.RenderEndTag ();
140                                 }
141                                 if (ButtonType == ButtonType.Link) {
142                                         if (callScript != null) {
143                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
144                                         }
145                                         writer.AddAttribute (HtmlTextWriterAttribute.Href, postScript);
146                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
147                                         writer.Write (Text);
148                                         writer.RenderEndTag ();
149                                 }
150                                 if (ButtonType == ButtonType.Button) {
151                                         if (callScript != null)
152                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
153                                         else
154                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
155
156                                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "button");
157                                         writer.AddAttribute (HtmlTextWriterAttribute.Name, ClientID);
158                                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
159                                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
160                                         writer.RenderEndTag ();
161                                 }
162                         }
163                         else {
164                                 if (ImageUrl.Length > 0) {
165                                         ControlStyle.AddAttributesToRender (writer);
166                                         writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
167                                         if (Text.Length > 0)
168                                                 writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
169                                         writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
170                                         writer.RenderBeginTag (HtmlTextWriterTag.Img);
171                                         writer.RenderEndTag ();
172                                 }
173                                 else {
174                                         if (!ControlStyle.IsEmpty) {
175                                                 ControlStyle.AddAttributesToRender (writer);
176                                         }
177                                         writer.RenderBeginTag (HtmlTextWriterTag.Span);
178                                         writer.Write (Text);
179                                         writer.RenderEndTag ();
180                                 }
181                         }
182                 }
183                 
184                 protected override PostBackOptions GetPostBackOptions () {
185                         PostBackOptions options = new PostBackOptions (this);
186                         options.Argument = "";
187                         options.RequiresJavaScriptProtocol = ButtonType == ButtonType.Link;
188                         options.ClientSubmit = true;
189                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
190                         if (options.PerformValidation)
191                                 options.ValidationGroup = ValidationGroup;
192
193                         return options;
194                 }
195
196                 private void EnsureForeColor () {
197                         if (ForeColor != Color.Empty)
198                                 return;
199
200                         for (Control parent = Parent; parent != null; parent = parent.Parent) {
201                                 WebControl wc = parent as WebControl;
202                                 if (wc != null && wc.ForeColor != Color.Empty) {
203                                         ForeColor = wc.ForeColor;
204                                         break;
205                                 }
206                                 if (parent == container)
207                                         break;
208                         }
209                 }
210         }
211 }
212
213 #endif