Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I use status bar to show exception message in code-behind. I wanna add a close button in the status bar for user, so they can remove the bar. Here is my code:

        protected virtual void ShowNotifyException(Severity severity, string errorMessage)
    {
        string statusBar = @"
        ExecuteOrDelayUntilScriptLoaded(function(){{
            ExecuteOrDelayUntilScriptLoaded(
                function(){{
                    this.statusID = SP.UI.Status.addStatus(""{0}"", ""<p><span style='float: left'>{1}</span><span style='float: right'><input type='button' name='cancelStatusBar' value='X' onclick='SP.UI.Status.removeStatus(this.statusID);'/></span></p>"");
                    SP.UI.Status.setStatusPriColor(this.statusID, ""{2}"");
                }},
            'core.js'
            )}},
        'sp.js'
        );";

        string color = "";
        string title = "";
        if (severity.Equals(Severity.Error))
        {
            color = "red";
            title = EMC.Documentum.SharePoint.Core.ResourceManagement.ResourceManager.GetLocalString("StatusBarTitle_Error");
        }
        else if (severity.Equals(Severity.Warn))
        {
            color = "yellow";
            title = EMC.Documentum.SharePoint.Core.ResourceManagement.ResourceManager.GetLocalString("StatusBarTitle_Warn");
        }
        else if (severity.Equals(Severity.Info))
        {
            color = "blue";
            title = EMC.Documentum.SharePoint.Core.ResourceManagement.ResourceManager.GetLocalString("StatusBarTitle_Info");
        }
        else 
        {
            color = "green";
        }
        string script = string.Format(statusBar, title, errorMessage, color);
        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "ShowErrorMessage", script, true);
    }

The question is, 1. the button is displayed correctly, but it doesn't work. 2. Except add button, is there any other way to implement my requirement?

share|improve this question
    
I don't wanna add timeout for the status bar, because our requirement is to remove status bar from a button. –  ChrisLiu May 24 '13 at 12:03
add comment

1 Answer

up vote 1 down vote accepted

I separate the JS to a standalone file, and include a function:

var statusID='';
var addStatusBar = function(title, message, color){
var messageHtml = '<p><span style="float: left"><b>' + title + '</b>' + message + '</span><span style="float: right" id="close_button_span"></span></p>';
statusID = SP.UI.Status.addStatus(messageHtml);
SP.UI.Status.setStatusPriColor(statusID, color);
var close_button = document.createElement('a');
var linkText = document.createTextNode("close");
close_button.appendChild(linkText);
close_button.name = 'status_close_button';
close_button.href = "#";
close_button.onclick = function(){
    SP.UI.Status.removeStatus(statusID);
    statusID = '';
}
var statusBar = document.getElementById('close_button_span');
statusBar.appendChild(close_button);};

Then register the js file in CreateChildControls with ScriptLink control, and use below statement to call the js function:

ScriptManager.RegisterClientScriptBlock(Page, GetType(), "ShowErrorMessage", "ExecuteOrDelayUntilScriptLoaded(function() {{addStatusBar('Error: ', 'message', 'red')}}, 'sp.js');", true);

The button in status bar will be worked.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.