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);
66 base.AddAttributesToRender (w);
67 if (Page == null || !Enabled)
70 if (CausesValidation && Page.AreValidatorsUplevel ()) {
71 ClientScriptManager csm = new ClientScriptManager (Page);
72 w.AddAttribute (HtmlTextWriterAttribute.Href,
73 String.Format ("javascript:{{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) {0};}}",
74 csm.GetPostBackEventReference (this, String.Empty)));
76 w.AddAttribute (HtmlTextWriterAttribute.Href, Page.ClientScript.GetPostBackClientHyperlink (this, ""));
82 protected virtual void RaisePostBackEvent (string eventArgument)
84 throw new NotImplementedException ();
88 void IPostBackEventHandler.RaisePostBackEvent (string ea)
92 Page.Validate (ValidationGroup);
97 OnClick (EventArgs.Empty);
98 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
101 protected override void AddParsedSubObject (object obj)
103 if (HasControls ()) {
104 base.AddParsedSubObject (obj);
108 LiteralControl lc = obj as LiteralControl;
114 Controls.Add (new LiteralControl (s));
116 base.AddParsedSubObject (obj);
123 protected virtual PostBackOptions GetPostBackOptions ()
125 return new PostBackOptions(this, PostBackUrl, null, false, true,
126 false, true, CausesValidation,
131 protected override void LoadViewState (object savedState)
133 base.LoadViewState (savedState);
135 // Make sure we clear child controls when this happens
136 if (ViewState ["Text"] != null)
137 Text = (string) ViewState ["Text"];
140 [MonoTODO ("Why override?")]
146 override void OnPreRender (EventArgs e)
148 base.OnPreRender (e);
156 override void RenderContents (HtmlTextWriter writer)
159 base.RenderContents (writer);
169 [WebSysDescription ("")]
170 [WebCategory ("Behavior")]
177 bool CausesValidation {
179 return ViewState.GetBool ("CausesValidation", true);
182 ViewState ["CausesValidation"] = value;
189 [WebSysDescription ("")]
190 [WebCategory ("Behavior")]
194 public string CommandArgument {
196 return ViewState.GetString ("CommandArgument", "");
199 ViewState ["CommandArgument"] = value;
204 [WebSysDescription ("")]
205 [WebCategory ("Behavior")]
209 public string CommandName {
211 return ViewState.GetString ("CommandName", "");
215 ViewState ["CommandName"] = value;
224 [WebSysDescription ("")]
225 [WebCategoryAttribute ("Behavior")]
226 public virtual string OnClientClick
229 throw new NotImplementedException ();
232 throw new NotImplementedException ();
240 [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
244 [WebSysDescription ("")]
245 [WebCategory ("Appearance")]
246 public virtual string Text {
248 return ViewState.GetString ("Text", "");
251 ViewState ["Text"] = value;
257 protected virtual void OnClick (EventArgs e)
259 EventHandler h = (EventHandler) Events [ClickEvent];
263 static readonly object ClickEvent = new object ();
265 [WebSysDescription ("")]
266 [WebCategory ("Action")]
267 public event EventHandler Click {
268 add { Events.AddHandler (ClickEvent, value); }
269 remove { Events.RemoveHandler (ClickEvent, value); }
272 protected virtual void OnCommand (CommandEventArgs e)
274 CommandEventHandler h = (CommandEventHandler) Events [CommandEvent];
278 RaiseBubbleEvent (this, e);
280 static readonly object CommandEvent = new object ();
282 [WebSysDescription ("")]
283 [WebCategory ("Action")]
284 public event CommandEventHandler Command {
285 add { Events.AddHandler (CommandEvent, value); }
286 remove { Events.RemoveHandler (CommandEvent, value); }
289 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
291 [UrlProperty ("*.aspx")]
293 public virtual string PostBackUrl {
295 return ViewState.GetString ("PostBackUrl", String.Empty);
298 ViewState["PostBackUrl"] = value;
304 [WebSysDescription ("")]
305 [WebCategoryAttribute ("Behavior")]
306 public virtual string ValidationGroup {
308 return ViewState.GetString ("ValidationGroup", "");
311 ViewState ["ValidationGroup"] = value;