merge -r 58784:58785
[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                                 w.AddAttribute ("language", "javascript");
76                         } else {
77                                 w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
78                         }
79                 }
80
81 #if NET_2_0
82                 [MonoTODO]
83                 protected virtual void RaisePostBackEvent (string eventArgument)
84                 {
85                         throw new NotImplementedException ();
86                 }
87 #endif          
88                 
89                 void IPostBackEventHandler.RaisePostBackEvent (string ea)
90                 {
91                         if (CausesValidation)
92 #if NET_2_0
93                                 Page.Validate (ValidationGroup);
94 #else
95                                 Page.Validate ();
96 #endif
97                         
98                         OnClick (EventArgs.Empty);
99                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
100                 }
101
102                 protected override void AddParsedSubObject (object obj)
103                 {
104                         if (HasControls ()) {
105                                 base.AddParsedSubObject (obj);
106                                 return;
107                         }
108                         
109                         LiteralControl lc = obj as LiteralControl;
110
111                         if (lc == null) {
112                                 string s = Text;
113                                 if (s.Length != 0) {
114                                         Text = null;
115                                         Controls.Add (new LiteralControl (s));
116                                 }
117                                 base.AddParsedSubObject (obj);
118                         } else {
119                                 Text = lc.Text;
120                         }
121                 }
122
123 #if NET_2_0
124                 [MonoTODO]
125                 protected virtual PostBackOptions GetPostBackOptions ()
126                 {
127                         throw new NotImplementedException ();
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                 public virtual
194 #else           
195                 public
196 #endif          
197                 string CommandArgument {
198                         get {
199                                 return ViewState.GetString ("CommandArgument", "");
200                         }
201                         set {
202                                 ViewState ["CommandArgument"] = value;
203                         }
204                 }
205
206                 [DefaultValue("")]
207                 [WebSysDescription ("")]
208                 [WebCategory ("Behavior")]
209 #if NET_2_0
210                 [Themeable (false)]
211                 public virtual
212 #else           
213                 public
214 #endif          
215                 string CommandName {
216                         get {
217                                 return ViewState.GetString ("CommandName", ""); 
218                         }
219                 
220                         set {
221                                 ViewState ["CommandName"] = value;
222                         }
223                 
224                 }
225
226 #if NET_2_0
227                 [DefaultValue ("")]
228                 [Themeable (false)]
229                 [MonoTODO]
230                 [WebSysDescription ("")]
231                 [WebCategoryAttribute ("Behavior")]
232                 public virtual string OnClientClick
233                 {
234                         get {
235                                 throw new NotImplementedException ();
236                         }
237                         set {
238                                 throw new NotImplementedException ();
239                         }
240                 }
241
242 #endif          
243
244                 [Bindable(true)]
245                 [DefaultValue("")]
246                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
247 #if NET_2_0
248                 [Localizable (true)]
249 #endif
250                 [WebSysDescription ("")]
251                 [WebCategory ("Appearance")]
252                 public virtual string Text {
253                         get {
254                                 return ViewState.GetString ("Text", "");        
255                         }
256                         set {
257                                 ViewState ["Text"] = value;     
258                                 if (HasControls ())
259                                         Controls.Clear ();
260                         }
261                 }
262
263                 protected virtual void OnClick (EventArgs e)
264                 {
265                         EventHandler h = (EventHandler) Events [ClickEvent];
266                         if (h != null)
267                                 h (this, e);
268                 }
269                 static readonly object ClickEvent = new object ();
270
271                 [WebSysDescription ("")]
272                 [WebCategory ("Action")]
273                 public event EventHandler Click {
274                         add { Events.AddHandler (ClickEvent, value); }
275                         remove { Events.RemoveHandler (ClickEvent, value); }
276                 }
277
278                 protected virtual void OnCommand (CommandEventArgs e)
279                 {
280                         CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
281                         if (h != null)
282                                 h (this, e);
283
284                         RaiseBubbleEvent (this, e);
285                 }
286                 static readonly object CommandEvent = new object ();
287
288                 [WebSysDescription ("")]
289                 [WebCategory ("Action")]
290                 public event CommandEventHandler Command {
291                         add { Events.AddHandler (CommandEvent, value); }
292                         remove { Events.RemoveHandler (CommandEvent, value); }
293                 }
294 #if NET_2_0
295                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
296                 [Themeable (false)]
297                 [UrlProperty ("*.aspx")]
298                 [DefaultValue ("")]
299                 [MonoTODO]
300                 public string PostBackUrl {
301                         get {
302                                 throw new NotImplementedException ();
303                         }
304                         set {
305                                 throw new NotImplementedException ();
306                         }
307                 }
308
309                 [DefaultValue ("")]
310                 [Themeable (false)]
311                 [WebSysDescription ("")]
312                 [WebCategoryAttribute ("Behavior")]
313                 public string ValidationGroup {
314                         get {
315                                 return ViewState.GetString ("ValidationGroup", "");     
316                         }
317                         set {
318                                 ViewState ["ValidationGroup"] = value;  
319                         }
320                 }       
321 #endif
322         }
323 }
324