2005-04-12 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / DataControlButton.cs
1 //\r
2 // System.Web.UI.WebControls.DataControlButton.cs\r
3 //\r
4 // Authors:\r
5 //      Lluis Sanchez Gual (lluis@novell.com)\r
6 //\r
7 // (C) 2005 Novell, Inc (http://www.novell.com)\r
8 //\r
9 // Permission is hereby granted, free of charge, to any person obtaining\r
10 // a copy of this software and associated documentation files (the\r
11 // "Software"), to deal in the Software without restriction, including\r
12 // without limitation the rights to use, copy, modify, merge, publish,\r
13 // distribute, sublicense, and/or sell copies of the Software, and to\r
14 // permit persons to whom the Software is furnished to do so, subject to\r
15 // the following conditions:\r
16 // \r
17 // The above copyright notice and this permission notice shall be\r
18 // included in all copies or substantial portions of the Software.\r
19 // \r
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
27 //\r
28 \r
29 #if NET_2_0\r
30 \r
31 using System;\r
32 using System.Web;\r
33 using System.Web.UI;\r
34 using System.ComponentModel;
35 using System.ComponentModel.Design;\r
36 \r
37 namespace System.Web.UI.WebControls\r
38 {\r
39         internal class DataControlButton: Button\r
40         {\r
41                 Control container;\r
42                 \r
43                 public DataControlButton (Control container)\r
44                 {\r
45                         this.container = container;\r
46                 }\r
47                 \r
48                 public DataControlButton (Control container, string text, string image, string command, string commandArg, bool allowCallback)\r
49                 {\r
50                         this.container = container;\r
51                         Text = text;\r
52                         ImageUrl = image;\r
53                         CommandName = command;\r
54                         CommandArgument = commandArg;\r
55                         AllowCallback = allowCallback;\r
56                 }\r
57                 \r
58                 public string ImageUrl {\r
59                         get {\r
60                                 object o = ViewState["iu"];\r
61                                 if (o != null) return (string) o;\r
62                                 return String.Empty;\r
63                         }\r
64                         set {\r
65                                 ViewState["iu"] = value;\r
66                         }\r
67                 }\r
68                 \r
69                 public bool AllowCallback {\r
70                         get {\r
71                                 object o = ViewState["ac"];\r
72                                 if (o != null) return (bool) o;\r
73                                 return true;\r
74                         }\r
75                         set {\r
76                                 ViewState["ac"] = value;\r
77                         }\r
78                 }\r
79                 \r
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 override void Render (HtmlTextWriter writer)\r
92                 {\r
93                         if (CommandName.Length > 0 || ButtonType == ButtonType.Button)\r
94                         {\r
95                                 string postScript = null;\r
96                                 string callScript = null;\r
97                                 \r
98                                 IPostBackContainer pcner = container as IPostBackContainer;
99                                 if (pcner != null) {\r
100                                         PostBackOptions ops = pcner.GetPostBackOptions (this);\r
101                                         postScript = container.Page.ClientScript.GetPostBackEventReference (ops);\r
102                                 } else\r
103                                         postScript = Page.ClientScript.GetPostBackClientEvent (this, "");\r
104 \r
105                                 if (CausesValidation && Page.Validators.Count > 0) {\r
106                                         postScript = Utils.GetClientValidatedEvent (Page) + postScript;\r
107                                 }\r
108                                 \r
109                                 if (AllowCallback) {\r
110                                         ICallbackContainer ccner = container as ICallbackContainer;\r
111                                         if (ccner != null)\r
112                                                 callScript = ccner.GetCallbackScript (this, CommandName + "$" + CommandArgument);\r
113                                 }\r
114                         
115                                 if (ButtonType == ButtonType.Link || ButtonType == ButtonType.Image)
116                                 {\r
117                                         if (ImageUrl.Length > 0) {\r
118                                                 writer.AddAttribute (HtmlTextWriterAttribute.Type, "image");\r
119                                                 writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (ImageUrl));\r
120                                                 if (callScript != null)\r
121                                                         writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);\r
122                                                 else\r
123                                                         writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);\r
124                                                 if (Text.Length > 0)\r
125                                                         writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);\r
126                                                 writer.RenderBeginTag (HtmlTextWriterTag.Input);\r
127                                                 writer.RenderEndTag ();\r
128                                         }\r
129                                         else {\r
130                                                 if (callScript != null) {\r
131                                                         writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);\r
132                                                         writer.AddAttribute (HtmlTextWriterAttribute.Href, "javascript:");\r
133                                                 }\r
134                                                 else\r
135                                                         writer.AddAttribute (HtmlTextWriterAttribute.Href, postScript);\r
136                                                 writer.RenderBeginTag (HtmlTextWriterTag.A);\r
137                                                 writer.Write (Text);\r
138                                                 writer.RenderEndTag ();\r
139                                         }
140                                 }
141                                 else if (ButtonType == ButtonType.Button)
142                                 {
143                                         if (callScript != null)\r
144                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, callScript);\r
145                                         else\r
146                                                 writer.AddAttribute (HtmlTextWriterAttribute.Onclick, postScript);
147                                                 \r
148                                         writer.AddAttribute (HtmlTextWriterAttribute.Type, "submit");\r
149                                         writer.AddAttribute (HtmlTextWriterAttribute.Name, ClientID);\r
150                                         writer.AddAttribute (HtmlTextWriterAttribute.Value, Text);\r
151                                         writer.RenderBeginTag (HtmlTextWriterTag.Input);\r
152                                         writer.RenderEndTag ();\r
153                                 }\r
154                         } else {\r
155                                 if (ImageUrl.Length > 0) {\r
156                                         writer.AddAttribute (HtmlTextWriterAttribute.Src, ResolveUrl (ImageUrl));\r
157                                         if (Text.Length > 0)\r
158                                                 writer.AddAttribute (HtmlTextWriterAttribute.Alt, Text);\r
159                                         writer.RenderBeginTag (HtmlTextWriterTag.Img);\r
160                                         writer.RenderEndTag ();\r
161                                 }\r
162                                 else {\r
163                                         writer.Write (Text);\r
164                                 }
165                         }\r
166                 }\r
167         }\r
168 }\r
169 \r
170 #endif\r