2 // System.Web.UI.WebControls.LinkButton.cs
5 // Ben Maurer (bmaurer@novell.com)
7 // (C) 2005 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)]
45 [SupportsEventValidation]
46 [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
48 [ToolboxData("<{0}:LinkButton runat=server>LinkButton</{0}:LinkButton>")]
50 public class LinkButton : WebControl, IPostBackEventHandler
56 public LinkButton () : base (HtmlTextWriterTag.A)
61 protected override void AddAttributesToRender (HtmlTextWriter w)
64 Page.VerifyRenderingInServerForm (this);
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");
74 if (onclick.Length > 0)
75 w.AddAttribute (HtmlTextWriterAttribute.Onclick, onclick);
77 if (Enabled && Page != null) {
78 PostBackOptions options = GetPostBackOptions ();
79 string href = Page.ClientScript.GetPostBackEventReference (options, true);
80 w.AddAttribute (HtmlTextWriterAttribute.Href, href);
82 base.AddAttributesToRender (w);
83 AddDisplayStyleAttribute (w);
85 base.AddAttributesToRender (w);
86 if (Page == null || !Enabled)
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), ";}"));
95 w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
103 void RaisePostBackEvent (string eventArgument)
105 if (CausesValidation)
107 Page.Validate (ValidationGroup);
112 OnClick (EventArgs.Empty);
113 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
116 void IPostBackEventHandler.RaisePostBackEvent (string ea)
118 RaisePostBackEvent (ea);
121 protected override void AddParsedSubObject (object obj)
123 if (HasControls ()) {
124 base.AddParsedSubObject (obj);
128 LiteralControl lc = obj as LiteralControl;
134 Controls.Add (new LiteralControl (s));
136 base.AddParsedSubObject (obj);
143 protected virtual PostBackOptions GetPostBackOptions ()
145 PostBackOptions options = new PostBackOptions (this);
146 options.ActionUrl = (PostBackUrl.Length > 0 ?
148 CreateActionUrl (PostBackUrl)
150 Page.ResolveClientUrl (PostBackUrl)
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;
165 protected override void LoadViewState (object savedState)
167 base.LoadViewState (savedState);
169 // Make sure we clear child controls when this happens
170 if (ViewState ["Text"] != null)
171 Text = (string) ViewState ["Text"];
174 [MonoTODO ("Why override?")]
180 override void OnPreRender (EventArgs e)
182 base.OnPreRender (e);
190 override void RenderContents (HtmlTextWriter writer)
192 if (HasControls () || HasRenderMethodDelegate ())
193 base.RenderContents (writer);
203 [WebSysDescription ("")]
204 [WebCategory ("Behavior")]
211 bool CausesValidation {
213 return ViewState.GetBool ("CausesValidation", true);
216 ViewState ["CausesValidation"] = value;
223 [WebSysDescription ("")]
224 [WebCategory ("Behavior")]
228 public string CommandArgument {
230 return ViewState.GetString ("CommandArgument", "");
233 ViewState ["CommandArgument"] = value;
238 [WebSysDescription ("")]
239 [WebCategory ("Behavior")]
243 public string CommandName {
245 return ViewState.GetString ("CommandName", "");
249 ViewState ["CommandName"] = value;
257 [WebSysDescription ("")]
258 [WebCategoryAttribute ("Behavior")]
259 public virtual string OnClientClick
262 return ViewState.GetString ("OnClientClick", "");
265 ViewState ["OnClientClick"] = value;
273 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
277 [WebSysDescription ("")]
278 [WebCategory ("Appearance")]
279 public virtual string Text {
281 return ViewState.GetString ("Text", "");
284 ViewState ["Text"] = value;
290 protected virtual void OnClick (EventArgs e)
292 EventHandler h = (EventHandler) Events [ClickEvent];
296 static readonly object ClickEvent = new object ();
298 [WebSysDescription ("")]
299 [WebCategory ("Action")]
300 public event EventHandler Click {
301 add { Events.AddHandler (ClickEvent, value); }
302 remove { Events.RemoveHandler (ClickEvent, value); }
305 protected virtual void OnCommand (CommandEventArgs e)
307 CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
311 RaiseBubbleEvent (this, e);
313 static readonly object CommandEvent = new object ();
315 [WebSysDescription ("")]
316 [WebCategory ("Action")]
317 public event CommandEventHandler Command {
318 add { Events.AddHandler (CommandEvent, value); }
319 remove { Events.RemoveHandler (CommandEvent, value); }
322 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
324 [UrlProperty ("*.aspx")]
326 public virtual string PostBackUrl {
328 return ViewState.GetString ("PostBackUrl", String.Empty);
331 ViewState["PostBackUrl"] = value;
337 [WebSysDescription ("")]
338 [WebCategoryAttribute ("Behavior")]
339 public virtual string ValidationGroup {
341 return ViewState.GetString ("ValidationGroup", "");
344 ViewState ["ValidationGroup"] = value;