LinkButton.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ FXUpdate3074 / FXUpdate3074 / 1.1 / DEVDIV / depot / DevDiv / releases / whidbey / QFE / ndp / fx / src / xsp / System / Web / UI / WebControls / LinkButton.cs / 4 / LinkButton.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Web.UI.WebControls { 
 
    using System;
    using System.ComponentModel; 
    using System.Drawing.Design;
    using System.Web;
    using System.Web.UI;
    using System.Web.Util; 
    using System.Security.Permissions;
 
 
    /// 
    /// Interacts with the parser to build a  control. 
    /// 
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class LinkButtonControlBuilder : ControlBuilder { 

 
        ///  
        /// 
        ///    Specifies whether white space literals are allowed. 
        /// 
        public override bool AllowWhitespaceLiterals() {
            return false;
        } 
    }
 
 

    ///  
    ///    Constructs a link button and defines its properties.
    /// 
    [
    ControlBuilderAttribute(typeof(LinkButtonControlBuilder)), 
    DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + AssemblyRef.SystemDesign),
    DefaultEvent("Click"), 
    DefaultProperty("Text"), 
    ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton"),
    Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + AssemblyRef.SystemDesign), 
    ParseChildren(false),
    SupportsEventValidation
    ]
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class LinkButton : WebControl, IButtonControl, IPostBackEventHandler { 
 
        private static readonly object EventClick = new object();
        private static readonly object EventCommand = new object(); 


        /// 
        /// Initializes a new instance of the  class. 
        /// 
        public LinkButton() : base(HtmlTextWriterTag.A) { 
        } 

 
        /// 
        ///    Specifies the command name that is propagated in the
        ///    event along with the associated 
        ///    property. 
        /// 
        [ 
        DefaultValue(""), 
        Themeable(false),
        WebCategory("Behavior"), 
        WebSysDescription(SR.WebControl_CommandName)
        ]
        public string CommandName {
            get { 
                string s = (string)ViewState["CommandName"];
                return((s == null) ? String.Empty : s); 
            } 
            set {
                ViewState["CommandName"] = value; 
            }
        }

 

        ///  
        ///    Specifies the command argument that is propagated in the 
        ///    event along with the associated 
        ///    property. 
        /// 
        [
        Bindable(true),
        DefaultValue(""), 
        Themeable(false),
        WebCategory("Behavior"), 
        WebSysDescription(SR.WebControl_CommandArgument) 
        ]
        public string CommandArgument { 
            get {
                string s = (string)ViewState["CommandArgument"];
                return((s == null) ? String.Empty : s);
            } 
            set {
                ViewState["CommandArgument"] = value; 
            } 
        }
 

        /// 
        ///    Gets or sets whether pressing the button causes page validation to fire. This defaults to True so that when
        ///          using validation controls, the validation state of all controls are updated when the button is clicked, both 
        ///          on the client and the server. Setting this to False is useful when defining a cancel or reset button on a page
        ///          that has validators. 
        ///  
        [
        DefaultValue(true), 
        Themeable(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_CausesValidation)
        ] 
        public virtual bool CausesValidation {
            get { 
                object b = ViewState["CausesValidation"]; 
                return((b == null) ? true : (bool)b);
            } 
            set {
                ViewState["CausesValidation"] = value;
            }
        } 
#if SITECOUNTERS
 
        [ 
        DefaultValue("LinkButton"),
        Themeable(false), 
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_CounterGroup),
        ]
        public String CounterGroup { 
            get {
                String s = (String)ViewState["CounterGroup"]; 
                return((s == null) ? "LinkButton" : s); 
            }
            set { 
                ViewState["CounterGroup"] = value;
            }
        }
 

        [ 
        DefaultValue(""), 
        Themeable(false),
        WebCategory("SiteCounters"), 
        WebSysDescription(SR.Control_For_SiteCounters_CounterName),
        ]
        public String CounterName {
            get { 
                String s = (String)ViewState["CounterName"];
                return((s == null) ? String.Empty : s); 
            } 
            set {
                ViewState["CounterName"] = value; 
            }
        }

 
        [
        DefaultValue(false), 
        Themeable(false), 
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_CountClicks), 
        ]
        public bool CountClicks {
            get {
                object b = ViewState["CountClicks"]; 
                return((b == null) ? false : (bool)b);
            } 
            set { 
                ViewState["CountClicks"] = value;
            } 
        }
