How can I get to the javascript postback event call for an asp button?
<ItemTemplate>
<asp:Button id="btnTest" runat="server"
Text="button" OnClientClick="
customAsyncConfirmDialog(function (isconfirmed)
{
if(isconfirmed)
{
//call this buttons postback event
<%# Page.ClientScript.GetPostBackEventReference(new PostBackOptions(Container.FindControl("btnTest")))%>;
}
});return false;
"/>
</ItemTemplate>
</asp:DataList>
Update: The customAsyncConfirmDialog is an asynchronous function that does not block the javascript to wait for a response.
No need to do that. Just return true and the postback handler will be hit. Ex:
<ItemTemplate>
<asp:Button id="btnTest" runat="server"
Text="button" OnClientClick="
customConfirmDialog(function (isconfirmed)
{
return isconfirmed;
});
"/>
</ItemTemplate>
I'm having trouble accessing what I wrote earlier, but here's a quick example based on what you wrote in your other answer. You might need to change a few things but you should get the idea:
window.confirm = function(question, ok, cancel) {
var defer = $.Deferred();
ok = ok || 'OK'; //setup text for the confirmation
cancel = cancel || 'Cancel';
$(document).ready(function() {
$('#customMessage').text(question);
var btnOk = $('#btnCustomOK');
var btnCancel = $('#btnCustomCancel');
btnOk.val(ok);
btnCancel.val(cancel);
btnOk.unbind("click"); //prevents chaining of events if multiple confirms are used
btnCancel.unbind("click");
btnOk.click(function() {
$('#MyConfirm').hide();
defer.resolve(true); //gets passed as isconfirmed=true
return false; //prevent postback
});
btnCancel.click(function() {
$('#MyConfirm').hide();
defer.resolve(false);
return false; //prevents postback
});
$('#MyConfirm').show();
btnOk.focus();
});
return defer.promise();
}
Then to use:
<asp:Button id="btnTest" runat="server" Text="button"
OnClientClick="confirm('My Message blah blah').then(//this runs after ok/cancel is clicked, thanks to defer.resolve() and defer.promise().
function(isConfirmed) {
if (isConfirmed)
__doPostBack(source.id,'');
}); return false;//return false here to prevent the initial postback from occurring"/>
</ItemTemplate>
the 'source.id' part isn't exactly right, but the part with the promises should help you out.
I was able to get it to call the postback with the following:
<script type="text/javascript">
function customConfirmDialog(sourceButton, callback) {
setTimeout(function () { callback(sourceButton, true); }, 1);
return false;
}
</script>
<asp:DataList ID="dlTest" runat="server">
<ItemTemplate>
<%# Page.ClientScript.GetPostBackEventReference(new PostBackOptions(Container.FindControl("btnTest")))%>;
<asp:LinkButton runat="server" ID="neededfor__dopostbackgenerationforsomereason"></asp:LinkButton>
<asp:Button id="btnTest" runat="server"
Text="button"
OnClick="btnTest_OnClick"
OnClientClick="
return customConfirmDialog(this,function (sourceButton, isconfirmed)
{
if(isconfirmed)
{
//call this button's postback event
__doPostBack(sourceButton.name,'');
}
});
"/>
</ItemTemplate>
</asp:DataList>
I had to add a linkbutton to get __doPostBack
to generate, not sure why the ASP:Button alone didn't trigger that. I still couldn't use the Page.ClientScript.GetPostBackEventReference
in the onClientClick
function.
Final note, you need to call __doPostBack
with sourceButton.name
rather that sourceButton.id
to trigger the correct server side event.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.