merge -r 60814:60815
[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                 [MonoTODO]
124                 protected virtual PostBackOptions GetPostBackOptions ()
125                 {
126                         throw new NotImplementedException ();
127                 }
128 #endif          
129
130                 protected override void LoadViewState (object savedState)
131                 {
132                         base.LoadViewState (savedState);
133
134                         // Make sure we clear child controls when this happens
135                         if (ViewState ["Text"] != null)
136                                 Text = (string) ViewState ["Text"];
137                 }
138
139                 [MonoTODO ("Why override?")]
140 #if NET_2_0
141                 protected internal
142 #else           
143                 protected
144 #endif          
145                 override void OnPreRender (EventArgs e)
146                 {
147                         base.OnPreRender (e);
148                 }
149         
150 #if NET_2_0
151                 protected internal
152 #else           
153                 protected
154 #endif          
155                 override void RenderContents (HtmlTextWriter writer)
156                 {
157                         if (HasControls ())
158                                 base.RenderContents (writer);
159                         else
160                                 writer.Write (Text);
161                 }
162         
163         
164 #if ONLY_1_1
165                 [Bindable(false)]
166 #endif          
167                 [DefaultValue(true)]
168                 [WebSysDescription ("")]
169                 [WebCategory ("Behavior")]
170 #if NET_2_0
171                 [Themeable (false)]
172                 public virtual
173 #else
174                 public
175 #endif          
176                 bool CausesValidation {
177                         get {
178                                 return ViewState.GetBool ("CausesValidation", true);
179                         }
180                         set {
181                                 ViewState ["CausesValidation"] = value;
182                         }
183                 
184                 }
185
186                 [Bindable(true)]
187                 [DefaultValue("")]
188                 [WebSysDescription ("")]
189                 [WebCategory ("Behavior")]
190 #if NET_2_0
191                 [Themeable (false)]
192 #endif          
193                 public string CommandArgument {
194                         get {
195                                 return ViewState.GetString ("CommandArgument", "");
196                         }
197                         set {
198                                 ViewState ["CommandArgument"] = value;
199                         }
200                 }
201
202                 [DefaultValue("")]
203                 [WebSysDescription ("")]
204                 [WebCategory ("Behavior")]
205 #if NET_2_0
206                 [Themeable (false)]
207 #endif          
208                 public string CommandName {
209                         get {
210                                 return ViewState.GetString ("CommandName", ""); 
211                         }
212                 
213                         set {
214                                 ViewState ["CommandName"] = value;
215                         }
216                 
217                 }
218
219 #if NET_2_0
220                 [DefaultValue ("")]
221                 [Themeable (false)]
222                 [MonoTODO]
223                 [WebSysDescription ("")]
224                 [WebCategoryAttribute ("Behavior")]
225                 public virtual string OnClientClick
226                 {
227                         get {
228                                 throw new NotImplementedException ();
229                         }
230                         set {
231                                 throw new NotImplementedException ();
232                         }
233                 }
234
235 #endif          
236
237                 [Bindable(true)]
238                 [DefaultValue("")]
239                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
240 #if NET_2_0
241                 [Localizable (true)]
242 #endif
243                 [WebSysDescription ("")]
244                 [WebCategory ("Appearance")]
245                 public virtual string Text {
246                         get {
247                                 return ViewState.GetString ("Text", "");        
248                         }
249                         set {
250                                 ViewState ["Text"] = value;     
251                                 if (HasControls ())
252                                         Controls.Clear ();
253                         }
254                 }
255
256                 protected virtual void OnClick (EventArgs e)
257                 {
258                         EventHandler h = (EventHandler) Events [ClickEvent];
259                         if (h != null)
260                                 h (this, e);
261                 }
262                 static readonly object ClickEvent = new object ();
263
264                 [WebSysDescription ("")]
265                 [WebCategory ("Action")]
266                 public event EventHandler Click {
267                         add { Events.AddHandler (ClickEvent, value); }
268                         remove { Events.RemoveHandler (ClickEvent, value); }
269                 }
270
271                 protected virtual void OnCommand (CommandEventArgs e)
272                 {
273                         CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
274                         if (h != null)
275                                 h (this, e);
276
277                         RaiseBubbleEvent (this, e);
278                 }
279                 static readonly object CommandEvent = new object ();
280
281                 [WebSysDescription ("")]
282                 [WebCategory ("Action")]
283                 public event CommandEventHandler Command {
284                         add { Events.AddHandler (CommandEvent, value); }
285                         remove { Events.RemoveHandler (CommandEvent, value); }
286                 }
287 #if NET_2_0
288                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
289                 [Themeable (false)]
290                 [UrlProperty ("*.aspx")]
291                 [DefaultValue ("")]
292                 [MonoTODO]
293                 public virtual string PostBackUrl {
294                         get {
295                                 throw new NotImplementedException ();
296                         }
297                         set {
298                                 throw new NotImplementedException ();
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