2 // System.Web.UI.WebControls.LinkButton.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 // (C) Gaurav Vaish (2002)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Web.UI;
\r
36 using System.ComponentModel;
37 using System.ComponentModel.Design;
\r
39 namespace System.Web.UI.WebControls
\r
41 [DefaultEvent("Click")]
\r
42 [DefaultProperty("Text")]
\r
43 [ControlBuilder(typeof(LinkButtonControlBuilder))]
\r
44 [Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
\r
45 [DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + Consts.AssemblySystem_Design)]
\r
46 [ParseChildren(false)]
\r
47 [ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton</{0}:LinkButton>")]
\r
48 public class LinkButton : WebControl, IPostBackEventHandler
\r
50 private static readonly object ClickEvent = new object();
\r
51 private static readonly object CommandEvent = new object();
\r
53 public LinkButton () : base (HtmlTextWriterTag.A)
\r
57 [DefaultValue (true), Bindable (false), WebCategory ("Behavior")]
58 [WebSysDescription ("Determines if validation is performed when clicked.")]
\r
59 public bool CausesValidation
\r
62 object o = ViewState ["CausesValidation"];
\r
63 return (o == null) ? true : (bool) o;
\r
66 set { ViewState ["CausesValidation"] = value; }
\r
69 [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
70 [WebSysDescription ("An argument for the Command of this control.")]
\r
71 public string CommandArgument
\r
74 object o = ViewState ["CommandArgument"];
\r
75 return (o == null) ? String.Empty : (string) o;
\r
78 set { ViewState ["CommandArgument"] = value; }
\r
81 [DefaultValue (""), WebCategory ("Behavior")]
82 [WebSysDescription ("The name of the Command of this control.")]
\r
83 public string CommandName
\r
86 object o = ViewState ["CommandName"];
\r
87 return (o == null) ? String.Empty : (string) o;
\r
90 set { ViewState ["CommandName"] = value; }
\r
93 [DefaultValue (""), Bindable (true), WebCategory ("Appearance")]
94 [PersistenceMode (PersistenceMode.InnerDefaultProperty)]
95 [WebSysDescription ("The text that should be shown on this LinkButton.")]
\r
96 public virtual string Text
\r
99 object o = ViewState ["Text"];
\r
100 return (o == null) ? String.Empty : (string) o;
\r
103 set { ViewState ["Text"] = value; }
\r
106 [WebCategory ("Action")]
107 [WebSysDescription ("Raised when the LinkButton is clicked.")]
\r
108 public event EventHandler Click
\r
110 add { Events.AddHandler(ClickEvent, value); }
\r
111 remove { Events.RemoveHandler(ClickEvent, value); }
\r
114 [WebCategory ("Action")]
115 [WebSysDescription ("Raised when a LinkButton Command is executed.")]
\r
116 public event CommandEventHandler Command
\r
118 add { Events.AddHandler(CommandEvent, value); }
\r
119 remove { Events.RemoveHandler(CommandEvent, value); }
\r
122 protected virtual void OnClick (EventArgs e)
\r
124 if(Events != null){
\r
125 EventHandler eh = (EventHandler) (Events [ClickEvent]);
\r
131 protected virtual void OnCommand (CommandEventArgs e)
\r
133 if(Events != null){
\r
134 CommandEventHandler ceh = (CommandEventHandler) (Events [CommandEvent]);
\r
138 RaiseBubbleEvent (this, e);
\r
141 protected override void OnPreRender (EventArgs e)
\r
143 base.OnPreRender(e);
\r
146 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
\r
148 if (CausesValidation)
\r
150 OnClick (EventArgs.Empty);
\r
151 OnCommand (new CommandEventArgs (CommandName, CommandArgument));
\r
154 protected override void AddAttributesToRender (HtmlTextWriter writer)
\r
156 base.AddAttributesToRender (writer);
\r
157 if (Enabled && Page != null){
\r
158 if (CausesValidation && Page.Validators.Count > 0){
\r
159 writer.AddAttribute (HtmlTextWriterAttribute.Href,
\r
161 Utils.GetClientValidatedPostBack (this));
\r
164 writer.AddAttribute (HtmlTextWriterAttribute.Href,
\r
165 Page.GetPostBackClientHyperlink (this, ""));
\r
169 protected override void AddParsedSubObject (object obj)
\r
171 if (HasControls ()){
\r
172 base.AddParsedSubObject (obj);
\r
176 if (obj is LiteralControl){
\r
177 Text = ((LiteralControl) obj).Text;
\r
181 if (Text.Length > 0){
\r
182 base.AddParsedSubObject (new LiteralControl (Text));
\r
183 Text = String.Empty;
\r
186 base.AddParsedSubObject (obj);
\r
189 protected override void LoadViewState (object savedState)
\r
191 if (savedState != null){
\r
192 base.LoadViewState (savedState);
\r
193 string savedText = (string) ViewState ["Text"];
\r
194 if (savedText != null)
\r
199 protected override void RenderContents (HtmlTextWriter writer)
\r
201 if (HasControls ()){
\r
202 base.RenderContents (writer);
\r
205 writer.Write (Text);
\r