using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.WebControls; using SPSolutions.Web; using System; using System.Globalization; using System.Reflection; using System.Runtime.CompilerServices; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace SPSolutions.SharePoint.WebControls { public abstract class SelectorEntityEditorAdapter : ContextSelector, IPostBackEventHandler { private static readonly string SelectedCommand = "selected"; private HiddenField HiddenSelectedId; public event EventHandler Selected; //{ // [MethodImpl(MethodImplOptions.Synchronized)] // add // { // this.Selected += value;// (EventHandler)Delegate.Combine(this.Selected, value); // //this.Selected += SelectorEntityEditorAdapter_Selected; // } // [MethodImpl(MethodImplOptions.Synchronized)] // remove // { // this.Selected -= value; // //this.Selected += (EventHandler)Delegate.Remove(this.Selected, value); // } //} void SelectorEntityEditorAdapter_Selected(object sender, EventArgs e) { throw new NotImplementedException(); } public abstract Type PickerDialogType { get; } public string SelectedId { get { return this.HiddenSelectedId.Value; } } public bool UseCookieInitialization { get { return this.ViewState["Microsoft.SharePoint.SPAlertTemplate"] == null || (bool)this.ViewState["Microsoft.SharePoint.SPAlertTemplate"]; } set { this.ViewState["Microsoft.SharePoint.SPAlertTemplate"] = value; } } internal static string CookieCurrentId { get { PropertyInfo property = typeof(ContextSelector).GetProperty("CookieCurrentId", BindingFlags.Static | BindingFlags.NonPublic); return (string)property.GetValue(null, null); } set { PropertyInfo property = typeof(ContextSelector).GetProperty("CookieCurrentId", BindingFlags.Static | BindingFlags.NonPublic); property.SetValue(null, value, null); } } protected override string NavigateUrl { get { return string.Format("/_layouts/15/picker.aspx?PickerDialogType={0}", HttpUtility.UrlEncode(this.PickerDialogType.AssemblyQualifiedName)); } } protected override void CreateChildControls() { this.HiddenSelectedId = new HiddenField(); this.HiddenSelectedId.ID = "HiddenSelectedId"; this.Controls.Add(this.HiddenSelectedId); base.CreateChildControls(); } protected override void RenderContents(HtmlTextWriter output) { this.HiddenSelectedId.RenderControl(output); base.RenderContents(output); } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (base.AllowChange) { var text1 = "SelectContext_" + this.ID; string text = "CallbackWrapper_" + this.ID; string postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(this, SelectorEntityEditorAdapter.SelectedCommand); string text2; if (base.CurrentItem == null) { text2 = this.NavigateUrl; } else { text2 = HttpUtil.AppendUrlQueryString(this.NavigateUrl, string.Format("{0}={1}", this.Key, base.CurrentId)); } string script = string.Concat(new string[] { "" }); this.Page.ClientScript.RegisterClientScriptBlock(typeof(ContextSelectorEntityEditorAdapter), text, script); string text3 = string.Format(CultureInfo.InvariantCulture, "commonShowModalDialog('{0}','resizable: yes; status: no; scroll: no; help: no; center: yes; dialogWidth : {1}pt; dialogHeight : {2}pt;',{3});", new object[] { SPHttpUtility.EcmaScriptStringLiteralEncode(text2), base.DialogWidth, base.DialogHeight, SPHttpUtility.EcmaScriptStringLiteralEncode(text) }); this.ChangeItem.ClientOnClickScript = (SPHttpUtility.NoEncode(text3)); PickerDialog.RegisterPickerReturnValueHiddenFields(this.Page); } } protected override void OnLoad(EventArgs e) { if (!this.Page.IsPostBack) { if (base.CurrentItem != null) { T currentItem = base.CurrentItem; if (!currentItem.Equals(default(T))) { return; } } string idFromQueryString = this.IdFromQueryString; if (idFromQueryString == null) { if (string.IsNullOrEmpty(base.ClearSelectionText)) { if (this.UseCookieInitialization) { base.CurrentId = (SelectorEntityEditorAdapter.CookieCurrentId); return; } if (base.CurrentItem != null) { T currentItem2 = base.CurrentItem; if (!currentItem2.Equals(default(T))) { return; } } base.CurrentId = (this.DefaultSelectionId()); return; } } else { base.CurrentId = idFromQueryString; } } } public void RaisePostBackEvent(string eventArgument) { if (eventArgument == null) { throw new ArgumentNullException("eventArgument"); } string a = eventArgument.ToLower(CultureInfo.InvariantCulture); if (a == SelectorEntityEditorAdapter.SelectedCommand) { this.OnSelected(new EventArgs()); } } protected virtual void OnSelected(EventArgs e) { base.CurrentId = (this.SelectedId); if (this.Selected != null) { this.Selected(this, e); } } } }