SharePoint 2010中使用SPPageStatusSetter显示状态
本文中我们将学习如何在状态栏中(也就是显示在紧挨着功能区下面,顶部导航菜单上面的部分)显示一条状态信息(比如列表项已删除,已更新等等)。
整个过程是将SPPageStatusSetter控件包进一个WebPart里,然后从一个事件接收器中动态添加该WebPart到列表视图页面,同时显示一条状态栏信息。这种做饭背后的原因是因为我们无法在事件接收器中获得当前页面的上下文,从而在任何页面上包含SPPageStatusSetter控件。简单的说,我们将创建一个包含SPPageStatusSetter控件的WebPart,在事件接收器中我们将通过设置SPPageStatusSetter的属性来显示消息。
创建WebPart- 新建一个空白SharePoint项目。添加一个WebPart项和一个事件接收器项。在WebPart的代码中添加一个自定义属性和SPPageStatusSetter控件。传递给SPPageStatusSetter控件的状态消息将在事件接受器中通过WebPart的自定义属性进行设置。
WebPart代码:
public class StatusBarWebpart : WebPart
{
SPPageStatusSetter statusBar;
string oMessage;
public StatusBarWebpart()
{ }
[Category("Custom Properties")][Browsable(false)]
public string Message
{
get
{
return oMessage;
}
set
{
oMessage = value;
}
}
protected override void CreateChildControls()
{
statusBar = new SPPageStatusSetter();
statusBar.AddStatus(“Action”, Message, SPPageStatusColor.Blue);
Controls.Add(statusBar);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(“Status Bar demo”);
RenderChildren(writer);
}
}
接下来我们将在事件中动态添加包含SPPageStatus控件的WebPart,并确保在每个事件中通过该WebPart的自定义属性修改SPPageStatusSetter控件的状态。
public class EventRecieverStatus : SPItemEventReceiver
{
///
/// An item is being added.
///
StatusBarWebpart.StatusBarWebpart owp;
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPWeb web = properties.Web;
string oUrl = properties.List.DefaultViewUrl;
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager coll = web.GetLimitedWebPartManager(oUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
if (coll.WebParts.Count > 1)
{
owp = (StatusBarWebpart.StatusBarWebpart)coll.WebParts[1];
if (owp != null)
{
owp.Message = “Item Added”;
coll.SaveChanges(owp);
}
}
else
{
owp = new StatusBarWebpart.StatusBarWebpart();
owp.Message = “Item Added”;
owp.Hidden = true;
coll.AddWebPart(owp, “Left”, 1);
coll.SaveChanges(owp);
}
}
///
/// An item is being updated.
///
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
SPWeb web = properties.Web;
string oUrl = properties.List.DefaultViewUrl;
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager coll = web.GetLimitedWebPartManager(oUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
if (coll.WebParts.Count > 1)
{
owp = (StatusBarWebpart.StatusBarWebpart)coll.WebParts[1];
if (owp != null)
{
owp.Message = “Item Updated”;
coll.SaveChanges(owp);
}
}
else
{
owp = new StatusBarWebpart.StatusBarWebpart();
owp.Message = “Item Updated”;
owp.Hidden = true;
coll.AddWebPart(owp, “Left”, 1);
coll.SaveChanges(owp);
}
}
}
参考资料
浙公网安备 33010602011771号