Sharepoint 如何在visual Web部件中调用SP.UI.Status

Sharepoint 如何在visual Web部件中调用SP.UI.Status,sharepoint,sharepoint-2010,alert,banner,Sharepoint,Sharepoint 2010,Alert,Banner,我有一个场景,需要在列表中显示带有标题列的警报横幅 下面是我试图使用的代码。但它没有正确显示。有人能告诉我吗 <script type="text/ecmascript" language="ecmascript"> var strStatusID; function showInfo(strMessage) { alert("Code sucess"); strStatusID = SP.UI.Status.addStatus(s

我有一个场景,需要在列表中显示带有标题列的警报横幅

下面是我试图使用的代码。但它没有正确显示。有人能告诉我吗

<script type="text/ecmascript" language="ecmascript">

    var strStatusID;

    function showInfo(strMessage) {
        alert("Code sucess");
        strStatusID = SP.UI.Status.addStatus(strMessage, true);
        alert(strMessage.toString());
        SP.UI.Status.setStatusPriColor(strStatusID, "yellow");

    }




</script>
<asp:Panel ID="Panel1" runat="server">

<asp:Label ID="lblScripter" runat="server" Visible="false"></asp:Label>

</asp:Panel>


  public void LoadGrid()
        {
            //System.Diagnostics.Debugger.Break();

            var oSPWeb = SPContext.Current.Web;
            SPList oSpList = oSPWeb.Lists["AlertList"];
            SPQuery oQuery = new SPQuery();
            oQuery.Query = @"<Where><Neq><FieldRef Name='NoOfDays' /><Value Type='Calculated'>0:00</Value></Neq></Where>";
            SPListItemCollection _AlertListCollection = oSpList.GetItems(oQuery);
            DataTable Table_Calendar = _AlertListCollection.GetDataTable();

            if (Table_Calendar != null)
            {

                foreach (SPListItem item in _AlertListCollection)
                {

                    MessageShow = item["Title"].ToString();

                }


               // strStatusID = SP.UI.Status.addStatus(strMessage, true);
                lblScripter.Text = "<Script language='javascript;>showInfo('" + MessageShow + "');</script>";
            }


            else
            {
                lblScripter.Visible = false;


            }

        }

var-strStatusID;
函数showInfo(strMessage){
警报(“代码成功”);
strStatusID=SP.UI.Status.addStatus(strMessage,true);
警报(strMessage.toString());
SP.UI.Status.setStatusPriColor(strStatusID,“黄色”);
}
公共void LoadGrid()
{
//System.Diagnostics.Debugger.Break();
var oSPWeb=SPContext.Current.Web;
SPList-oSpList=oSPWeb.Lists[“AlertList”];
SPQuery oQuery=新建SPQuery();
Query=@“0:00”;
SPListItemCollection\u AlertListCollection=oSpList.GetItems(oQuery);
DataTable_Calendar=_AlertListCollection.GetDataTable();
if(表格日历!=null)
{
foreach(AlertListCollection中的SPListItem项)
{
MessageShow=item[“Title”].ToString();
}
//strStatusID=SP.UI.Status.addStatus(strMessage,true);
lblscript.Text=“尝试设置:
lblscript.Text=@“SP.SOD.executeOrderLayUntilscript加载(函数(){showInfo(“+MessageShow+”);},'SP.js');”

这很可能会解决在加载SP.UI.Status javascript函数之前调用这些函数的问题。

为了实现这一点,我已经解决了此PFB代码。感谢我的朋友Hameed,他帮助我实现了这一点

<script type="text/ecmascript" language="ecmascript">

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RegisterMethods);
    function RegisterMethods() {
        showInfo();
        RemoveLastStatus();
    }
    function showInfo() {
        var strMessage = document.getElementById('<%= Message.ClientID %>').value;
        var strstatus = document.getElementById('<%= StartStatus.ClientID %>').value;

        if (strMessage != null) {
            if (strstatus != "false") {
                var strStatusID;

                strStatusID = SP.UI.Status.addStatus(strMessage);
                SP.UI.Status.setStatusPriColor(strStatusID, "red");
                document.getElementById('<%= StartStatus.ClientID %>').value = "false";
                document.getElementById('<%= StatusId.ClientID %>').value = strStatusID;
            }

        }


    }
    function RemoveLastStatus() {
        var statusId = document.getElementById('<%= StatusId.ClientID %>').value;
        var stopStatus = document.getElementById('<%= StopStatus.ClientID %>').value;
        if (statusId != null) {
            if (stopStatus == "true") {
                SP.UI.Status.removeStatus(statusId);
                statusId = '';
            }
        }
    }
</script>
<asp:Timer runat="server" ID="UpdateTimer" Interval="5000" OnTick="UpdateTimer_Tick" />
<asp:UpdatePanel runat="server" ID="TimedPanel" UpdateMode="Conditional">

    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="UpdateTimer" EventName="Tick" />
    </Triggers>
    <ContentTemplate>

        <asp:Label runat="server" ID="DateStampLabel" />
          <asp:Label ID="lblScripter" runat="server" Visible="True"></asp:Label>
          <asp:HiddenField ID="StartStatus" runat="server" />
          <asp:HiddenField ID="StopStatus" runat="server" />
          <asp:HiddenField ID="Message" runat="server" />
          <asp:HiddenField ID="StatusId" runat="server" />
   </ContentTemplate>
</asp:UpdatePanel>

CODEBEHIND.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using System.Globalization;
using System.Data;
using System.Linq;
using Microsoft.SharePoint.Utilities;

namespace ITBANNER
{
    public partial class ITOpsBannerUserControl : UserControl
    {
        String MessageShow = string.Empty;
        DateTime Enddate, starttime;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateStampLabel.Text = DateTime.Now.ToString();
                DateTime Currenttime = DateTime.Parse(DateStampLabel.Text.ToString());

                var oSPWeb = SPContext.Current.Web;
                SPList oSpList = oSPWeb.Lists["AlertList"];

                SPListItemCollection items = oSpList.Items;
                SPListItem item;
                for (int i = 0; i < items.Count; i++)
                {
                    item = items[i];
                    Console.WriteLine("Index = {0} ID = {1}", i, item.ID);
                }

                SPListItem item = oSpList.GetItemById(3);

                Message.Value = item["Title"].ToString();

                starttime = DateTime.Parse(item["StartDate"].ToString());

                if (Currenttime >= starttime)
                {
                    StartStatus.Value = "true";
                    StopStatus.Value = "false";
                }

            }
        }
        protected void UpdateTimer_Tick(object sender, EventArgs e)
        {

            DateStampLabel.Text = DateTime.Now.ToString();
            DateTime Currenttime = DateTime.Parse(DateStampLabel.Text.ToString());
            var oSPWeb = SPContext.Current.Web;
            SPList oSpList = oSPWeb.Lists["AlertList"];
            SPListItem item = oSpList.GetItemById(3);
            Enddate = DateTime.Parse(item["EndDate"].ToString());
            if (Currenttime >= Enddate)
            {
                StopStatus.Value = "true";

            }

        }

        //public void HideStatusBar()
        //{
        //    string script = "document.onreadystatechange=fnRemoveAllStatus; function fnRemoveAllStatus(){removeAllStatus(true)};";
        //    this.Page.ClientScript.RegisterClientScriptBlock(typeof(Type), "statusBarRemover", script, true);
        //}


    }
}

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RegisterMethods);
函数注册表方法(){
showInfo();
RemoveLastStatus();
}
函数showInfo(){
var strMessage=document.getElementById(“”).value;
var strstatus=document.getElementById(“”).value;
if(strMessage!=null){
if(strstatus!=“false”){
var-strStatusID;
strStatusID=SP.UI.Status.addStatus(strMessage);
SP.UI.Status.setStatusPriColor(strStatusID,“红色”);
document.getElementById(“”).value=“false”;
document.getElementById(“”).value=strStatusID;
}
}
}
函数RemoveLastStatus(){
var statusId=document.getElementById(“”).value;
var stopStatus=document.getElementById(“”).value;
if(statusId!=null){
如果(stopStatus==“true”){
SP.UI.Status.removeStatus(statusId);
statusId='';
}
}
}
CODEBEHIND.cs
使用制度;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用Microsoft.SharePoint;
使用Microsoft.SharePoint.WebControl;
使用System.Collections.Generic;
使用System.Web;
使用系统图;
利用制度全球化;
使用系统数据;
使用System.Linq;
使用Microsoft.SharePoint.Utilities;
名称空间横幅
{
公共部分类ITOpsBannerUserControl:UserControl
{
String MessageShow=String.Empty;
DateTime Enddate,starttime;
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
DateStampLabel.Text=DateTime.Now.ToString();
DateTime Currenttime=DateTime.Parse(DateStampLabel.Text.ToString());
var oSPWeb=SPContext.Current.Web;
SPList-oSpList=oSPWeb.Lists[“AlertList”];
SPListItemCollection items=oSpList.items;
SPListItem项目;
对于(int i=0;i=开始时间)
{
StartStatus.Value=“true”;
StopStatus.Value=“false”;
}
}
}
受保护的void UpdateTimer_Tick(对象发送方、事件参数e)
{
DateStampLabel.Text=DateTime.Now.ToString();
DateTime Currenttime=DateTime.Parse(DateStampLabel.Text.ToString());
var oSPWeb=SPContext.Current.Web;
SPList-oSpList=oSPWeb.Lists[“AlertList”];
SPListItem=oSpList.GetItemById(3);
Enddate=DateTime.Parse(项[“Enddate”].ToString());
如果(当前时间>=结束日期)
{
StopStatus.Value=“true”;
}
}
//公共空间HideStatusBar()
//{
//string script=“document.onreadystatechange=fnRemoveAllStatus;函数fnRemoveAllStatus(){removeAllStatus(true)};”;
//this.Page.ClientScript.RegisterClientScriptBlock(typeof(Type),“statusBarRemover”,script,true);
//}
}
}

