[bcl] Remove NET_4_0 defines from class libs.
[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-2010 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         [SupportsEventValidation]
45         [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
46         public class LinkButton : WebControl, IPostBackEventHandler, IButtonControl
47         {
48                 public LinkButton () : base (HtmlTextWriterTag.A) 
49                 {
50                 }
51         
52                 protected override void AddAttributesToRender (HtmlTextWriter w)
53                 {
54                         Page page = Page;
55                         if (page != null)
56                                 page.VerifyRenderingInServerForm (this);
57
58                         base.AddAttributesToRender (w);
59                         bool enabled = IsEnabled;
60                         string onclick = OnClientClick;
61                         onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick);
62                         if (HasAttributes && Attributes ["onclick"] != null) {
63                                 onclick = ClientScriptManager.EnsureEndsWithSemicolon (onclick + Attributes ["onclick"]);
64                                 Attributes.Remove ("onclick");
65                         }
66
67                         if (onclick.Length > 0)
68                                 w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
69                         
70                         if (enabled && page != null) {
71                                 PostBackOptions options = GetPostBackOptions ();
72                                 string href = page.ClientScript.GetPostBackEventReference (options, true);
73                                 w.AddAttribute (HtmlTextWriterAttribute.Href, href);
74                         }
75                         
76                         AddDisplayStyleAttribute (w);
77                 }
78
79                 protected virtual void RaisePostBackEvent (string eventArgument)
80                 {
81                         ValidateEvent (UniqueID, eventArgument);
82                         if (CausesValidation) {
83                                 Page page = Page;
84                                 if (page != null)
85                                         page.Validate (ValidationGroup);
86                         }
87                         
88                         OnClick (EventArgs.Empty);
89                         OnCommand (new CommandEventArgs (CommandName, CommandArgument));
90                 }
91                 
92                 void IPostBackEventHandler.RaisePostBackEvent (string ea)
93                 {
94                         RaisePostBackEvent (ea);
95                 }
96
97                 protected override void AddParsedSubObject (object obj)
98                 {
99                         if (HasControls ()) {
100                                 base.AddParsedSubObject (obj);
101                                 return;
102                         }
103                         
104                         LiteralControl lc = obj as LiteralControl;
105
106                         if (lc == null) {
107                                 string s = Text;
108                                 if (s.Length != 0) {
109                                         Text = null;
110                                         Controls.Add (new LiteralControl (s));
111                                 }
112                                 base.AddParsedSubObject (obj);
113                         } else
114                                 Text = lc.Text;
115                 }
116
117                 protected virtual PostBackOptions GetPostBackOptions ()
118                 {
119                         PostBackOptions options = new PostBackOptions (this);
120                         Page page = Page;
121                         
122                         options.ActionUrl = (PostBackUrl.Length > 0 ?
123                                              page != null ? page.ResolveClientUrl (PostBackUrl) : PostBackUrl
124                                              : null);
125                         options.ValidationGroup = null;
126                         options.Argument = String.Empty;
127                         options.ClientSubmit = true;
128                         options.RequiresJavaScriptProtocol = true;
129                         options.PerformValidation = CausesValidation && page != null && page.AreValidatorsUplevel (ValidationGroup);
130                         if (options.PerformValidation)
131                                 options.ValidationGroup = ValidationGroup;
132
133                         return options;
134                 }
135
136                 protected override void LoadViewState (object savedState)
137                 {
138                         base.LoadViewState (savedState);
139
140                         // Make sure we clear child controls when this happens
141                         if (ViewState ["Text"] != null)
142                                 Text = (string) ViewState ["Text"];
143                 }
144
145                 [MonoTODO ("Why override?")]
146                 protected internal override void OnPreRender (EventArgs e)
147                 {
148                         base.OnPreRender (e);
149                 }
150         
151                 protected internal override void RenderContents (HtmlTextWriter writer)
152                 {
153                         if (HasControls () || HasRenderMethodDelegate ())
154                                 base.RenderContents (writer);
155                         else
156                                 writer.Write (Text);
157                 }
158
159                 [DefaultValue(true)]
160                 [WebSysDescription ("")]
161                 [WebCategory ("Behavior")]
162                 [Themeable (false)]
163                 public virtual bool CausesValidation {
164                         get { return ViewState.GetBool ("CausesValidation", true); }
165                         set { ViewState ["CausesValidation"] = value; }
166                 
167                 }
168
169                 [Bindable(true)]
170                 [DefaultValue("")]
171                 [WebSysDescription ("")]
172                 [WebCategory ("Behavior")]
173                 [Themeable (false)]
174                 public string CommandArgument {
175                         get { return ViewState.GetString ("CommandArgument", String.Empty); }
176                         set { ViewState ["CommandArgument"] = value; }
177                 }
178
179                 [DefaultValue("")]
180                 [WebSysDescription ("")]
181                 [WebCategory ("Behavior")]
182                 [Themeable (false)]
183                 public string CommandName {
184                         get { return ViewState.GetString ("CommandName", String.Empty); }
185                         set { ViewState ["CommandName"] = value; }
186                 
187                 }
188
189                 [DefaultValue ("")]
190                 [Themeable (false)]
191                 [WebSysDescription ("")]
192                 [WebCategoryAttribute ("Behavior")]
193                 public virtual string OnClientClick
194                 {
195                         get { return ViewState.GetString ("OnClientClick", String.Empty); }
196                         set { ViewState ["OnClientClick"] = value; }
197                 }
198
199                 [Bindable(true)]
200                 [DefaultValue("")]
201                 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
202                 [Localizable (true)]
203                 [WebSysDescription ("")]
204                 [WebCategory ("Appearance")]
205                 public virtual string Text {
206                         get { return ViewState.GetString ("Text", String.Empty); }
207                         set {
208                                 ViewState ["Text"] = value;
209                                 if (HasControls ())
210                                         Controls.Clear ();
211                         }
212                 }
213
214                 protected virtual void OnClick (EventArgs e)
215                 {
216                         EventHandler h = (EventHandler) Events [ClickEvent];
217                         if (h != null)
218                                 h (this, e);
219                 }
220                 static readonly object ClickEvent = new object ();
221
222                 [WebSysDescription ("")]
223                 [WebCategory ("Action")]
224                 public event EventHandler Click {
225                         add { Events.AddHandler (ClickEvent, value); }
226                         remove { Events.RemoveHandler (ClickEvent, value); }
227                 }
228
229                 protected virtual void OnCommand (CommandEventArgs e)
230                 {
231                         CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
232                         if (h != null)
233                                 h (this, e);
234
235                         RaiseBubbleEvent (this, e);
236                 }
237                 static readonly object CommandEvent = new object ();
238
239                 [WebSysDescription ("")]
240                 [WebCategory ("Action")]
241                 public event CommandEventHandler Command {
242                         add { Events.AddHandler (CommandEvent, value); }
243                         remove { Events.RemoveHandler (CommandEvent, value); }
244                 }
245
246                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
247                 [Themeable (false)]
248                 [UrlProperty ("*.aspx")]
249                 [DefaultValue ("")]
250                 public virtual string PostBackUrl {
251                         get { return ViewState.GetString ("PostBackUrl", String.Empty); }
252                         set { ViewState["PostBackUrl"] = value; }
253                 }
254
255                 [DefaultValue ("")]
256                 [Themeable (false)]
257                 [WebSysDescription ("")]
258                 [WebCategoryAttribute ("Behavior")]
259                 public virtual string ValidationGroup {
260                         get { return ViewState.GetString ("ValidationGroup", String.Empty); }
261                         set { ViewState ["ValidationGroup"] = value; }
262                 }
263                 public override bool SupportsDisabledAttribute {
264                         get { return RenderingCompatibilityLessThan40; }
265                 }
266         }
267 }
268
269