refactoring
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / LinkButton.cs
1 //
2 // System.Web.UI.WebControls.LinkButton.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@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 using System.ComponentModel;
30 using System.Security.Permissions;
31
32 namespace System.Web.UI.WebControls {
33
34         // CAS
35         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
37         // attributes
38         [ControlBuilder(typeof(LinkButtonControlBuilder))]
39         [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
40         [DefaultEvent("Click")]
41         [DefaultProperty("Text")]
42         [Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43         [ParseChildren(false)]
44 #if NET_2_0
45         [SupportsEventValidation]
46         [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
47 #else           
48         [ToolboxData("<{0}:LinkButton runat=server>LinkButton</{0}:LinkButton>")]
49 #endif          
50         public class LinkButton : WebControl, IPostBackEventHandler
51 #if NET_2_0
52         , IButtonControl
53 #endif
54         {
55         
56                 public LinkButton () : base (HtmlTextWriterTag.A) 
57                 {
58                 }
59         
60         
61                 protected override void AddAttributesToRender (HtmlTextWriter w)
62                 {
63                         if (Page != null)
64                                 Page.VerifyRenderingInServerForm (this);
65
66                         base.AddAttributesToRender (w);
67                         if (Page == null || !Enabled)
68                                 return;
69                         
70                         if (CausesValidation && Page.AreValidatorsUplevel ()) {
71                                 ClientScriptManager csm = new ClientScriptManager (Page);
72                                 w.AddAttribute (HtmlTextWriterAttribute.Href,
73                                                 String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) {0};}}",
74                                                                csm.GetPostBackEventReference (this, String.Empty)));
75                         } else {
76                                 w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
77                         }
78                 }
79
80 #if NET_2_0
81                 [MonoTODO]
82                 protected virtual void RaisePostBackEvent (string eventArgument)
83                 {
84                         throw new NotImplementedException ();
85                 }
86 #endif          
87                 
88                 void IPostBackEventHandler.RaisePostBackEvent (string ea)
89                 {
90                         if (CausesValidation)
91 #if NET_2_0
92                                 Page.Validate (ValidationGroup);
93 #else
94                                 Page.Validate ();
95 #endif
96                         
97                         OnClick (EventArgs.Empty);
98                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
99                 }
100
101                 protected override void AddParsedSubObject (object obj)
102                 {
103                         if (HasControls ()) {
104                                 base.AddParsedSubObject (obj);
105                                 return;
106                         }
107                         
108                         LiteralControl lc = obj as LiteralControl;
109
110                         if (lc == null) {
111                                 string s = Text;
112                                 if (s.Length != 0) {
113                                         Text = null;
114                                         Controls.Add (new LiteralControl (s));
115                                 }
116                                 base.AddParsedSubObject (obj);
117                         } else {
118                                 Text = lc.Text;
119                         }
120                 }
121
122 #if NET_2_0
123                 protected virtual PostBackOptions GetPostBackOptions ()
124                 {
125                         return new PostBackOptions(this, PostBackUrl, null, false, true,
126                                                    false, true, CausesValidation,
127                                                    ValidationGroup);
128                 }
129 #endif          
130
131                 protected override void LoadViewState (object savedState)
132                 {
133                         base.LoadViewState (savedState);
134
135                         // Make sure we clear child controls when this happens
136                         if (ViewState ["Text"] != null)
137                                 Text = (string) ViewState ["Text"];
138                 }
139
140                 [MonoTODO ("Why override?")]
141 #if NET_2_0
142                 protected internal
143 #else           
144                 protected
145 #endif          
146                 override void OnPreRender (EventArgs e)
147                 {
148                         base.OnPreRender (e);
149                 }
150         
151 #if NET_2_0
152                 protected internal
153 #else           
154                 protected
155 #endif          
156                 override void RenderContents (HtmlTextWriter writer)
157                 {
158                         if (HasControls ())
159                                 base.RenderContents (writer);
160                         else
161                                 writer.Write (Text);
162                 }
163         
164         
165 #if ONLY_1_1
166                 [Bindable(false)]
167 #endif          
168                 [DefaultValue(true)]
169                 [WebSysDescription ("")]
170                 [WebCategory ("Behavior")]
171 #if NET_2_0
172                 [Themeable (false)]
173                 public virtual
174 #else
175                 public
176 #endif          
177                 bool CausesValidation {
178                         get {
179                                 return ViewState.GetBool ("CausesValidation", true);
180                         }
181                         set {
182                                 ViewState ["CausesValidation"] = value;
183                         }
184                 
185                 }
186
187                 [Bindable(true)]
188                 [DefaultValue("")]
189                 [WebSysDescription ("")]
190                 [WebCategory ("Behavior")]
191 #if NET_2_0
192                 [Themeable (false)]
193 #endif          
194                 public string CommandArgument {
195                         get {
196                                 return ViewState.GetString ("CommandArgument", "");
197                         }
198                         set {
199                                 ViewState ["CommandArgument"] = value;
200                         }
201                 }
202
203                 [DefaultValue("")]
204                 [WebSysDescription ("")]
205                 [WebCategory ("Behavior")]
206 #if NET_2_0
207                 [Themeable (false)]
208 #endif          
209                 public string CommandName {
210                         get {
211                                 return ViewState.GetString ("CommandName", ""); 
212                         }
213                 
214                         set {
215                                 ViewState ["CommandName"] = value;
216                         }
217                 
218                 }
219
220 #if NET_2_0
221                 [DefaultValue ("")]
222                 [Themeable (false)]
223                 [MonoTODO]
224                 [WebSysDescription ("")]
225                 [WebCategoryAttribute ("Behavior")]
226                 public virtual string OnClientClick
227                 {
228                         get {
229                                 throw new NotImplementedException ();
230                         }
231                         set {
232                                 throw new NotImplementedException ();
233                         }
234                 }
235
236 #endif          
237
238                 [Bindable(true)]
239                 [DefaultValue("")]
240                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
241 #if NET_2_0
242                 [Localizable (true)]
243 #endif
244                 [WebSysDescription ("")]
245                 [WebCategory ("Appearance")]
246                 public virtual string Text {
247                         get {
248                                 return ViewState.GetString ("Text", "");        
249                         }
250                         set {
251                                 ViewState ["Text"] = value;     
252                                 if (HasControls ())
253                                         Controls.Clear ();
254                         }
255                 }
256
257                 protected virtual void OnClick (EventArgs e)
258                 {
259                         EventHandler h = (EventHandler) Events [ClickEvent];
260                         if (h != null)
261                                 h (this, e);
262                 }
263                 static readonly object ClickEvent = new object ();
264
265                 [WebSysDescription ("")]
266                 [WebCategory ("Action")]
267                 public event EventHandler Click {
268                         add { Events.AddHandler (ClickEvent, value); }
269                         remove { Events.RemoveHandler (ClickEvent, value); }
270                 }
271
272                 protected virtual void OnCommand (CommandEventArgs e)
273                 {
274                         CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
275                         if (h != null)
276                                 h (this, e);
277
278                         RaiseBubbleEvent (this, e);
279                 }
280                 static readonly object CommandEvent = new object ();
281
282                 [WebSysDescription ("")]
283                 [WebCategory ("Action")]
284                 public event CommandEventHandler Command {
285                         add { Events.AddHandler (CommandEvent, value); }
286                         remove { Events.RemoveHandler (CommandEvent, value); }
287                 }
288 #if NET_2_0
289                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
290                 [Themeable (false)]
291                 [UrlProperty ("*.aspx")]
292                 [DefaultValue ("")]
293                 public virtual string PostBackUrl {
294                         get {
295                                 return ViewState.GetString ("PostBackUrl", String.Empty);
296                         }
297                         set {
298                                 ViewState["PostBackUrl"] = value;
299                         }
300                 }
301
302                 [DefaultValue ("")]
303                 [Themeable (false)]
304                 [WebSysDescription ("")]
305                 [WebCategoryAttribute ("Behavior")]
306                 public virtual string ValidationGroup {
307                         get {
308                                 return ViewState.GetString ("ValidationGroup", "");     
309                         }
310                         set {
311                                 ViewState ["ValidationGroup"] = value;  
312                         }
313                 }       
314 #endif
315         }
316 }
317