Question

My *.aspx Page (main page):

function getEmployeeHours(employeeId,year,month) {
 $('#clocked-details').load('/Employees/GetEmployeeClockedHours/',{ 'employeeId': employeeId,'year': year,'month': month });
    };

My partial view *.ascx :

 <td>
   <button id="btnSave" type="button" class="actionButton">
     Save</button>
  </td>

As above code snippet I need to accsess partial view btnSave from main view to trigger click event.

I have written below code inside the main view.

$('#btnSave').off('click').on('click', function () {

         var yearValue = $("#year").val();
         var monthValue = $("#month").val();

         $.ajax({
             url: "/Employees/UpdateEmployeeClockedHoursByProvider",
             type: 'POST',
             cache: false,
             data: { employeeId: employeeId, year: yearValue, month: monthValue },
             success: function (result) {

             },
             error: function (xhr, ajaxOptions, thrownError) {
                 alert(xhr.status);
                 alert(thrownError);
             }
         });

         return false;
     });

But it doesn't fire.Here I am using load jquery method to load my partial view asynchronously.

So my question is how should I fire click event of partial view ?

Was it helpful?

Solution 2

Since the usercontrol containging the button is being loaded dynamically, you can use ("#btnSave").live("click"){...} event of jquery while keeping the script in the main view, which can keep track of the controls being added to the DOM in future. So whenever a control matching the specified selector for "live(..)" appears, the event is wiredup automatically.

Hope this help you.

OTHER TIPS

Since your html is being loaded into the page dynamically you'll have to give more context to your DOM query.

Try this.

$(document).on('click', '#bntSave', function(){
  //do stuff
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top