polterguy/phosphorusfive

p5.ajax Exception with MasterPage usage

janek-lopez opened this issue · 5 comments

When you have a MasterPage with <% ... %> code blocks you get an exception

Steps to reproduce:

Create a standard VS 2015 WebForm Project.

Create a new WebForm that use the default Site.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Phosp5.SiteMaster" %>

<!DOCTYPE html>

<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %> - My ASP.NET Application</title>

    <asp:PlaceHolder runat="server">
        <%: Scripts.Render("~/bundles/modernizr") %>
    </asp:PlaceHolder>

    <webopt:bundlereference runat="server" path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />

</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="respond" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>

        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" runat="server" href="~/">Application name</a>
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li><a runat="server" href="~/">Home</a></li>
                        <li><a runat="server" href="~/About">About</a></li>
                        <li><a runat="server" href="~/Contact">Contact</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="container body-content">
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
            <hr />
            <footer>
                <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
            </footer>
        </div>

    </form>
</body>
</html>

When you run the project you'll get an Exception in StatePersister.cs line 61

Thx, I'll take a look at both of these immediately :)

Hmmm, just fixed your other issue, but the example you provide here, seems to be having lots of references to things not related to the actual bug (jQuery inclusions, etc)

Do you think you could create a reproducible that demonstrates the bug, without references to other things, not related to the issue itself ...?

Please ...?

Files:

DemoMaster.Master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="DemoMaster.master.cs" Inherits="Phosp5.DemoMaster" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        Current Date: <%= System.DateTime.Now %>
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

WebForm1.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/DemoMaster.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Phosp5.WebForm1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 <p5:Literal 
      runat="server"
      id="foo"
      onclick="foo_onclick"
      Element="button">Click me!</p5:Literal>    
</asp:Content>

WebForm1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Phosp5
{
    public partial class WebForm1 : p5.ajax.core.AjaxPage
    {
        [p5.ajax.core.WebMethod]
        public void foo_onclick(p5.ajax.widgets.Literal sender, EventArgs args)
        {
            sender.innerValue = "Hello World from Managed Ajax!";
        }
    }
}

If you execute this you'll get an Exception in line 61 of StatePersister.cs the exception is caused because of line 11 of DemoMaster.Master

Current Date: <%= System.DateTime.Now %>

If you remove that line the page execute correctly but when you click the button you'll get another exception in line 262 of Widget.cs

 throw new ApplicationException ("Method + '" + eventHandlerName + "' could not be found");

It was a little bit "tricky" to fix in fact, but there is now fresh code to be found here, that hopefully fixes your problems.

I still haven't tested all possible permutations, but I've got some 500+ unit tests in System42, which I'll run before I create the next release. All the "p5.ajax-samples" Unit tests came out green though, so I doubt there'll be problems.

In fact, as an additional bonus, I chose to also implement support for creating custom WebControls, using composition, implying that (I think) it should now work creating also event handlers for children controls, inside of parent controls, such that you can create your own extension WebControls, composing them of a bunch of other p5.ajax widgets, and handle events upwards somewhere in the parent hierarchy. It's not as slick as the MyEvent += delegate syntax as you're used to from traditional WebControls, since you'll have to reference these methods by name (string, using the subscript operator, the same way you'd add a named reference in your .ASPX syntax) - But it's none the less a nice feature I believe.

In addition, they should work inside of UserControls, and most other scenarios I can think of ...

I'll probably wrap up a new release within a week or so, just need to finish cleaning up this and this first.

I haven't tested creating custom WebControls myself, since it's kind of outside of my normal everyday usage myself, since I exclusively use this in combination with Hyperlambda, and the rest of Phosphorus Five - But I think most cases should be covered ...

Keep me in the loop if something doesn't behave as expected ... :)

Did it work for you ...?