System.Web.UI.ScriptManager.RegisterStartupScript只执行一次

时间:2015-04-10 18:40:37

标签: javascript c# sharepoint code-behind scriptmanager

我有一个奇怪的问题。我在Sharepoint页面上使用JavaScript。我参考以下JavaScript代码:

var statusId = '';
var notifyId = '';

function AddNotification(message) {
    notifyId = SP.UI.Notify.addNotification(message, true);
}

function RemoveNotification() {
    SP.UI.Notify.removeNotification(notifyId);
    notifyId  = '';
}

function AddStatus(message) {
    statusId = SP.UI.Status.addStatus(message);
    SP.UI.Status.setStatusPriColor(statusId, 'red');
}

function RemoveLastStatus() {
    SP.UI.Status.removeStatus(statusId);
    statusId = '';
}

function RemoveAllStatus() {
    SP.UI.Status.removeAllStatus(true);
}

然后,当用户点击某个按钮时,会显示一条消息"请稍候..."。在调用C#方法退出之前,它应该删除通知。像这样:

protected void SaveButton_Click(object sender, EventArgs e)
{
       System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "Notif", "AddNotification('" + Core.Classes.ResourceHelper.LoadResource(Core.Classes.ResourceName.PleaseWaitString) + "');", true);
       //Busiess logic...
       if (ActivityDate.SelectedDate == null || //Date required
           ActivityProjectnumber.SelectedIndex == 0 || //Project number required
           ActivityActivity.Text == string.Empty || //Activity description required
           EndTime.SelectedDate.Hour < StartTime.SelectedDate.Hour ||    //
           EndTime.SelectedDate.Hour == StartTime.SelectedDate.Hour &&   //Start time should not be less or equal end time
           EndTime.SelectedDate.Minute <= StartTime.SelectedDate.Minute) //
       {
           StatusSetter.SetPageStatus(Core.Classes.ResourceHelper.LoadResource(Core.Classes.ResourceName.CheckRequiredFieldsString), Core.Classes.ResourceHelper.LoadResource(Core.Classes.ResourceName.WarningString), this.Controls);
           return;
       }

       //If business logic passed, save item.
       SaveItem();

       System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "Notif", "RemoveNotification();", true); //Problem lies here...
}

用户单击按钮时会显示通知。但它并没有消失。我调试了代码,相应的行肯定正在执行。我怀疑它与我在一种方法中使用ScriptManager.RegisterStartupScript两次有关。但我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

您需要为每个脚本指定不同的名称(RegisterStartupScript的第3个参数)。