#endif

        ///  
        ///    The script that is executed on a client-side click.
        ///  
        [ 
        DefaultValue(""),
        Themeable(false), 
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_OnClientClick)
        ]
        public virtual string OnClientClick { 
            get {
                string s = (string)ViewState["OnClientClick"]; 
                if (s == null) { 
                    return String.Empty;
                } 
                return s;
            }
            set {
                ViewState["OnClientClick"] = value; 
            }
        } 
 

        internal override bool RequiresLegacyRendering { 
            get {
                return true;
            }
        } 

 
        ///  
        ///    Gets or sets the text display for the link button.
        ///  
        [
        Localizable(true),
        Bindable(true),
        WebCategory("Appearance"), 
        DefaultValue(""),
        WebSysDescription(SR.LinkButton_Text), 
        PersistenceMode(PersistenceMode.InnerDefaultProperty) 
        ]
        public virtual string Text { 
            get {
                object o = ViewState["Text"];
                return((o == null) ? String.Empty : (string)o);
            } 
            set {
                if (HasControls()) { 
                    Controls.Clear(); 
                }
                ViewState["Text"] = value; 
            }
        }

 
        [
        DefaultValue(""), 
        Editor("System.Web.UI.Design.UrlEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)), 
        Themeable(false),
        UrlProperty("*.aspx"), 
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_PostBackUrl)
        ]
        public virtual string PostBackUrl { 
            get {
                string s = (string)ViewState["PostBackUrl"]; 
                return s == null? String.Empty : s; 
            }
            set { 
                ViewState["PostBackUrl"] = value;
            }
        }
#if SITECOUNTERS 

        [ 
        DefaultValue(-1), 
        Themeable(false),
        WebCategory("SiteCounters"), 
        WebSysDescription(SR.Control_For_SiteCounters_RowsPerDay),
        ]
        public int RowsPerDay {
            get { 
                Object o = ViewState["RowsPerDay"];
                return((o == null) ? -1 : (int) o); 
            } 
            set {
                if (value == 0) { 
                    throw new ArgumentOutOfRangeException("value");
                }
                ViewState["RowsPerDay"] = value;
            } 
        }
 
 
        [
        DefaultValue(""), 
        Themeable(false),
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_SiteCountersProvider)
        ] 
        public String SiteCountersProvider {
            get { 
                String s = (String) ViewState["SiteCountersProvider"]; 
                return((s != null) ? s : String.Empty);
            } 
            set {
                ViewState["SiteCountersProvider"] = value;
            }
        } 

 
        [ 
        DefaultValue(true),
        Themeable(false), 
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_TrackApplicationName),
        ]
        public bool TrackApplicationName { 
            get {
                object b = ViewState["TrackApplicationName"]; 
                return((b == null) ? true : (bool)b); 
            }
            set { 
                ViewState["TrackApplicationName"] = value;
            }
        }
 

        [ 
        DefaultValue(true), 
        Themeable(false),
        WebCategory("SiteCounters"), 
        WebSysDescription(SR.Control_For_SiteCounters_TrackPageUrl),
        ]
        public bool TrackPageUrl {
            get { 
                object b = ViewState["TrackPageUrl"];
                return((b == null) ? true : (bool)b); 
            } 
            set {
                ViewState["TrackPageUrl"] = value; 
            }
        }
