How would i fire a button click event after a user pressed the OK button in a OK cancel pop up window?? (like return confrim) i used the following code to call the btnCommitSaving click event if the user pressed OK but i doesnt work. is there something wrong with the code?
ClientScriptManager csm = Page.ClientScript;
csm.RegisterClientScriptBlock(this.GetType(), "__Mensagem", "if (confirm('" + "Proceed" + "')){ var button = document.getElementByID(btnCommitSaving);button.click();}", true);
please help :)
解决方案
Just did this earlier today. Easiest way is to use __doPostBack from javascript:
// Assume myButton is an (or anything) previously defined
// that has an OnClick event registered in the code behind
var uniqueID = '';
var additionalData = '';
__doPostBack(uniqueID, arguments);
The arguments come back in Request["__EVENTARGUMENT"] if you're interested in those. The tricky thing is to use UniqueID instead of ClientID with the __doPostBack. A lot of sites tell you to use ClientID, this will do the postback, but won't route into the event handler.
Edit
Another opition is to set OnClientClick on the button to a javascript function that returns true/false:
function someJSFunction()
{
return confirm('Are you sure you want to do that?');
}
This way, the codebehind method will only fire if the JS function returns true.