2006-08-09 Robert Jordan <robertj@gmx.net>
[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
37 namespace System.Web.UI.WebControls
38 {
39         internal class DataControlButton: Button
40         {
41                 Control container;
42                 
43                 public DataControlButton (Control container)
44                 {
45                         this.container = container;
46                 }
47                 
48                 public DataControlButton (Control container, string text, string image, string command, string commandArg, bool allowCallback)
49                 {
50                         this.container = container;
51                         Text = text;
52                         ImageUrl = image;
53                         CommandName = command;
54                         CommandArgument = commandArg;
55                         AllowCallback = allowCallback;
56                 }
57                 
58                 public string ImageUrl {
59                         get {
60                                 object o = ViewState["iu"];
61                                 if (o != null) return (string) o;
62                                 return String.Empty;
63                         }
64                         set {
65                                 ViewState["iu"] = value;
66                         }
67                 }
68                 
69                 public bool AllowCallback {
70                         get {
71                                 object o = ViewState["ac"];
72                                 if (o != null) return (bool) o;
73                                 return true;
74                         }
75                         set {
76                                 ViewState["ac"] = value;
77                         }
78                 }
79                 
80                 public virtual ButtonType ButtonType {
81                         get {
82                                 object ob = ViewState ["ButtonType"];
83                                 if (ob != null) return (ButtonType) ob;
84                                 return ButtonType.Link;
85                         }
86                         set {
87                                 ViewState ["ButtonType"] = value;
88                         }
89                 }
90
91                 protected internal override void Render (HtmlTextWriter writer)
92                 {
93                         if (CommandName.Length > 0 || ButtonType == ButtonType.Button)
94                         {
95                                 string postScript = null;
96                                 string callScript = null;
97                                 
98                                 IPostBackContainer pcner = container as IPostBackContainer;
99                                 if (pcner != null) {
100                                         PostBackOptions ops = pcner.GetPostBackOptions (this);
101                                         postScript = container.Page.ClientScript.GetPostBackEventReference (ops);
102                                 } else
103                                         postScript = Page.ClientScript.GetPostBackEventReference (this, "");
104
105                                 if (CausesValidation && Page.Validators.Count > 0) {
106                                         // TOSHOK: review if this is the correct usage of the "fresh" client side stuff
107                                         ClientScriptManager csm = new ClientScriptManager (Page);
108                                         postScript = csm.GetClientValidationEvent () + postScript;
109                                 }
110                                 
111                                 if (AllowCallback) {
112                                         ICallbackContainer ccner = container as ICallbackContainer;
113                                         if (ccner != null)
114                                                 callScript = ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);
115                                 }
116                         
117                                 ControlStyle.AddAttributesToRender (writer);
118                                 
119                                 if (ButtonType == ButtonType.Image) {
120                                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");
121                                         writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
122                                         if (callScript != null)
123                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
124                                         else
125                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
126                                         if (Text.Length > 0)
127                                                 writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
128                                         writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
129                                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
130                                         writer.RenderEndTag ();
131                                 }
132                                 if (ButtonType == ButtonType.Link) {
133                                         if (callScript != null) {
134                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
135                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, "javascript:");
136                                         }
137                                         else
138                                                 writer.AddAttribute (HtmlTextWriterAttribute.Href, "javascript:" + postScript);
139                                         writer.RenderBeginTag (HtmlTextWriterTag.A);
140                                         writer.Write (Text);
141                                         writer.RenderEndTag ();
142                                 }
143                                 if (ButtonType == ButtonType.Button) {
144                                         if (callScript != null)
145                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);
146                                         else
147                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
148
149                                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");
150                                         writer.AddAttribute (HtmlTextWriterAttribute.Name, ClientID);
151                                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);
152                                         writer.RenderBeginTag (HtmlTextWriterTag.Input);
153                                         writer.RenderEndTag ();
154                                 }
155                         }
156                         else {
157                                 if (ImageUrl.Length > 0) {
158                                         ControlStyle.AddAttributesToRender (writer);
159                                         writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveClientUrl (ImageUrl));
160                                         if (Text.Length > 0)
161                                                 writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);
162                                         writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0px");
163                                         writer.RenderBeginTag (HtmlTextWriterTag.Img);
164                                         writer.RenderEndTag ();
165                                 }
166                                 else {
167                                         if (!ControlStyle.IsEmpty) {
168                                                 ControlStyle.AddAttributesToRender (writer);
169                                         }
170                                         writer.RenderBeginTag (HtmlTextWriterTag.Span);
171                                         writer.Write (Text);
172                                         writer.RenderEndTag ();
173                                 }
174                         }
175                 }
176         }
177 }
178
179 #endif