#endif
 
        [
        WebCategory("Behavior"), 
        Themeable(false), 
        DefaultValue(""),
        WebSysDescription(SR.PostBackControl_ValidationGroup) 
        ]
        public virtual string ValidationGroup {
            get {
                string s = (string)ViewState["ValidationGroup"]; 
                return((s == null) ? String.Empty : s);
            } 
            set { 
                ViewState["ValidationGroup"] = value;
            } 
        }


        ///  
        ///    Occurs when the link button is clicked.
        ///  
        [ 
        WebCategory("Action"),
        WebSysDescription(SR.LinkButton_OnClick) 
        ]
        public event EventHandler Click {
            add {
                Events.AddHandler(EventClick, value); 
            }
            remove { 
                Events.RemoveHandler(EventClick, value); 
            }
        } 



        ///  
        /// Occurs when any item is clicked within the  control tree.
        ///  
        [ 
        WebCategory("Action"),
        WebSysDescription(SR.Button_OnCommand) 
        ]
        public event CommandEventHandler Command {
            add {
                Events.AddHandler(EventCommand, value); 
            }
            remove { 
                Events.RemoveHandler(EventCommand, value); 
            }
        } 


        /// 
        ///  
        ///    Render the attributes on the begin tag.
        ///  
        protected override void AddAttributesToRender(HtmlTextWriter writer) { 
            // Make sure we are in a form tag with runat=server.
            if (Page != null) { 
                Page.VerifyRenderingInServerForm(this);
            }

            // Need to merge the onclick attribute with the OnClientClick 
            string onClick = Util.EnsureEndWithSemiColon(OnClientClick);
 
            if (HasAttributes) { 
                string userOnClick = Attributes["onclick"];
                if (userOnClick != null) { 
                    // We don't use Util.MergeScript because OnClientClick or
                    // onclick attribute are set by page developer directly.  We
                    // should preserve the value without adding javascript prefix.
                    onClick += Util.EnsureEndWithSemiColon(userOnClick); 
                    Attributes.Remove("onclick");
                } 
            } 

            if (onClick.Length > 0) { 
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
            }

            bool effectiveEnabled = IsEnabled; 
            if (Enabled && !effectiveEnabled) {
                // We need to do the cascade effect on the server, because the browser 
                // only renders as disabled, but doesn't disable the functionality. 
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            } 

            base.AddAttributesToRender(writer);

            if (effectiveEnabled && Page != null) { 
                //
 
                PostBackOptions options = GetPostBackOptions(); 
                string postBackEventReference = null;
                if (options != null) { 
                    postBackEventReference = Page.ClientScript.GetPostBackEventReference(options, true);
                }

                // If the postBackEventReference is empty, use a javascript no-op instead, since 
                //  is a link to the root of the current directory.
                if (String.IsNullOrEmpty(postBackEventReference)) { 
                    postBackEventReference = "javascript:void(0)"; 
                }
 
                writer.AddAttribute(HtmlTextWriterAttribute.Href, postBackEventReference);
            }
        }
 

        ///  
        ///  
        /// 
        protected override void AddParsedSubObject(object obj) { 
            if (HasControls()) {
                base.AddParsedSubObject(obj);
            }
            else { 
                if (obj is LiteralControl) {
                    Text = ((LiteralControl)obj).Text; 
                } 
                else {
                    string currentText = Text; 
                    if (currentText.Length != 0) {
                        Text = String.Empty;
                        base.AddParsedSubObject(new LiteralControl(currentText));
                    } 
                    base.AddParsedSubObject(obj);
                } 
            } 
        }
 
        // Returns the client post back options.
        protected virtual PostBackOptions GetPostBackOptions() {
            PostBackOptions options = new PostBackOptions(this, String.Empty);
            options.RequiresJavaScriptProtocol = true; 

            if (!String.IsNullOrEmpty(PostBackUrl)) { 
                // 

                options.ActionUrl = HttpUtility.UrlPathEncode(ResolveClientUrl(PostBackUrl)); 

                // Also, there is a specific behavior in IE that when the script
                // is triggered in href attribute, the whole string will be
                // decoded once before the code is run.  This doesn't happen to 
                // onclick or other event attributes.  So here we do an extra
                // encoding to compensate the weird behavior on IE. 
                if (!DesignMode && Page != null && 
                    String.Equals(Page.Request.Browser.Browser, "IE", StringComparison.OrdinalIgnoreCase)) {
                    options.ActionUrl = Util.QuoteJScriptString(options.ActionUrl, true); 
                }
            }

            if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) { 
                options.PerformValidation = true;
                options.ValidationGroup = ValidationGroup; 
            } 

            return options; 
        }


        ///  
        /// 
        ///    Load previously saved state. 
        ///    Overridden to synchronize Text property with LiteralContent. 
        /// 
        protected override void LoadViewState(object savedState) { 
            if (savedState != null) {
                base.LoadViewState(savedState);
                string s = (string)ViewState["Text"];
                if (s != null) 
                    Text = s;
            } 
        } 

 
        /// 
        /// Raises the  event.
        /// 
        protected virtual void OnClick(EventArgs e) { 
            EventHandler onClickHandler = (EventHandler)Events[EventClick];
            if (onClickHandler != null) onClickHandler(this,e); 
        } 

 
        /// 
        /// Raises the  event.
        /// 
        protected virtual void OnCommand(CommandEventArgs e) { 
            CommandEventHandler onCommandHandler = (CommandEventHandler)Events[EventCommand];
            if (onCommandHandler != null) 
                onCommandHandler(this,e); 

            // Command events are bubbled up the control heirarchy 
            RaiseBubbleEvent(this, e);
        }

 
        /// 
        ///  
        /// Raises a event upon postback 
        /// to the server, and a  event if the 
        /// is defined. 
        /// 
        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
            RaisePostBackEvent(eventArgument);
        } 

 
        ///  
        /// 
        /// Raises a event upon postback 
        /// to the server, and a  event if the 
        /// is defined.
        /// 
        protected virtual void RaisePostBackEvent(string eventArgument) { 
            ValidateEvent(this.UniqueID, eventArgument);
#if ORCAS 
            SiteCounters siteCounters = Context.SiteCounters; 
            if (siteCounters.Enabled && CountClicks) {
                String counterName = CounterName; 
                if (counterName.Length == 0) {
                    counterName = ID;
                }
                siteCounters.Write(CounterGroup, counterName, SiteCounters.ClickEventText, 
                                   null, TrackApplicationName, TrackPageUrl,
                                   SiteCountersProvider, RowsPerDay); 
            } 
#endif
            if (CausesValidation) { 
                Page.Validate(ValidationGroup);
            }
            OnClick(EventArgs.Empty);
            OnCommand(new CommandEventArgs(CommandName, CommandArgument)); 
        }
 
 
        /// 
        protected internal override void OnPreRender(EventArgs e) { 
            base.OnPreRender(e);
            if (Page != null && Enabled) {
                Page.RegisterPostBackScript();
 
                if ((CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) ||
                     !String.IsNullOrEmpty(PostBackUrl)) { 
                    Page.RegisterWebFormsScript();  // 
                }
            } 
        }


        ///  
        /// 
        ///  
        protected internal override void RenderContents(HtmlTextWriter writer) { 
            if (HasRenderingData()) {
                base.RenderContents(writer); 
            }
            else {
                writer.Write(Text);
            } 
        }
    } 
} 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

