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);
84 base.AddAttributesToRender (w);
85 if (Page == null || !Enabled)
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)));
94 w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
102 void RaisePostBackEvent (string eventArgument)
104 if (CausesValidation)
106 Page.Validate (ValidationGroup);
111 OnClick (EventArgs.Empty);
112 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
115 void IPostBackEventHandler.RaisePostBackEvent (string ea)
117 RaisePostBackEvent (ea);
120 protected override void AddParsedSubObject (object obj)
122 if (HasControls ()) {
123 base.AddParsedSubObject (obj);
127 LiteralControl lc = obj as LiteralControl;
133 Controls.Add (new LiteralControl (s));
135 base.AddParsedSubObject (obj);
142 protected virtual PostBackOptions GetPostBackOptions ()
144 PostBackOptions options = new PostBackOptions (this);
145 options.ActionUrl = (PostBackUrl.Length > 0 ? Page.ResolveClientUrl (PostBackUrl) : null);
147 vmw.@internal.j2ee.IPortletRenderResponse resp = GetRenderResponse();
148 if (resp != null && options.ActionUrl != null)
149 options.ActionUrl = resp.createActionURL(options.ActionUrl);
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;
163 protected override void LoadViewState (object savedState)
165 base.LoadViewState (savedState);
167 // Make sure we clear child controls when this happens
168 if (ViewState ["Text"] != null)
169 Text = (string) ViewState ["Text"];
172 [MonoTODO ("Why override?")]
178 override void OnPreRender (EventArgs e)
180 base.OnPreRender (e);
188 override void RenderContents (HtmlTextWriter writer)
191 base.RenderContents (writer);
201 [WebSysDescription ("")]
202 [WebCategory ("Behavior")]
209 bool CausesValidation {
211 return ViewState.GetBool ("CausesValidation", true);
214 ViewState ["CausesValidation"] = value;
221 [WebSysDescription ("")]
222 [WebCategory ("Behavior")]
226 public string CommandArgument {
228 return ViewState.GetString ("CommandArgument", "");
231 ViewState ["CommandArgument"] = value;
236 [WebSysDescription ("")]
237 [WebCategory ("Behavior")]
241 public string CommandName {
243 return ViewState.GetString ("CommandName", "");
247 ViewState ["CommandName"] = value;
255 [WebSysDescription ("")]
256 [WebCategoryAttribute ("Behavior")]
257 public virtual string OnClientClick
260 return ViewState.GetString ("OnClientClick", "");
263 ViewState ["OnClientClick"] = value;
271 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
275 [WebSysDescription ("")]
276 [WebCategory ("Appearance")]
277 public virtual string Text {
279 return ViewState.GetString ("Text", "");
282 ViewState ["Text"] = value;
288 protected virtual void OnClick (EventArgs e)
290 EventHandler h = (EventHandler) Events [ClickEvent];
294 static readonly object ClickEvent = new object ();
296 [WebSysDescription ("")]
297 [WebCategory ("Action")]
298 public event EventHandler Click {
299 add { Events.AddHandler (ClickEvent, value); }
300 remove { Events.RemoveHandler (ClickEvent, value); }
303 protected virtual void OnCommand (CommandEventArgs e)
305 CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
309 RaiseBubbleEvent (this, e);
311 static readonly object CommandEvent = new object ();
313 [WebSysDescription ("")]
314 [WebCategory ("Action")]
315 public event CommandEventHandler Command {
316 add { Events.AddHandler (CommandEvent, value); }
317 remove { Events.RemoveHandler (CommandEvent, value); }
320 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
322 [UrlProperty ("*.aspx")]
324 public virtual string PostBackUrl {
326 return ViewState.GetString ("PostBackUrl", String.Empty);
329 ViewState["PostBackUrl"] = value;
335 [WebSysDescription ("")]
336 [WebCategoryAttribute ("Behavior")]
337 public virtual string ValidationGroup {
339 return ViewState.GetString ("ValidationGroup", "");
342 ViewState ["ValidationGroup"] = value;