Wednesday, June 3, 2009

[jQuery] Triggering asp:Button click event from client side

Hi,

I have a page with an asp:Button save that performs an ajax validation
before post back. If the data is incorrect, the aspx returns a
warning text message. The user can ignore the warning and continue to
save the data.

I managed to perform the desired result using the posted code below.
Notice that the button click event is called 2 times first when the
user trigger the event, then the 2nd time by target.click() after
setting the doCheck and response values.

I am not happy with the code. Is there any other way to replace
target.click() with some thing which by pass the validation again?


var doCheck = true;
var response = true;

$(document).ready(function() {
$("input[name$='ajaxConfirm']").bind("click", function(e)
{
var target = $(e.target);

if (doCheck) {

$.get("GetConfirmation.aspx", { param1: "test",
param2: "data" },
function(result) {
if (result != "") {
response = confirm(result);
}
doCheck = false;
target.click(); //replace this
}
, "text");

e.preventDefault();
}
else {
if (!response)
e.preventDefault();

doCheck = true;
response = true;
}

});

-- Generated asp:Button

<input type="submit" name="ctl00$conMain$ajaxConfirm"
value="Ajax Confirm" id="ctl00_conMain_ajaxConfirm" />

No comments: