views:

4541

answers:

2

Hi All,

I have an UpdatePanel with a GridView inside it.

The Gridview has a TemplateColumn containing a Hyperlink.

Using jQuery at the start of the page, I set all of these hyperlinks to become nyroModal modal hyperlinks. I.e. so when you click them, the target page is loaded into a modal dialogue. This code is:

<script type="text/javascript">
    $(document).ready(function(){
        $(".modal").nyroModal();
    });
</script>

If I remove the UpdatePanel, then the above nyromodal code works perfectly well. But with the UpdatePanel there, it stuffs up the nyroModal calls and opens the links as raw entire new windows/tabs in Firefox. The UpdatePanel is trying to be too smart and initiate a postback etc.

Do I have to somehow register the nyromodal call for each button at render time or something to tell .Net/UpdatePanel to "stay the hell away" ? I played around a bit with RegisterClientScriptBlock etc but can't seem to make it happy. It seems like such a hassle to register client side script with .Net server-side code. I find it cumbersome and frustrating :)

Thanks a lot for any suggestions or insight.

+4  A: 

Hello again Aaron, try this instead:

<script type="text/javascript">
    $(document).ready(function(){
        /*
            This binds a callback to the click event of all 
            elements with the 'modal' class, even ones that are
            updated inside an UpdatePanel.
        */
        $(".modal").live('click', function(){
            /*
                When the element is clicked, open the nyroModal dialog
                manually. (If the element is a link, nyroModal will 
                automatically get the content based on the link's href attribute.)
            */
            $(this).nyroModalManual();
        });
    });
</script>

Edit

I've created a page that seems to work fine. The full source code is below.

Note: There's one important thing I didn't to in the code above. I forgot to prevent the link from actually changing the browser location, so note the evt.preventDefault(); in the code below (Sorry about that :P).

<%@ Page Language="C#" %>
<script runat="server">
  public void Page_Load()
  {
    BindGridViewUrls();
  }

  public void GridViewUrls_PageIndexChanging(object sender, GridViewPageEventArgs e)
  {
    GridViewUrls.PageIndex = e.NewPageIndex;
    BindGridViewUrls();  
  }

  public void BindGridViewUrls() {
    var Urls = new[]{
      new {UrlText="Google", Url="http://google.com"},
      new {UrlText="Stack Overflow", Url="http://stackoverflow.com"},
      new {UrlText="XKCD", Url="http://xkcd.com"},
      new {UrlText="Google Reader", Url="http://google.com/reader"},
      new {UrlText="reddit", Url="http://reddit.com"},
      new {UrlText="Hacker News", Url="http://news.ycombinator.com"},
      new {UrlText="Ubuntu", Url="http://ubuntu.com"},
      new {UrlText="Twitter", Url="http://twitter.com"},
      new {UrlText="YouTube", Url="http://youtube.com"},
      new {UrlText="Wikipedia", Url="http://en.wikipedia.org"},
      new {UrlText="Coding Horror", Url="http://codinghorror.com"},
      new {UrlText="Mozilla", Url="http://mozilla.org"},
      new {UrlText="jQuery", Url="http://jquery.com"},
      new {UrlText="NyroModal", Url="http://nyromodal.nyrodev.com/"},
    };
    GridViewUrls.DataSource = Urls;
    GridViewUrls.DataBind();
  }
</script>
<!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>UpdatePanel Test</title>
  <script 
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;
  </script>
  <script src="jquery.nyroModal-1.4.2.js"></script>
  <script>
    $(function(){
      $('.modal').live('click', function(evt){
        $(this).nyroModalManual({minWidth:750});
        evt.preventDefault();
      });
    });
  </script>
</head>
<body>
  <form runat="server">
  <div>
    <asp:ScriptManager runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel runat="server">
      <ContentTemplate>
        <asp:GridView runat="server" ID="GridViewUrls" AllowPaging="true"
          PageSize="5" OnPageIndexChanging="GridViewUrls_PageIndexChanging">
          <Columns>
            <asp:HyperLinkField DataTextField="UrlText"
              DataNavigateUrlFields="Url" ControlStyle-CssClass="modal"
            />
          </Columns>
        </asp:GridView>
      </ContentTemplate>
    </asp:UpdatePanel>
  </div>
  </form>
</body>
</html>
brianpeiris
Hi again Brian! Thanks again for your reply.I tried this code. Now a modal kind of appears, but it contains the red error text just saying "The requested content cannot be loaded. Please try again later. Close.". Then that dialog sits there for about 2 seconds, then a postback still appears to happen and the target page reloads in a whole new browser window.So (A) The modal is trying to pop up this time (great).(B) But it can't fetch the page for some reason; AND(C) The postback still seems to go ahead regardless and interfere.Thanks again, any thoughts?
Aaron
I'm about to go insane. I really don't enjoy working with ASP.Net and all of its bloat and inflexibility! I am stuck with it for work. I have a simple gridview, which in real money is a "HTML Table", nothing more. Why can't ASP.Net stay the hell away and let my javascript code operate like PHP or any other "real" server-side development platform would. ASP.Net - "The point and click programmer's web dev language". Its shockingly "not there yet".
Aaron
UpdatePanel is gone. I'm refusing to use it now. Its too overcomplicated just to achieve a simple task. Sure its super "easy" to just drag and drop an UpdatePanel onto your page. But its the "UpdatePanel Hell" you have to go through when you want to "get underneath the hood" and do some non-standard things that kills you.MS UpdatePanel = EPIC FAIL.
Aaron
Don't Panic! I've updated my answer with full source code for an aspx page that seems to work. Try it and let me know if it works for you. If not, it's probably something about your link URLs that's causing the problem.
brianpeiris
Brian :) :)preventDefault() fixes the problem!I.e. "turn off whatever other default javascript events/calls were attached to this" - exactly what I needed.I was going insane with this one. I am glad there is a nice jQuery call to achieve exactly this.I still believe ASP.Net interferes way too much and you should be able to bind your own client-side JS calls without worrying about what bloat ASP.Net might have auto-inserted.You've made my day and I definitely owe you a beer. You can message me your address if you like and I'll send you a present in the post! :) Seriously.Cheers.
Aaron
Ah, good to hear it worked. Sorry I forgot to include preventDefault in the first place, I shouldn't have air-coded it.I appreciate the offer for a present! But the opportunity to help you is more than enough reward. (No to mention the stackoverflow reputation points ;) ). Happy coding.
brianpeiris
Ah no worries :) Thanks again..
Aaron
A: 

I suggest you use the dialog from jquery UI.

Refer this for how to use it: http://jqueryui.com/demos/dialog

I have implemented something similar to what you want using dialog and it works fine. You only have to add the dialog back to the form like this

jQuery(".modal").each(function() { var popup = jQuery(this); popup.parent().appendTo(jQuery("form:first")); });

nunespascal
Hi nunespascal! Thanks a lot for your response!I am using nyromodal right throughout my project, so for consistency of UI design, I need to stick with it. Its working great everywhere, except on this page :(I feel that the nyromodal is basically working fine, but its ASP.Net's fault for interfering.Any other thoughts?Thanks again!
Aaron

related questions