0

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

下面是我试图使用的代码。但它显示不正确。谁能告诉我Errorl

<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;


            }

        }

<asp:Timer runat="server" ID="UpdateTimer" Interval="6000" 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"></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
     protected void UpdateTimer_Tick(object sender, EventArgs e)
            {
                System.Diagnostics.Debugger.Break();
                DateStampLabel.Text = DateTime.Now.ToString();
                DateTime Currenttime = DateTime.Parse(DateStampLabel.Text.ToString());

                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();
                        Enddate = DateTime.Parse(item["EndDate"].ToString());
                    }



                    lblScripter.Text = @"<script type=""text/javascript"">SP.SOD.executeOrDelayUntilScriptLoaded(function() {showInfo('" + MessageShow + "');}, 'sp.js');</script>";

                    if (Currenttime >= Enddate)
                    {

                        lblScripter.Text = "hi after date tim refreshed";
                        lblScripter.Text = @"<script type=""text/javascript"">SP.SOD.executeOrDelayUntilScriptLoaded(function() {removeAllInfos();}, 'sp.js');</script>";

                    }

                }


                //else
                //{
                //    lblScripter.Visible = false;


                //}



            }
4

2 回答 2

1

尝试设置:

 lblScripter.Text = @"<script type=""text/javascript"">SP.SOD.executeOrDelayUntilScriptLoaded(function() {showInfo('" + MessageShow + "');}, 'sp.js');</script>"

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

于 2013-04-28T09:15:06.593 回答
0

为了实现这一点,我已经解决了这个 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);
        //}


    }
}
于 2013-05-11T08:40:40.750 回答