Hi-Tex你是一个冠军。它工作起来很有魅力。感谢你的帮助Hi-Tex还有一个问题是,当我在visual Web部件中使用计时器控件时,我无法调用此java脚本。你能帮助我如何在计时器上调用java脚本吗?下面是我的codeHi-Tex关于aove查询的任何帮助。请帮助我,因为这非常紧急。Th谢谢你的合作
 lblScripter.Text = @"<script type=""text/javascript"">SP.SOD.executeOrDelayUntilScriptLoaded(function() {showInfo('" + MessageShow + "');}, 'sp.js');</script>"
<script type="text/ecmascript" language="ecmascript">

    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(RegisterMethods);
    function RegisterMethods() {
        showInfo();
        RemoveLastStatus();
    }
    function showInfo() {
        var strMessage = document.getElementById('<%= Message.ClientID %>').value;
        var strstatus = document.getElementById('<%= StartStatus.ClientID %>').value;

        if (strMessage != null) {
            if (strstatus != "false") {
                var strStatusID;

                strStatusID = SP.UI.Status.addStatus(strMessage);
                SP.UI.Status.setStatusPriColor(strStatusID, "red");
                document.getElementById('<%= StartStatus.ClientID %>').value = "false";
                document.getElementById('<%= StatusId.ClientID %>').value = strStatusID;
            }

        }


    }
    function RemoveLastStatus() {
        var statusId = document.getElementById('<%= StatusId.ClientID %>').value;
        var stopStatus = document.getElementById('<%= StopStatus.ClientID %>').value;
        if (statusId != null) {
            if (stopStatus == "true") {
                SP.UI.Status.removeStatus(statusId);
                statusId = '';
            }
        }
    }
