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);
146 options.ValidationGroup = null;
147 options.Argument = "";
148 options.ClientSubmit = true;
149 options.RequiresJavaScriptProtocol = true;
150 options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
151 if (options.PerformValidation)
152 options.ValidationGroup = ValidationGroup;
158 protected override void LoadViewState (object savedState)
160 base.LoadViewState (savedState);
162 // Make sure we clear child controls when this happens
163 if (ViewState ["Text"] != null)
164 Text = (string) ViewState ["Text"];
167 [MonoTODO ("Why override?")]
173 override void OnPreRender (EventArgs e)
175 base.OnPreRender (e);
183 override void RenderContents (HtmlTextWriter writer)
186 base.RenderContents (writer);
196 [WebSysDescription ("")]
197 [WebCategory ("Behavior")]
204 bool CausesValidation {
206 return ViewState.GetBool ("CausesValidation", true);
209 ViewState ["CausesValidation"] = value;
216 [WebSysDescription ("")]
217 [WebCategory ("Behavior")]
221 public string CommandArgument {
223 return ViewState.GetString ("CommandArgument", "");
226 ViewState ["CommandArgument"] = value;
231 [WebSysDescription ("")]
232 [WebCategory ("Behavior")]
236 public string CommandName {
238 return ViewState.GetString ("CommandName", "");
242 ViewState ["CommandName"] = value;
250 [WebSysDescription ("")]
251 [WebCategoryAttribute ("Behavior")]
252 public virtual string OnClientClick
255 return ViewState.GetString ("OnClientClick", "");
258 ViewState ["OnClientClick"] = value;
266 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
270 [WebSysDescription ("")]
271 [WebCategory ("Appearance")]
272 public virtual string Text {
274 return ViewState.GetString ("Text", "");
277 ViewState ["Text"] = value;
283 protected virtual void OnClick (EventArgs e)
285 EventHandler h = (EventHandler) Events [ClickEvent];
289 static readonly object ClickEvent = new object ();
291 [WebSysDescription ("")]
292 [WebCategory ("Action")]
293 public event EventHandler Click {
294 add { Events.AddHandler (ClickEvent, value); }
295 remove { Events.RemoveHandler (ClickEvent, value); }
298 protected virtual void OnCommand (CommandEventArgs e)
300 CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
304 RaiseBubbleEvent (this, e);
306 static readonly object CommandEvent = new object ();
308 [WebSysDescription ("")]
309 [WebCategory ("Action")]
310 public event CommandEventHandler Command {
311 add { Events.AddHandler (CommandEvent, value); }
312 remove { Events.RemoveHandler (CommandEvent, value); }
315 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
317 [UrlProperty ("*.aspx")]
319 public virtual string PostBackUrl {
321 return ViewState.GetString ("PostBackUrl", String.Empty);
324 ViewState["PostBackUrl"] = value;
330 [WebSysDescription ("")]
331 [WebCategoryAttribute ("Behavior")]
332 public virtual string ValidationGroup {
334 return ViewState.GetString ("ValidationGroup", "");
337 ViewState ["ValidationGroup"] = value;