views:

57

answers:

1

Integrating SimpleModal with ASP.NET and MasterPages

This is a continuation of a previous thread found here.

Thanks to the help I received previously the code worked in a single page and I was able to use SimpleModal. But my application has MasterPages, so I pasted it into another test form. The results are different then the test I ran without a MasterPage. Without the MasterPage the modal opened and stayed open until closed by the user. With this MasterPage version the modal opens for only one second and then closes. Anybody know why?

Below is a default sample master page. No edits were done.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Test.master.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

And here is the jquery call from web page.

Note: this contains exactly the same jquery code from the previous post I mentioned above except I am now pointing to local .js files.

<%@ Page Title="" Language="C#" MasterPageFile="~/test.master" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <style type="text/css">
        #simplemodal-overlay {background-color:#000;}
        #simplemodal-container {background-color:#333; border:8px solid #444; padding:12px;}
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div>
        Email: <input type="text" id="email" /><br />
        Message: <input type="text" id="message" /><br />
        <button id='theModal'>Show</button>
        <div id="sample" style="display:none"> 
            <h2>Sample Data</h2> 
            <p>Your email was successful!!</p> 
            <p>You can press ESC to close this dialog or click <a href="#" class="simplemodal-close">close</a>.</p> 
        </div> 
    </div>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jquery.simplemodal.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $("#theModal").click(function() {
          $("#sample").modal({
            opacity: 80,
            overlayCss: { backgroundColor: "#fff" }
          });
        });
      });
    </script>
</asp:Content>

I tried moving the declarations to the MasterPage. It also resulted in a flashing modal. Not sure why as I am a complete noob when it comes to jquery.

Any help would be greatly appreciated.

Victor

+1  A: 

I think I have an answer for you.

The previous sample was a plain HTML page that had a div or two and some input elements.

However, moving the sample to an ASPX master page / content placeholder scheme introduced a form element that wrapped the content. When your page is rendered, you'll see your contentplaceholder inside a form element, like so:

<form method="post" action="test2.aspx" id="form1">
<!-- your content is contained here -->
</form>

As a result, when you clicked the button to show the simplemodal dialog, the form was being submitted...at least, it was with Chrome. I didn't test it on FireFox, but IE8 was not re-submitting the form. I'm assuming this is because the button that's being used to open the dialog is a button element; this apparently causes a form in Chrome to submit, but not in IE8. (That's an assumption -- I don't know that for sure.)

The fix is to prevent the default action of a button click in the button click event handler. It's very simple to do - here's the updated code:

$(document).ready(function () {
    $("#theModal").click(function (e) {  //e = the element that raised the event (button)
        e.preventDefault();  //prevent the default action of the button click
        $("#sample").modal({
            opacity: 80,
            overlayCss: { backgroundColor: "#fff" }
        });
    });
});

The click handler's function now takes an argument that represents the element that raised the event (e -- the button). The first thing I then do is to prevent the default action of the button click from happening (e.preventDefault();). Then, I continue on with displaying the modal. This does the trick! I tested this on Chrome and IE8 and both worked fine.

Hopefully this helps. Let me know if you have other questions about this and I'll update my answer accordingly. Good luck!!

David Hoerster
D Hoerster,Thank you, you've found the issue and it does indeed work in FireFox. Again, your example worked first time with no adaptation needed right out of the box. My gratitude goes beyond my ability to express here, so I will simply say thank you again for your generosity, and have a great end of week up there in my hometown in PA.Sincerely,Victor
asus3000
Thanks, Victor. Glad it worked for you. Us Pittsburghers have to stick together! :) Don't forget to mark this as a correct answer if it helped. It helps your acceptance rate, which attracts more potential answers for future questions. (And it helps both of our reputation, too -- I have to be honest.) Anyway, good luck again!!
David Hoerster
ah yes, thanks for the reminder.. Done. I'm originally from Monroeville BTW.. Went to Gateway High. You? Moved to Florida in the 80's. Miss the burgh, yinz are generally good people! lol - And I like to think I still have that in me as well. Victor
asus3000
Canevin Catholic - I'm from the West Hills (out by the airport). Probably not a lot has changed since you left -- the Pirates are still losing! (except for a few years in the late 80s and early 90s) I'm one of the few fans left.
David Hoerster
D Hoerster, This is the first time I'm not using runat="server" in my application. The submit button is client side and I think from what you said previously, I'm going to need a WCF service to execute the stored procedure and send the email. Here goes the learning curve again. I'm using a production shared server host and questioning if all this is worth the trouble circumventing the simplicity of asp.net. If you can tell me that the curve won't be too steep (with my limited time to get into production), I'll take the leap. Otherwise back out of jquery and notify another way. -Victor
asus3000
I'd hate to recommend a path for you one way or another, but I will say that using jQuery w/ WCF services is not a simple transition from traditional ASP.NET postbacks. It's a slightly different mindset, at least from what I've seen on my team. There really is no event model (which I like) and it forces a more clean break between functionality and presentation (which I also like). At least for me, once I "got it", I didn't want to go back. :) But it is not a simple transition, I'll be honest. You may want to try script services (the ASP.NET AJAX stuff) where you can have more...
David Hoerster
...traditional web services in your ASPX or ASMX (web service) code-behind and call them from your javascript. That may be a less steep learning curve. Dave Ward (Encosia.com) has a couple of good posts on this: http://encosia.com/2010/03/03/asmx-and-json-common-mistakes-and-misconceptions/ and http://encosia.com/2010/03/08/asmx-scriptservice-mistakes-installation-and-configuration/ Hopefully this helps out.
David Hoerster
Well I would like to get some jquery w/WCF under my belt, so to speak. It really looks nice on my site. Here is your sample: http://edutuit.com/corp/ContactUs.aspx (just hit submit)I'm afraid ASMX is possibly already becoming deprecated by WCF. If I'm going to take the time to learn something it might as well be the one with the brighter future.For me the best place to start is with a working demo, and you've provided that for SimpleModal, for which I am very grateful. I shall give jquery w/WCF a try. I'll give myself a week to successfully exec a stored proc. and send an email. :) Victor
asus3000
I think that's the right direction to take. And the ContactUs page looks good! If you want a good walk-through of hooking up jQuery with a WCF REST service, Rick Strahl does a great job of detailed blog posts. Here's one that intro's calling WCF via jQuery AJAX (lots of code!) -- http://west-wind.com/weblog/posts/324917.aspx. You can follow me on Twitter if you have add'l questions -- @DavidHoerster
David Hoerster
I'm following you on twitter now, and I see you're the jquery w/WCF man to know. (I'm @vdelprete) Someone from my host community recommended this as a tutorial to setup WCF: http://tinyurl.com/5fyqzl which I'll start with. I'll keep you abreast of my progress. Thanks for taking an interest, it's a huge confidence boost knowing you're there. Victor
asus3000