1

I'm noticing that not all of my Javacript - all of which worked in a basic ASP.NET Web Forms application - is working. I'm not sure what could be causing this. For example, the following JS/C# calling code does nothing in the web part:

Javascript:

// Close an open dialog.
function CloseAddDialog() {
    alert('i am about to close the dlg');
    $("#dlgAddParts").dialog("close");
};

C#:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), UniqueID, "CloseAddDialog();", true);

So no alert, and the dialog doesn't close.

Yet the following code does work:

Javascript:

// Show the Add Part dialog on button click.
function ShowAddPartDialog() {
    $("#dlgAddParts").dialog({
        title: "Add New Part",
        modal: true,
        width: 'auto'
    });
    $("#dlgAddParts").parent().appendTo($("form:first"));
};

C#:

Page.ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowAddPartDialog();", true);

I checked the Chrome Dev Tools console and saw two errors being thrown when the web part is started up:

Uncaught ReferenceError: documentGetElementsByName is not defined

Uncaught ReferenceError: MoveSiteTitle is not defined

I've researched these errors on the net, and they seem to have something to do with site banners. SharePoint replaces any banner you use with theirs, and this code is often to blame. However, I do not find either of these methods in my project - it is something hard-wired into the web part page itself. It doesn't seem related but I'd like to resolve it if possible.

No errors popup when a Javascript command does not succeed. In case this was a syntax issue with my .js file, I ran the entire contents of it through a JS syntax checker, and no issues were found.

Any help greatly appreciated. Using SharePoint 2010.

  • Using OnClientClick="CloseAddDialog();" in the button markup actually closes the dialog, but using ScriptManager.RegisterClientScriptBlock in C# doesn't? – Paul Oct 8 '14 at 13:59
2

If you're using an UpdatePanel on your web part, make sure any RegisterClientScriptBlock calls on controls within that UpdatePanel use the UpdatePanel as the control parameter, rather than 'this' - I.E.:

ScriptManager.RegisterClientScriptBlock(updatePanel, updatePanel.GetType(), UniqueID, "CloseAddDialog();", true);

I have changed my code as above for controls contained in update panels, and this works great every time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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