views:

125

answers:

2

I am trying to get JQuery events to work with partial views in ASP.NET MVC. However, after you load a partial view through Ajax, JQuery seems not able to fire events for any of the elements in the partial view. I suspect that this issue will also happen if you are using other frameworks or JavaScript libraries to load partial html code with Ajax.

For instance, consider the following example:

Controller:

 public class TestController : Controller
    {

    public ActionResult Index()
    {
        return View();
    }

    public ActionResult LoadPartialView()
    {
        return View("PartialView");
    }
}

Index.aspx

     <script type="text/javascript">
         $(document).ready(function() {
             $("#button").click(function() {
                 alert('button clicked');
             });
         });   
     </script>    

     <div>
     <%using(Ajax.BeginForm("LoadPartialView", new AjaxOptions { UpdateTargetId="PartialView"})){ %>
        Click <input type="submit" value="here" /> to load partial view.
     <% } %>
     </div>
     <br /><br />
     <% Html.RenderPartial("PartialView"); %>

PartialView.ascx

<div id="PartialView">
   Click <input type="button" id="button" value="here"></input> to display a javascript message.
</div>

Once the page loads for the first time, you can click on the "Click here to display a Javascript message" and you will get a Javascript alert message that says "button clicked". However, once you click on the "Click here to load partial view", clicking on the button that is supposed to bring the Javascript alert message doesn't have any effect. It seems that the 'click' event is not being fired anymore.

Does anyone know why this issue occurs with JQuery and how to fix? This issue also occurs with other JQuery plugins that use events.

+2  A: 

The easiest way to handle this would be to utilize the live() method:

<script type="text/javascript"> 
         $(document).ready(function() { 
             $("#button").live('click', function() { 
                 alert('button clicked'); 
             }); 
         });    
</script>  
mkedobbs
Awesome. This does indeed work. According to the official documentation live() can be used to attach a handler to the event for all elements which match the current selector, now or in the future.The only drawback is that the other people who wrote the plug-ins didn't apparently use the live method. Hence, to make it work with existing plug-ins, someone would probably need to dig in the source code and tweak it.
dritan
True. That or after the content is added to the page, you could call the plugin again instead of just on page load. If the answer suffices, please mark it as accepted...
mkedobbs
A: 

Your answer is correct. However, loading a plug-in or an event even after the content (partial view) is loaded in the page through Ajax doesn't work. You have to use live() for all of your event handling code to make it work.

By the way, how do I mark the your answer as accepted? I don't think I signed with an OpenID.

dritan