namespace System.Web.UI.WebControls { 
 
    using System;
    using System.ComponentModel; 
    using System.Drawing.Design;
    using System.Web;
    using System.Web.UI;
    using System.Web.Util; 
    using System.Security.Permissions;
 
 
    /// 
    /// Interacts with the parser to build a  control. 
    /// 
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class LinkButtonControlBuilder : ControlBuilder { 

 
        ///  
        /// 
        ///    Specifies whether white space literals are allowed. 
        /// 
        public override bool AllowWhitespaceLiterals() {
            return false;
        } 
    }
 
 

    ///  
    ///    Constructs a link button and defines its properties.
    /// 
    [
    ControlBuilderAttribute(typeof(LinkButtonControlBuilder)), 
    DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + AssemblyRef.SystemDesign),
    DefaultEvent("Click"), 
    DefaultProperty("Text"), 
    ToolboxData("<{0}:LinkButton runat=\"server\">LinkButton"),
    Designer("System.Web.UI.Design.WebControls.LinkButtonDesigner, " + AssemblyRef.SystemDesign), 
    ParseChildren(false),
    SupportsEventValidation
    ]
    [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)] 
    [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
    public class LinkButton : WebControl, IButtonControl, IPostBackEventHandler { 
 
        private static readonly object EventClick = new object();
        private static readonly object EventCommand = new object(); 


        /// 
        /// Initializes a new instance of the  class. 
        /// 
        public LinkButton() : base(HtmlTextWriterTag.A) { 
        } 

 
        /// 
        ///    Specifies the command name that is propagated in the
        ///    event along with the associated 
        ///    property. 
        /// 
        [ 
        DefaultValue(""), 
        Themeable(false),
        WebCategory("Behavior"), 
        WebSysDescription(SR.WebControl_CommandName)
        ]
        public string CommandName {
            get { 
                string s = (string)ViewState["CommandName"];
                return((s == null) ? String.Empty : s); 
            } 
            set {
                ViewState["CommandName"] = value; 
            }
        }

 

        ///  
        ///    Specifies the command argument that is propagated in the 
        ///    event along with the associated 
        ///    property. 
        /// 
        [
        Bindable(true),
        DefaultValue(""), 
        Themeable(false),
        WebCategory("Behavior"), 
        WebSysDescription(SR.WebControl_CommandArgument) 
        ]
        public string CommandArgument { 
            get {
                string s = (string)ViewState["CommandArgument"];
                return((s == null) ? String.Empty : s);
            } 
            set {
                ViewState["CommandArgument"] = value; 
            } 
        }
 

        /// 
        ///    Gets or sets whether pressing the button causes page validation to fire. This defaults to True so that when
        ///          using validation controls, the validation state of all controls are updated when the button is clicked, both 
        ///          on the client and the server. Setting this to False is useful when defining a cancel or reset button on a page
        ///          that has validators. 
        ///  
        [
        DefaultValue(true), 
        Themeable(false),
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_CausesValidation)
        ] 
        public virtual bool CausesValidation {
            get { 
                object b = ViewState["CausesValidation"]; 
                return((b == null) ? true : (bool)b);
            } 
            set {
                ViewState["CausesValidation"] = value;
            }
        } 
#if SITECOUNTERS
 
        [ 
        DefaultValue("LinkButton"),
        Themeable(false), 
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_CounterGroup),
        ]
        public String CounterGroup { 
            get {
                String s = (String)ViewState["CounterGroup"]; 
                return((s == null) ? "LinkButton" : s); 
            }
            set { 
                ViewState["CounterGroup"] = value;
            }
        }
 

        [ 
        DefaultValue(""), 
        Themeable(false),
        WebCategory("SiteCounters"), 
        WebSysDescription(SR.Control_For_SiteCounters_CounterName),
        ]
        public String CounterName {
            get { 
                String s = (String)ViewState["CounterName"];
                return((s == null) ? String.Empty : s); 
            } 
            set {
                ViewState["CounterName"] = value; 
            }
        }

 
        [
        DefaultValue(false), 
        Themeable(false), 
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_CountClicks), 
        ]
        public bool CountClicks {
            get {
                object b = ViewState["CountClicks"]; 
                return((b == null) ? false : (bool)b);
            } 
            set { 
                ViewState["CountClicks"] = value;
            } 
        }
