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