fussy fussy - 2 months ago 24
Javascript Question

Click Gridview button using jQuery

I have an asp datagrid with a remove button and I want to click on the button, enable the modal for the confirmation message and when okay is clicked in the modal, to call the normal C# code behind that would've been called if the modal was not there. I'm trying to achieve this using jQuery. Below is the code:

Gridview:

<asp:linkbutton id="btnRemoveProperty" runat="server" class="btnRemove" ClientIDMode ="AutoID" CommandName="RemoveProperty"> REMOVE <asp:linkbutton>


jQuery:

var btnConfirm;
var button;
$(".btnRemove").click(function () {
if (btnConfirm == 1) {
btnConfirm = "";
return true;
}
else {
button = $(this).attr('id');
$("modal_delProperty").show();
return false;
}
});

$("#btnDelete.click(function () { //button inside the modal
btnConfirm = 1;
$("#" + button).click(); //is not doing anything
});


The problem is that $("#" + button).click() is not doing anything even though it has the gridview button ID. It should have called the $(".btnRemove").click and then the code behind would've been called.

Answer

Try this:

$("#btnDelete").click(function () { //button inside the modal
  btnConfirm = 1;
  var el = $("#" + button); 
  $(el)[0].click();
  return false;
  });