File: AsyncPostBackTrigger.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (157 lines) | stat: -rw-r--r-- 6,497 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//------------------------------------------------------------------------------
// <copyright file="AsyncPostBackTrigger.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
 
namespace System.Web.UI {
    using System;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Reflection;
    using System.Web;
    using System.Web.UI;
    using System.Web.Resources;
    using System.Web.Util;

    public class AsyncPostBackTrigger : UpdatePanelControlTrigger {

        private IScriptManagerInternal _scriptManager;

        private Control _associatedControl;
        private static MethodInfo _eventHandler;
        private bool _eventHandled;
        private string _eventName;

        public AsyncPostBackTrigger() {
        }

        internal AsyncPostBackTrigger(IScriptManagerInternal scriptManager) {
            _scriptManager = scriptManager;
        }

        private static MethodInfo EventHandler {
            get {
                if (_eventHandler == null) {
                    _eventHandler = typeof(AsyncPostBackTrigger).GetMethod("OnEvent");
                }
                return _eventHandler;
            }
        }

        [
        SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "ID"),
        TypeConverter("System.Web.UI.Design.AsyncPostBackTriggerControlIDConverter, " +
            AssemblyRef.SystemWebExtensionsDesign)
        ]
        public new string ControlID {
            get {
                return base.ControlID;
            }
            set {
                base.ControlID = value;
            }
        }

        [
        DefaultValue(""),
        Category("Behavior"),
        ResourceDescription("AsyncPostBackTrigger_EventName"),
        TypeConverter("System.Web.UI.Design.AsyncPostBackTriggerEventNameConverter, " +
            AssemblyRef.SystemWebExtensionsDesign),
        ]
        public string EventName {
            get {
                if (_eventName == null) {
                    return String.Empty;
                }
                return _eventName;
            }
            set {
                _eventName = value;
            }
        }

        internal IScriptManagerInternal ScriptManager {
            get {
                if (_scriptManager == null) {
                    Page page = Owner.Page;
                    if (page == null) {
                        throw new InvalidOperationException(AtlasWeb.Common_PageCannotBeNull);
                    }
                    _scriptManager = UI.ScriptManager.GetCurrent(page);
                    if (_scriptManager == null) {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.Common_ScriptManagerRequired, Owner.ID));
                    }
                }
                return _scriptManager;
            }
        }

        protected internal override void Initialize() {
            base.Initialize();

            _associatedControl = FindTargetControl(true);

            ScriptManager.RegisterAsyncPostBackControl(_associatedControl);

            string eventName = EventName;
            if (eventName.Length != 0) {
                // If EventName is specified, attach our event handler to it
                EventInfo eventInfo = _associatedControl.GetType().GetEvent(eventName, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);
                if (eventInfo == null) {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.AsyncPostBackTrigger_CannotFindEvent, eventName, ControlID, Owner.ID));
                }

                MethodInfo handlerMethod = eventInfo.EventHandlerType.GetMethod("Invoke");
                ParameterInfo[] parameters = handlerMethod.GetParameters();
                if (!handlerMethod.ReturnType.Equals(typeof(void)) ||
                    (parameters.Length != 2) ||
                    (typeof(EventArgs).IsAssignableFrom(parameters[1].ParameterType) == false)) {
                    throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, AtlasWeb.AsyncPostBackTrigger_InvalidEvent, eventName, ControlID, Owner.ID));
                }

                Delegate handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, EventHandler);
                eventInfo.AddEventHandler(_associatedControl, handler);
            }
        }

        protected internal override bool HasTriggered() {
            if (!String.IsNullOrEmpty(EventName)) {
                // If EventName is specified we are triggered if our event was raised
                return _eventHandled;
            }
            else {
                // If EventName is not specified, check if the control that caused the
                // postback either has the exact UniqueID we're looking for, or at least
                // begins with it.
                string sourceElement = ScriptManager.AsyncPostBackSourceElementID;
                return
                    (sourceElement == _associatedControl.UniqueID) ||
                    (sourceElement.StartsWith(_associatedControl.UniqueID + "$", StringComparison.Ordinal));
            }
        }

        // DevDiv Bugs 127369: This method should be private and the reflection lookup should assert reflection permission
        // so the private reflection works in medium trust.
        // However, ASP.NET AJAX 1.0 was released with this method public. Since it would be a breaking change to make it private
        // now, it was decided to leave it as is.
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers",
            Justification="TODO: This will be fixed in fc_serverfx")]
        public void OnEvent(object sender, EventArgs e) {
            _eventHandled = true;
        }

        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
        public override string ToString() {
            if (String.IsNullOrEmpty(ControlID)) {
                return "AsyncPostBack";
            }
            else {
                return "AsyncPostBack: " + ControlID +
                    (String.IsNullOrEmpty(EventName) ? String.Empty : ("." + EventName));
            }
        }
    }
}