2008-03-13 Marek Habersack <mhabersack@novell.com>
[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 #if NET_2_0
67                         string onclick = OnClientClick;
68                         onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
69                         if (Attributes ["onclick"] != null) {
70                                 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
71                                 Attributes.Remove ("onclick");
72                         }
73
74                         if (onclick.Length > 0)
75                                 w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
76
77                         if (Enabled && Page != null) {
78                                 PostBackOptions options = GetPostBackOptions ();
79                                 string href = Page.ClientScript.GetPostBackEventReference (options, true);
80                                 w.AddAttribute (HtmlTextWriterAttribute.Href, href);
81                         }
82                         base.AddAttributesToRender (w);
83                         AddDisplayStyleAttribute (w);
84 #else
85                         base.AddAttributesToRender (w);
86                         if (Page == null || !Enabled)
87                                 return;
88                         
89                         if (CausesValidation && Page.AreValidatorsUplevel ()) {
90                                 ClientScriptManager csm = new ClientScriptManager (Page);
91                                 w.AddAttribute (HtmlTextWriterAttribute.Href,
92                                                 String.Concat ("javascript:{if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); ",
93                                                                csm.GetPostBackEventReference (this, String.Empty), ";}"));
94                         } else {
95                                 w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
96                         }
97 #endif
98                 }
99
100 #if NET_2_0
101                 protected virtual 
102 #endif          
103                 void RaisePostBackEvent (string eventArgument)
104                 {
105                         if (CausesValidation)
106 #if NET_2_0
107                                 Page.Validate (ValidationGroup);
108 #else
109                                 Page.Validate ();
110 #endif
111                         
112                         OnClick (EventArgs.Empty);
113                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
114                 }
115                 
116                 void IPostBackEventHandler.RaisePostBackEvent (string ea)
117                 {
118                         RaisePostBackEvent (ea);
119                 }
120
121                 protected override void AddParsedSubObject (object obj)
122                 {
123                         if (HasControls ()) {
124                                 base.AddParsedSubObject (obj);
125                                 return;
126                         }
127                         
128                         LiteralControl lc = obj as LiteralControl;
129
130                         if (lc == null) {
131                                 string s = Text;
132                                 if (s.Length != 0) {
133                                         Text = null;
134                                         Controls.Add (new LiteralControl (s));
135                                 }
136                                 base.AddParsedSubObject (obj);
137                         } else {
138                                 Text = lc.Text;
139                         }
140                 }
141
142 #if NET_2_0
143                 protected virtual PostBackOptions GetPostBackOptions ()
144                 {
145                         PostBackOptions options = new PostBackOptions (this);
146                         options.ActionUrl = (PostBackUrl.Length > 0 ?
147 #if TARGET_J2EE
148                                 CreateActionUrl (PostBackUrl)
149 #else
150                                 Page.ResolveClientUrl (PostBackUrl) 
151 #endif
152                                 : null);
153                         options.ValidationGroup = null;
154                         options.Argument = "";
155                         options.ClientSubmit = true;
156                         options.RequiresJavaScriptProtocol = true;
157                         options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
158                         if (options.PerformValidation)
159                                 options.ValidationGroup = ValidationGroup;
160
161                         return options;
162                 }
163 #endif          
164
165                 protected override void LoadViewState (object savedState)
166                 {
167                         base.LoadViewState (savedState);
168
169                         // Make sure we clear child controls when this happens
170                         if (ViewState ["Text"] != null)
171                                 Text = (string) ViewState ["Text"];
172                 }
173
174                 [MonoTODO ("Why override?")]
175 #if NET_2_0
176                 protected internal
177 #else           
178                 protected
179 #endif          
180                 override void OnPreRender (EventArgs e)
181                 {
182                         base.OnPreRender (e);
183                 }
184         
185 #if NET_2_0
186                 protected internal
187 #else           
188                 protected
189 #endif          
190                 override void RenderContents (HtmlTextWriter writer)
191                 {
192                         if (HasControls () || HasRenderMethodDelegate ())
193                                 base.RenderContents (writer);
194                         else
195                                 writer.Write (Text);
196                 }
197         
198         
199 #if ONLY_1_1
200                 [Bindable(false)]
201 #endif          
202                 [DefaultValue(true)]
203                 [WebSysDescription ("")]
204                 [WebCategory ("Behavior")]
205 #if NET_2_0
206                 [Themeable (false)]
207                 public virtual
208 #else
209                 public
210 #endif          
211                 bool CausesValidation {
212                         get {
213                                 return ViewState.GetBool ("CausesValidation", true);
214                         }
215                         set {
216                                 ViewState ["CausesValidation"] = value;
217                         }
218                 
219                 }
220
221                 [Bindable(true)]
222                 [DefaultValue("")]
223                 [WebSysDescription ("")]
224                 [WebCategory ("Behavior")]
225 #if NET_2_0
226                 [Themeable (false)]
227 #endif          
228                 public string CommandArgument {
229                         get {
230                                 return ViewState.GetString ("CommandArgument", "");
231                         }
232                         set {
233                                 ViewState ["CommandArgument"] = value;
234                         }
235                 }
236
237                 [DefaultValue("")]
238                 [WebSysDescription ("")]
239                 [WebCategory ("Behavior")]
240 #if NET_2_0
241                 [Themeable (false)]
242 #endif          
243                 public string CommandName {
244                         get {
245                                 return ViewState.GetString ("CommandName", ""); 
246                         }
247                 
248                         set {
249                                 ViewState ["CommandName"] = value;
250                         }
251                 
252                 }
253
254 #if NET_2_0
255                 [DefaultValue ("")]
256                 [Themeable (false)]
257                 [WebSysDescription ("")]
258                 [WebCategoryAttribute ("Behavior")]
259                 public virtual string OnClientClick
260                 {
261                         get {
262                                 return ViewState.GetString ("OnClientClick", "");
263                         }
264                         set {
265                                 ViewState ["OnClientClick"] = value;
266                         }
267                 }
268
269 #endif          
270
271                 [Bindable(true)]
272                 [DefaultValue("")]
273                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
274 #if NET_2_0
275                 [Localizable (true)]
276 #endif
277                 [WebSysDescription ("")]
278                 [WebCategory ("Appearance")]
279                 public virtual string Text {
280                         get {
281                                 return ViewState.GetString ("Text", "");        
282                         }
283                         set {
284                                 ViewState ["Text"] = value;     
285                                 if (HasControls ())
286                                         Controls.Clear ();
287                         }
288                 }
289
290                 protected virtual void OnClick (EventArgs e)
291                 {
292                         EventHandler h = (EventHandler) Events [ClickEvent];
293                         if (h != null)
294                                 h (this, e);
295                 }
296                 static readonly object ClickEvent = new object ();
297
298                 [WebSysDescription ("")]
299                 [WebCategory ("Action")]
300                 public event EventHandler Click {
301                         add { Events.AddHandler (ClickEvent, value); }
302                         remove { Events.RemoveHandler (ClickEvent, value); }
303                 }
304
305                 protected virtual void OnCommand (CommandEventArgs e)
306                 {
307                         CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
308                         if (h != null)
309                                 h (this, e);
310
311                         RaiseBubbleEvent (this, e);
312                 }
313                 static readonly object CommandEvent = new object ();
314
315                 [WebSysDescription ("")]
316                 [WebCategory ("Action")]
317                 public event CommandEventHandler Command {
318                         add { Events.AddHandler (CommandEvent, value); }
319                         remove { Events.RemoveHandler (CommandEvent, value); }
320                 }
321 #if NET_2_0
322                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
323                 [Themeable (false)]
324                 [UrlProperty ("*.aspx")]
325                 [DefaultValue ("")]
326                 public virtual string PostBackUrl {
327                         get {
328                                 return ViewState.GetString ("PostBackUrl", String.Empty);
329                         }
330                         set {
331                                 ViewState["PostBackUrl"] = value;
332                         }
333                 }
334
335                 [DefaultValue ("")]
336                 [Themeable (false)]
337                 [WebSysDescription ("")]
338                 [WebCategoryAttribute ("Behavior")]
339                 public virtual string ValidationGroup {
340                         get {
341                                 return ViewState.GetString ("ValidationGroup", "");     
342                         }
343                         set {
344                                 ViewState ["ValidationGroup"] = value;  
345                         }
346                 }       
347 #endif
348         }
349 }
350
351