【问题标题】:How to use a simple YES/NO MessageBox in a SharePoint 2010 EventHandler?如何在 SharePoint 2010 EventHandler 中使用简单的“是/否”消息框?
【发布时间】:2011-01-11 10:45:40
【问题描述】:

我创建了一个事件处理程序 (SPEventReceiverType.ItemDeleting),当用户从特定 SPList 中删除一个 SPListItem 时调用它。对于每个项目都有一个网站,我想用是/否消息框简单地询问用户是否也应该删除该网站...... 还有一个问题,在google了一段时间后,我意识到我找不到一些常用的提示如何显示一个MessageBox,如果点击是,如何继续做一些其他的代码。

无论如何,我会这样做的通常方式在 SharePoint 2010 EventHandler 中不起作用:

if (MessageBox.Show("Do you want to delete the site as well?", "Delete?", 
    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
    // Continue Here if "Yes" has been clicked
}

有时,SharePoint 让我发疯。 真诚的,马库斯·施瓦尔贝

附:如果你愿意,你可以尝试解决我的另一个问题:)

【问题讨论】:

    标签: sharepoint-2010 messagebox event-handling


    【解决方案1】:

    你可以这样做:

    public override void ItemDeleting(SPItemEventProperties properties)
            {
    
                string updateID = Convert.ToString(properties.ListItem["ID"], CultureInfo.InvariantCulture);
                string itemUID = tmpItem.UniqueId.ToString();
                string urlProcessing = string.Format(CultureInfo.InvariantCulture, Constants.StringRedirect, web.Url, Constants.PersonRecord, "Processing.aspx", itemUID, updateID, modifyTime);
                properties.Status = SPEventReceiverStatus.CancelNoError;
                SPUtility.Redirect(urlProcessing, SPRedirectFlags.Static, this.currentContext);
    
        }
    

    Processing.aspx 将包含一个自定义 MessageBox webPart,它将根据响应删除或不删除该项目。它将从 Url 查询字符串中读取 ID。这是sn-p:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    
    
    /// <summary>
    /// This class contains operations to check if a username is duplicated or not
    /// </summary>  
    [Guid("ddeaa4a4-6d63-4fc8-9a17-8874db582388")]
    [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
    [SecurityPermission(SecurityAction.InheritanceDemand, Unrestricted = true)]
    [CLSCompliant(false)]
    public class ConfirmWebpart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        /// <summary>
        /// Duplicated username
        /// </summary>
        private string userName;
    
        /// <summary>
        /// UNID of a item
        /// </summary>
        private string itemID;
    
        /// <summary>
        /// ID of updating item
        /// </summary>
        private string updateID;
    
        /// <summary>
        /// Last edit time
        /// </summary>
        private string lastEdit;
    
        /// <summary>
        /// To check error in the application.  
        /// </summary>
        private bool error;
    
        /// <summary>
        /// Current context
        /// </summary>
        private HttpContext currentContext;
    
        /// <summary>
        /// Initializes a new instance of the ConfirmWebpart class.
        /// </summary>
        public ConfirmWebpart()
        {
            this.currentContext = HttpContext.Current;
            this.ExportMode = WebPartExportMode.All;
        }
    
        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e">e parameter</param>
        [SuppressMessage("Microsoft.Security", "CA2109:ReviewVisibleEventHandlers", Justification = "This is to catch all possible exceptions and to prevent exceptions to bubble up on screen.")]
        protected override void OnLoad(EventArgs e)
        {
            if (!this.error)
            {
                try
                {
                    base.OnLoad(e);
                 this.EnsureChildControls();
                }
                catch (Exception ex)
                {
                    this.HandleException(ex);
                    throw;
                }
            }
        }
    
        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!this.error)
            {
                try
                {
                    base.CreateChildControls();
                    this.GetQueryStringValues();
                    this.RenderContents();
                }
                catch (Exception ex)
                {
                    this.HandleException(ex);
                    throw;
                }
            }
        }
    
        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex">ex parameter</param>
        protected void HandleException(Exception ex)
        {
            this.error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    
        /// <summary>
        /// Get all querystring value and assign to private members
        /// </summary>
        protected void GetQueryStringValues()
        {
            this.itemID = Page.Request.QueryString[Constants.ItemD];
            this.updateID = Page.Request.QueryString[Constants.Update];
            this.lastEdit = Page.Request.QueryString[Constants.LastEdit];
        }
    
        /// <summary>
        /// Render UI form
        /// </summary>
        protected void RenderContents()
        {
            this.GetDuplicatedUserName();
    
            this.Controls.Add(new LiteralControl("<table cellpadding=\"0\" cellspacing=\"0\"><tr style=\"height:50px\"><td style=\"width:50px\"></td><td colspan=\"3\"></td></tr><tr style=\"font-size: medium;color: #3366FF;height:40px\"><td></td><td colspan=\"3\">"));
            this.Controls.Add(new LiteralControl(string.Format(CultureInfo.CurrentCulture, Constants.ConfirmMessage, this.userName)));
            this.Controls.Add(new LiteralControl("</tr><tr><td></td><td colspan=\"3\"></td></tr><tr><td style=\"color: #3366FF\"></td><td colspan=\"3\" style=\"color: #3366FF\">"));
            this.Controls.Add(new LiteralControl(Constants.YesAction));
            this.Controls.Add(new LiteralControl("</tr><tr><td style=\"color: #3366FF\"></td><td colspan=\"3\" style=\"color: #3366FF\">"));
            this.Controls.Add(new LiteralControl(Constants.NoAction));
            this.Controls.Add(new LiteralControl("</td></tr><tr><td></td><td colspan=\"3\" style=\"height:20px\"></td></tr><tr><td style=\"width:100px\"></td><td style=\"width:100px\"></td><td>"));
    
            //// Add Yess button
            Button btnYes = new Button();
            btnYes.Text = Constants.YesButtonLabel;
            btnYes.Width = new Unit(70);
            btnYes.Click += new EventHandler(this.BtnYesClick);
            this.Controls.Add(btnYes);
    
            this.Controls.Add(new LiteralControl("</td><td>"));
            //// Add No button
            Button btnNo = new Button();
            btnNo.Text = Constants.NoButtonLabel;
            btnNo.Click += new EventHandler(this.BtnNoClick);
            btnNo.Width = new Unit(70);
            this.Controls.Add(btnNo);
            this.Controls.Add(new LiteralControl("</td></tr></table>"));
        }
    
    
    
        /// <summary>
        /// This method is used to handle the click event of Yes button
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">argument parameter</param>
        private void BtnYesClick(object sender, EventArgs e)
        {
           //Delete the item
        }
    
        /// <summary>
        /// This method is used to handle the click event of No button
        /// </summary>
        /// <param name="sender">sender parameter</param>
        /// <param name="e">argument parameter</param>
        private void BtnNoClick(object sender, EventArgs e)
        {
            // Redirect to the default view of People list
            string defaultView = string.Format(CultureInfo.InvariantCulture, Constants.WebRedirectUrl, SPContext.Current.Web.Url, Constants.ListName);
            SPUtility.Redirect(defaultView, SPRedirectFlags.Trusted, this.currentContext);
        }
    

    }

    【讨论】:

      【解决方案2】:

      您尝试进行的“是/否”确认应在到达事件接收器之前处理。例如,如果我从计划的作业、powershell 或内容部署导入/导出中删除项目,则我无法捕获用户输入。更糟糕的是,进程会永远阻塞等待永远不会到来的用户输入。

      应使用 SPEventReceiverType.ItemDeleting 事件,例如,根据站点中的信息验证删除(例如停止删除查找字段中的引用数据),如果失败,则使用属性取消它。

      properties.Cancel = true; 
      properties.ErrorMessage = "Can not delete";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-13
        • 2021-03-10
        • 1970-01-01
        • 2012-10-18
        • 2011-02-27
        • 2015-12-30
        • 2011-06-27
        • 1970-01-01
        相关资源
        最近更新 更多