26

Closed

ModalPopupExtender "Object Expected" Bug

description

The follow code results in an "Object Expected" javascript error:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title>Untitled Page</title> </head> <body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <asp:UpdatePanel runat="server">
     <ContentTemplate>
        <asp:Panel runat="server" id="ModalTest" style="width:200px;height:200px;border:1px;">
            <span>This is a test</span>
        <asp:TextBox runat="server" ID="txtTestInput" ></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="rfvTestInput" ControlToValidate="txtTestInput" />
        </asp:Panel>
        <asp:Button ID="btnPop" runat="server" Text="Submit" />
        <asp:ValidationSummary ID="vs" runat="server" />
        <cc1:ModalPopupExtender ID="MPE" runat="server" TargetControlID="btnPop" PopupControlID="ModalTest" >
        </cc1:ModalPopupExtender>
     </ContentTemplate>
    </asp:UpdatePanel>
</form>
</body>
</html>
<script runat="server"> protected void Page_Load(object sender, EventArgs e) { MPE.Show(); } </script> The cause of the error can be tracked back to two specific statements of code that get emitted:

document.getElementById('vs').dispose = function() {
Array.remove(Page_ValidationSummaries, document.getElementById('vs'));
}
(function() {var fn = function() {AjaxControlToolkit.ModalPopupBehavior.invokeViaServer('MPE', true); Sys.Application.remove_load(fn);};Sys.Application.add_load(fn);})();

The first part is the result of the RegisterValidatorDeclaration from the "BaseValidator" class. The second line is the results of the ChangeVisibility method on the ModalPopupExtender. The error occurs because the anonymous function from the first statement IS NOT TERMINATED with a semicolon AND the run time views the second line as part of the same statement (due mostly to the fact that it is a weird construct...an immediate anonymous function). Correct output should appear as follows:

document.getElementById('vs').dispose = function() {
Array.remove(Page_ValidationSummaries, document.getElementById('vs'));
};
(function() {var fn = function() {AjaxControlToolkit.ModalPopupBehavior.invokeViaServer('MPE', true); Sys.Application.remove_load(fn);};Sys.Application.add_load(fn);})();

Pat
Closed May 12, 2009 at 7:48 PM by
Issue resolved in latest checked-in source code.

comments

saravanan_rose wrote Oct 19, 2007 at 6:52 PM

Yup, Pat is correct. I too got the same issue. I could not open a MPE from code behind, if the page has a validation summary. A quick fix is disbaling the validation summary before opening the MPE in code behind. It opens ine from client side.

carternc wrote Oct 24, 2007 at 4:18 PM

Is there a workaround that will allow me to use validation summary? Disabling the validation summary before opening the MPE in the code behind does indeed allow the MPE to open as desired however, I'd like to find a workaround that allows the validation summary to function as well.

roger_leong wrote Dec 29, 2007 at 3:15 PM

A temporary workaround would be disabling client script on validation summary

pgrinsell wrote Jan 2, 2008 at 10:46 PM

Sorry, should have posted the workaround. Right after the validation summary I put an empty label. I then hooked into the pre-render event of the label and put in this code:
    void lblShim_PreRender(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "shim", ";", true);
    }
Basically, this just inserts a semi-colon where it should have been to begin with. If/when they fix this the extra semi-colon won't do any harm. Note that the problem lies in not in the control toolkit javascript but the Microsoft Ajax framework which probably means eons before this thing gets fixed...although a workaround could be put into the toolkit like adding an extra semi-colon like I did.

Pat

DSJoel wrote Jan 11, 2008 at 12:59 PM

Hi Pat, I took your code and implemented it in control that inherits from ValidationSummary to make it a bit easier to implement in a larger application, thought i'd post it here for the benefit of anyone still stuck on this one:
[ToolboxData("")]
public class AjaxValidationSummary : ValidationSummary
{
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ID, ";", true);
    }
}
Seems to work fine across our app.

quitchat wrote Mar 5, 2008 at 3:55 AM

Was having exactly the same problem and I will try these work-arounds.
Thanks.


Wondering how long it will take for the next release/fix to come out.

quitchat wrote Jun 28, 2008 at 8:04 AM

Another suggestion to DSJoel:
better use
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ClientID, ";", true);
instead of
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ID, ";", true);

in case there are multiple validation summary control with the same ID rendered on the same page...

Alex.

dapoussin wrote Dec 11, 2008 at 1:52 PM

I also had this problem and solved it by putting in an update panel the button triggering the modalpup.

smkowalczyk wrote Jan 21, 2009 at 7:57 PM

Thanks DSJoel and quickchat.

I replaced my ValidationSummary controls in my project with this custom one as suggested. Hopefully, the Toolkit team will eventually escalate this to the ASP.NET AJAX team.

ToolboxData("")]
public class AjaxValidationSummary : ValidationSummary
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), this.ClientID, ";", true);
}
}