2 // System.Web.UI.WebControls.LinkButton.cs
5 // Ben Maurer (bmaurer@novell.com)
7 // (C) 2005-2010 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
30 using System.Security.Permissions;
32 namespace System.Web.UI.WebControls
35 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
36 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
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
48 public LinkButton () : base (HtmlTextWriterTag.A)
52 protected override void AddAttributesToRender (HtmlTextWriter w)
56 page.VerifyRenderingInServerForm (this);
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");
67 if (onclick.Length > 0)
68 w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
70 if (enabled && page != null) {
71 PostBackOptions options = GetPostBackOptions ();
72 string href = page.ClientScript.GetPostBackEventReference (options, true);
73 w.AddAttribute (HtmlTextWriterAttribute.Href, href);
76 AddDisplayStyleAttribute (w);
79 protected virtual void RaisePostBackEvent (string eventArgument)
81 ValidateEvent (UniqueID, eventArgument);
82 if (CausesValidation) {
85 page.Validate (ValidationGroup);
88 OnClick (EventArgs.Empty);
89 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
92 void IPostBackEventHandler.RaisePostBackEvent (string ea)
94 RaisePostBackEvent (ea);
97 protected override void AddParsedSubObject (object obj)
100 base.AddParsedSubObject (obj);
104 LiteralControl lc = obj as LiteralControl;
110 Controls.Add (new LiteralControl (s));
112 base.AddParsedSubObject (obj);
117 protected virtual PostBackOptions GetPostBackOptions ()
119 PostBackOptions options = new PostBackOptions (this);
122 options.ActionUrl = (PostBackUrl.Length > 0 ?
123 page != null ? page.ResolveClientUrl (PostBackUrl) : PostBackUrl
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;
136 protected override void LoadViewState (object savedState)
138 base.LoadViewState (savedState);
140 // Make sure we clear child controls when this happens
141 if (ViewState ["Text"] != null)
142 Text = (string) ViewState ["Text"];
145 [MonoTODO ("Why override?")]
146 protected internal override void OnPreRender (EventArgs e)
148 base.OnPreRender (e);
151 protected internal override void RenderContents (HtmlTextWriter writer)
153 if (HasControls () || HasRenderMethodDelegate ())
154 base.RenderContents (writer);
160 [WebSysDescription ("")]
161 [WebCategory ("Behavior")]
163 public virtual bool CausesValidation {
164 get { return ViewState.GetBool ("CausesValidation", true); }
165 set { ViewState ["CausesValidation"] = value; }
171 [WebSysDescription ("")]
172 [WebCategory ("Behavior")]
174 public string CommandArgument {
175 get { return ViewState.GetString ("CommandArgument", String.Empty); }
176 set { ViewState ["CommandArgument"] = value; }
180 [WebSysDescription ("")]
181 [WebCategory ("Behavior")]
183 public string CommandName {
184 get { return ViewState.GetString ("CommandName", String.Empty); }
185 set { ViewState ["CommandName"] = value; }
191 [WebSysDescription ("")]
192 [WebCategoryAttribute ("Behavior")]
193 public virtual string OnClientClick
195 get { return ViewState.GetString ("OnClientClick", String.Empty); }
196 set { ViewState ["OnClientClick"] = value; }
201 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
203 [WebSysDescription ("")]
204 [WebCategory ("Appearance")]
205 public virtual string Text {
206 get { return ViewState.GetString ("Text", String.Empty); }
208 ViewState ["Text"] = value;
214 protected virtual void OnClick (EventArgs e)
216 EventHandler h = (EventHandler) Events [ClickEvent];
220 static readonly object ClickEvent = new object ();
222 [WebSysDescription ("")]
223 [WebCategory ("Action")]
224 public event EventHandler Click {
225 add { Events.AddHandler (ClickEvent, value); }
226 remove { Events.RemoveHandler (ClickEvent, value); }
229 protected virtual void OnCommand (CommandEventArgs e)
231 CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
235 RaiseBubbleEvent (this, e);
237 static readonly object CommandEvent = new object ();
239 [WebSysDescription ("")]
240 [WebCategory ("Action")]
241 public event CommandEventHandler Command {
242 add { Events.AddHandler (CommandEvent, value); }
243 remove { Events.RemoveHandler (CommandEvent, value); }
246 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
248 [UrlProperty ("*.aspx")]
250 public virtual string PostBackUrl {
251 get { return ViewState.GetString ("PostBackUrl", String.Empty); }
252 set { ViewState["PostBackUrl"] = value; }
257 [WebSysDescription ("")]
258 [WebCategoryAttribute ("Behavior")]
259 public virtual string ValidationGroup {
260 get { return ViewState.GetString ("ValidationGroup", String.Empty); }
261 set { ViewState ["ValidationGroup"] = value; }
263 public override bool SupportsDisabledAttribute {
264 get { return RenderingCompatibilityLessThan40; }