#endif

        ///  
        ///    The script that is executed on a client-side click.
        ///  
        [ 
        DefaultValue(""),
        Themeable(false), 
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_OnClientClick)
        ]
        public virtual string OnClientClick { 
            get {
                string s = (string)ViewState["OnClientClick"]; 
                if (s == null) { 
                    return String.Empty;
                } 
                return s;
            }
            set {
                ViewState["OnClientClick"] = value; 
            }
        } 
 

        internal override bool RequiresLegacyRendering { 
            get {
                return true;
            }
        } 

 
        ///  
        ///    Gets or sets the text display for the link button.
        ///  
        [
        Localizable(true),
        Bindable(true),
        WebCategory("Appearance"), 
        DefaultValue(""),
        WebSysDescription(SR.LinkButton_Text), 
        PersistenceMode(PersistenceMode.InnerDefaultProperty) 
        ]
        public virtual string Text { 
            get {
                object o = ViewState["Text"];
                return((o == null) ? String.Empty : (string)o);
            } 
            set {
                if (HasControls()) { 
                    Controls.Clear(); 
                }
                ViewState["Text"] = value; 
            }
        }

 
        [
        DefaultValue(""), 
        Editor("System.Web.UI.Design.UrlEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)), 
        Themeable(false),
        UrlProperty("*.aspx"), 
        WebCategory("Behavior"),
        WebSysDescription(SR.Button_PostBackUrl)
        ]
        public virtual string PostBackUrl { 
            get {
                string s = (string)ViewState["PostBackUrl"]; 
                return s == null? String.Empty : s; 
            }
            set { 
                ViewState["PostBackUrl"] = value;
            }
        }
#if SITECOUNTERS 

        [ 
        DefaultValue(-1), 
        Themeable(false),
        WebCategory("SiteCounters"), 
        WebSysDescription(SR.Control_For_SiteCounters_RowsPerDay),
        ]
        public int RowsPerDay {
            get { 
                Object o = ViewState["RowsPerDay"];
                return((o == null) ? -1 : (int) o); 
            } 
            set {
                if (value == 0) { 
                    throw new ArgumentOutOfRangeException("value");
                }
                ViewState["RowsPerDay"] = value;
            } 
        }
 
 
        [
        DefaultValue(""), 
        Themeable(false),
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_SiteCountersProvider)
        ] 
        public String SiteCountersProvider {
            get { 
                String s = (String) ViewState["SiteCountersProvider"]; 
                return((s != null) ? s : String.Empty);
            } 
            set {
                ViewState["SiteCountersProvider"] = value;
            }
        } 

 
        [ 
        DefaultValue(true),
        Themeable(false), 
        WebCategory("SiteCounters"),
        WebSysDescription(SR.Control_For_SiteCounters_TrackApplicationName),
        ]
        public bool TrackApplicationName { 
            get {
                object b = ViewState["TrackApplicationName"]; 
                return((b == null) ? true : (bool)b); 
            }
            set { 
                ViewState["TrackApplicationName"] = value;
            }
        }
 

        [ 
        DefaultValue(true), 
        Themeable(false),
        WebCategory("SiteCounters"), 
        WebSysDescription(SR.Control_For_SiteCounters_TrackPageUrl),
        ]
        public bool TrackPageUrl {
            get { 
                object b = ViewState["TrackPageUrl"];
                return((b == null) ? true : (bool)b); 
            } 
            set {
                ViewState["TrackPageUrl"] = value; 
            }
        }
