Sharepoint 2010 如何在SharePoint 2010 EventHandler中使用简单的“是/否”消息框?

Sharepoint 2010 如何在SharePoint 2010 EventHandler中使用简单的“是/否”消息框?,sharepoint-2010,messagebox,event-handling,Sharepoint 2010,Messagebox,Event Handling,我已经创建了一个Eventhandler(SPEventReceiverType.ItemDeleting),当用户从特定的SPList中删除SPListItem时调用它。对于每个项目都有一个网站,我想简单地用是/否消息框询问用户是否应该删除该网站。。。 还有一个问题,在谷歌上呆了一段时间后,我意识到我找不到一些有用的提示如何显示MessageBox,如果单击Yes,如何继续执行其他代码 无论如何,我通常的做法在SharePoint 2010 EventHandler中不起作用: if (Mes

我已经创建了一个Eventhandler(SPEventReceiverType.ItemDeleting),当用户从特定的SPList中删除SPListItem时调用它。对于每个项目都有一个网站,我想简单地用是/否消息框询问用户是否应该删除该网站。。。 还有一个问题,在谷歌上呆了一段时间后,我意识到我找不到一些有用的提示如何显示MessageBox,如果单击Yes,如何继续执行其他代码

无论如何,我通常的做法在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让我发疯。 真的,马库斯·施瓦尔贝


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

你可以这样做:

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 Web部件,该部件将删除该项目或不删除该项目,具体取决于响应。它将从Url查询字符串中读取ID。下面是一个片段:

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);
    }
使用系统;
使用System.Collections.Generic;
使用System.Diagnostics.CodeAnalysis;
利用制度全球化;
使用System.Runtime.InteropServices;
使用System.Security.Permissions;
使用系统文本;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用Microsoft.SharePoint;
使用Microsoft.SharePoint.Utilities;
/// 
///此类包含检查用户名是否重复的操作
///   
[Guid(“ddeaa4a4-6d63-4fc8-9a17-8874db582388”)]
[SecurityPermission(SecurityAction.LinkDemand,Unrestricted=true)]
[SecurityPermission(SecurityAction.InheritanceDemand,Unrestricted=true)]
[CLSCompliant(false)]
公共类ConfirmWebpart:Microsoft.SharePoint.WebPartPages.WebPart
{
/// 
///重复用户名
/// 
私有字符串用户名;
/// 
///项目的唯一性
/// 
私有字符串itemID;
/// 
///更新项目的ID
/// 
私有字符串更新ID;
/// 
///上次编辑时间
/// 
私有字符串lastEdit;
/// 
///检查应用程序中的错误。
/// 
私人布尔误差;
/// 
///当前上下文
/// 
私有HttpContext-currentContext;
/// 
///初始化ConfirmWebpart类的新实例。
/// 
公共确认Web部件()
{
this.currentContext=HttpContext.Current;
this.ExportMode=WebPartExportMode.All;
}
/// 
///确保在事件之前调用CreateChildControls()。
///使用CreateChildControls()创建控件。
/// 
///e参数
[SuppressMessage(“Microsoft.Security”、“CA2109:ReviewVisibleEventHandlers”,justionment=“这是为了捕获所有可能的异常并防止异常在屏幕上冒泡。”)]
受保护的覆盖无效加载(事件参数e)
{
如果(!this.error)
{
尝试
{
基础荷载(e);
this.ensureChildControl();
}
捕获(例外情况除外)
{
本.HandleException(ex);
投掷;
}
}
}
/// 
///在此处创建所有用于渲染的控件。
///尽量避免使用RenderWebPart()方法。
/// 
受保护的覆盖无效CreateChildControls()
{
如果(!this.error)
{
尝试
{
base.CreateChildControls();
这是.GetQueryStringValues();
这个.RenderContents();
}
捕获(例外情况除外)
{
本.HandleException(ex);
投掷;
}
}
}
/// 
///清除所有子控件并添加用于显示的错误消息。
/// 
///ex参数
受保护的无效句柄异常(异常ex)
{
this.error=true;
this.Controls.Clear();
this.Controls.Add(newliteralcontrol(ex.Message));
}
/// 
///获取所有querystring值并分配给私有成员
/// 
受保护的void GetQueryStringValues()
{
this.itemID=Page.Request.QueryString[Constants.ItemD];
this.updateID=Page.Request.QueryString[Constants.Update];
this.lastEdit=Page.Request.QueryString[Constants.lastEdit];
}
/// 
///呈现UI表单
/// 
受保护的void RenderContents()
{
这个.GetDuplicatedUserName();
this.Controls.Add(新的LiteralControl(“”);
添加(新的LiteralControl(string.Format(CultureInfo.CurrentCulture,Constants.ConfirmMessage,this.userName));
this.Controls.Add(新的LiteralControl(“”);
this.Controls.Add(新的LiteralControl(Constants.YesAction));
this.Controls.Add(新的LiteralControl(“”);
this.Controls.Add(新的LiteralControl(Constants.NoAction));
this.Controls.Add(新的LiteralControl(“”);
////添加Yess按钮
按钮btnYes=新按钮();
btnYes.Text=Constants.YesButtonLabel;
b宽度=新单位(70);
btnYes.Click+=neweventhandler(this.BtnYesClick);
this.Controls.Add(btnYes);
this.Controls.Add(新的LiteralControl(“”);
////不添加按钮
按钮btnNo=新按钮();
btnNo.Text=Constants.NoButtonLabel;
btnNo.Click+=neweventhandler(this.BtnNoClick);
btnNo.宽度=新装置(70);
this.Controls.Add(btnNo);
this.Controls.Add(新的LiteralControl(“”);
}
/// 
///此方法用于处理“是”按钮的单击事件
/// 
///发送器参数
///参数
私有void BtnYesClick(对象发送方,事件参数e)
{
//删除该项目
}
/// 
///此方法用于处理无按钮的点击事件
/// 
///发送器参数
///参数
私有void BtnNoClick(对象发送方,事件参数e)
{
//重定向到默认视图
properties.Cancel = true; 
properties.ErrorMessage = "Can not delete";