</script>
<asp:Timer runat="server" ID="UpdateTimer" Interval="5000" OnTick="UpdateTimer_Tick" />
<asp:UpdatePanel runat="server" ID="TimedPanel" UpdateMode="Conditional">

    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="UpdateTimer" EventName="Tick" />
    </Triggers>
    <ContentTemplate>

        <asp:Label runat="server" ID="DateStampLabel" />
          <asp:Label ID="lblScripter" runat="server" Visible="True"></asp:Label>
          <asp:HiddenField ID="StartStatus" runat="server" />
          <asp:HiddenField ID="StopStatus" runat="server" />
          <asp:HiddenField ID="Message" runat="server" />
          <asp:HiddenField ID="StatusId" runat="server" />
   </ContentTemplate>
</asp:UpdatePanel>

CODEBEHIND.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using System.Globalization;
using System.Data;
using System.Linq;
using Microsoft.SharePoint.Utilities;

namespace ITBANNER
{
    public partial class ITOpsBannerUserControl : UserControl
    {
        String MessageShow = string.Empty;
        DateTime Enddate, starttime;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateStampLabel.Text = DateTime.Now.ToString();
                DateTime Currenttime = DateTime.Parse(DateStampLabel.Text.ToString());

                var oSPWeb = SPContext.Current.Web;
                SPList oSpList = oSPWeb.Lists["AlertList"];

                SPListItemCollection items = oSpList.Items;
                SPListItem item;
                for (int i = 0; i < items.Count; i++)
                {
                    item = items[i];
                    Console.WriteLine("Index = {0} ID = {1}", i, item.ID);
                }

                SPListItem item = oSpList.GetItemById(3);

                Message.Value = item["Title"].ToString();

                starttime = DateTime.Parse(item["StartDate"].ToString());

                if (Currenttime >= starttime)
                {
                    StartStatus.Value = "true";
                    StopStatus.Value = "false";
                }

            }
        }
        protected void UpdateTimer_Tick(object sender, EventArgs e)
        {

            DateStampLabel.Text = DateTime.Now.ToString();
            DateTime Currenttime = DateTime.Parse(DateStampLabel.Text.ToString());
            var oSPWeb = SPContext.Current.Web;
            SPList oSpList = oSPWeb.Lists["AlertList"];
            SPListItem item = oSpList.GetItemById(3);
            Enddate = DateTime.Parse(item["EndDate"].ToString());
            if (Currenttime >= Enddate)
            {
                StopStatus.Value = "true";

            }

        }

        //public void HideStatusBar()
        //{
        //    string script = "document.onreadystatechange=fnRemoveAllStatus; function fnRemoveAllStatus(){removeAllStatus(true)};";
        //    this.Page.ClientScript.RegisterClientScriptBlock(typeof(Type), "statusBarRemover", script, true);
        //}


    }
}