#endif
 
        [
        WebCategory("Behavior"), 
        Themeable(false), 
        DefaultValue(""),
        WebSysDescription(SR.PostBackControl_ValidationGroup) 
        ]
        public virtual string ValidationGroup {
            get {
                string s = (string)ViewState["ValidationGroup"]; 
                return((s == null) ? String.Empty : s);
            } 
            set { 
                ViewState["ValidationGroup"] = value;
            } 
        }


        ///  
        ///    Occurs when the link button is clicked.
        ///  
        [ 
        WebCategory("Action"),
        WebSysDescription(SR.LinkButton_OnClick) 
        ]
        public event EventHandler Click {
            add {
                Events.AddHandler(EventClick, value); 
            }
            remove { 
                Events.RemoveHandler(EventClick, value); 
            }
        } 



        ///  
        /// Occurs when any item is clicked within the  control tree.
        ///  
        [ 
        WebCategory("Action"),
        WebSysDescription(SR.Button_OnCommand) 
        ]
        public event CommandEventHandler Command {
            add {
                Events.AddHandler(EventCommand, value); 
            }
            remove { 
                Events.RemoveHandler(EventCommand, value); 
            }
        } 


        /// 
        ///  
        ///    Render the attributes on the begin tag.
        ///  
        protected override void AddAttributesToRender(HtmlTextWriter writer) { 
            // Make sure we are in a form tag with runat=server.
            if (Page != null) { 
                Page.VerifyRenderingInServerForm(this);
            }

            // Need to merge the onclick attribute with the OnClientClick 
            string onClick = Util.EnsureEndWithSemiColon(OnClientClick);
 
            if (HasAttributes) { 
                string userOnClick = Attributes["onclick"];
                if (userOnClick != null) { 
                    // We don't use Util.MergeScript because OnClientClick or
                    // onclick attribute are set by page developer directly.  We
                    // should preserve the value without adding javascript prefix.
                    onClick += Util.EnsureEndWithSemiColon(userOnClick); 
                    Attributes.Remove("onclick");
                } 
            } 

            if (onClick.Length > 0) { 
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
            }

            bool effectiveEnabled = IsEnabled; 
            if (Enabled && !effectiveEnabled) {
                // We need to do the cascade effect on the server, because the browser 
                // only renders as disabled, but doesn't disable the functionality. 
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            } 

            base.AddAttributesToRender(writer);

            if (effectiveEnabled && Page != null) { 
                //
 
                PostBackOptions options = GetPostBackOptions(); 
                string postBackEventReference = null;
                if (options != null) { 
                    postBackEventReference = Page.ClientScript.GetPostBackEventReference(options, true);
                }

                // If the postBackEventReference is empty, use a javascript no-op instead, since 
                //  is a link to the root of the current directory.
                if (String.IsNullOrEmpty(postBackEventReference)) { 
                    postBackEventReference = "javascript:void(0)"; 
                }
 
                writer.AddAttribute(HtmlTextWriterAttribute.Href, postBackEventReference);
            }
        }
 

        ///  
        ///  
        /// 
        protected override void AddParsedSubObject(object obj) { 
            if (HasControls()) {
                base.AddParsedSubObject(obj);
            }
            else { 
                if (obj is LiteralControl) {
                    Text = ((LiteralControl)obj).Text; 
                } 
                else {
                    string currentText = Text; 
                    if (currentText.Length != 0) {
                        Text = String.Empty;
                        base.AddParsedSubObject(new LiteralControl(currentText));
                    } 
                    base.AddParsedSubObject(obj);
                } 
            } 
        }
 
        // Returns the client post back options.
        protected virtual PostBackOptions GetPostBackOptions() {
            PostBackOptions options = new PostBackOptions(this, String.Empty);
            options.RequiresJavaScriptProtocol = true; 

            if (!String.IsNullOrEmpty(PostBackUrl)) { 
                // 

                options.ActionUrl = HttpUtility.UrlPathEncode(ResolveClientUrl(PostBackUrl)); 

                // Also, there is a specific behavior in IE that when the script
                // is triggered in href attribute, the whole string will be
                // decoded once before the code is run.  This doesn't happen to 
                // onclick or other event attributes.  So here we do an extra
                // encoding to compensate the weird behavior on IE. 
                if (!DesignMode && Page != null && 
                    String.Equals(Page.Request.Browser.Browser, "IE", StringComparison.OrdinalIgnoreCase)) {
                    options.ActionUrl = Util.QuoteJScriptString(options.ActionUrl, true); 
                }
            }

            if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) { 
                options.PerformValidation = true;
                options.ValidationGroup = ValidationGroup; 
            } 

            return options; 
        }


        ///  
        /// 
        ///    Load previously saved state. 
        ///    Overridden to synchronize Text property with LiteralContent. 
        /// 
        protected override void LoadViewState(object savedState) { 
            if (savedState != null) {
                base.LoadViewState(savedState);
                string s = (string)ViewState["Text"];
                if (s != null) 
                    Text = s;
            } 
        } 

 
        /// 
        /// Raises the  event.
        /// 
        protected virtual void OnClick(EventArgs e) { 
            EventHandler onClickHandler = (EventHandler)Events[EventClick];
            if (onClickHandler != null) onClickHandler(this,e); 
        } 

 
        /// 
        /// Raises the  event.
        /// 
        protected virtual void OnCommand(CommandEventArgs e) { 
            CommandEventHandler onCommandHandler = (CommandEventHandler)Events[EventCommand];
            if (onCommandHandler != null) 
                onCommandHandler(this,e); 

            // Command events are bubbled up the control heirarchy 
            RaiseBubbleEvent(this, e);
        }

 
        /// 
        ///  
        /// Raises a event upon postback 
        /// to the server, and a  event if the 
        /// is defined. 
        /// 
        void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
            RaisePostBackEvent(eventArgument);
        } 

 
        ///  
        /// 
        /// Raises a event upon postback 
        /// to the server, and a  event if the 
        /// is defined.
        /// 
        protected virtual void RaisePostBackEvent(string eventArgument) { 
            ValidateEvent(this.UniqueID, eventArgument);
#if ORCAS 
            SiteCounters siteCounters = Context.SiteCounters; 
            if (siteCounters.Enabled && CountClicks) {
                String counterName = CounterName; 
                if (counterName.Length == 0) {
                    counterName = ID;
                }
                siteCounters.Write(CounterGroup, counterName, SiteCounters.ClickEventText, 
                                   null, TrackApplicationName, TrackPageUrl,
                                   SiteCountersProvider, RowsPerDay); 
            } 
#endif
            if (CausesValidation) { 
                Page.Validate(ValidationGroup);
            }
            OnClick(EventArgs.Empty);
            OnCommand(new CommandEventArgs(CommandName, CommandArgument)); 
        }
 
 
        /// 
        protected internal override void OnPreRender(EventArgs e) { 
            base.OnPreRender(e);
            if (Page != null && Enabled) {
                Page.RegisterPostBackScript();
 
                if ((CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) ||
                     !String.IsNullOrEmpty(PostBackUrl)) { 
                    Page.RegisterWebFormsScript();  // 
                }
            } 
        }


        ///  
        /// 
        ///  
        protected internal override void RenderContents(HtmlTextWriter writer) { 
            if (HasRenderingData()) {
                base.RenderContents(writer); 
            }
            else {
                writer.Write(Text);
            } 
        }
    } 
} 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK