Question

In my current project I've got a workflow in which I need to show two consecutive MessageBoxes to the user. The first one is more like a warning, that informs the user of the risk of the operation he tries do execute. I am using the ClientClickEvent of a link button to let the user confirm the operation:

<asp:LinkButton runat="server" id="lockorder"
    CommandName='LockOrder'
    OnClientClick="return confirm('Do you really want to lock this order?');" />

If the user confirms that he is sure to perform the operation, the application performs some validation from code behind. The result can be, that there are some unhandled tasks. This is valid, but the user should get informed about it, to have the possibility to finish those tasks. So I show a second confirmation dialog, but this time from code behind:

if (tasksOpen)
{
    ScriptManager.RegisterClientScriptBlock(
        this.Page,
        typeof(Page),
        "Onload",
        "var confirmationResult = window.confirm('There are some open tasks left, which might get closed. Do you really want to lock this order?'); if (confirmationResult) __doPostBack('ConfirmLockOrder', confirmationResult);",
        true);
    return;
}

For better readability here is the JS-snippet that get's handed to the client:

var confirmationResult 
    = window.confirm('There are some open tasks left, which might get closed. Do you really want to lock this order?'); 

if (confirmationResult) 
    __doPostBack('ConfirmLockOrder', confirmationResult);

Back again in code behind I am receiving this command (e.g. inside the Page_Load event) and continue with the actual operations. All worked fine, but with some change I've done it stopped performing the postback.

I've took a look at the client script using FireBug. The __doPostBack-function (which is apparently auto-generated) tells me that WebForm_OnSubmit is not defined. I think it is not defined, because the confirmation dialog get's shown in the OnLoad-event and thus before the form is actually loaded. But why does the __doPostBack-function even need to check if this event performed, if it is not defined? Can I customize the __doPostBack-behaviour? Or should I choose another event? Or am I completely wrong here?

I hope somebody can point me into the right direction!

Was it helpful?

Solution

Based on the error (and some educated guesses), your JavaScript code that you are registering via ScriptManager is firing before the rest of the JavaScript code generated by ASP.NET is loaded. Hence, WebForm_OnLoad is undefined.

You need to make certain that the web page is completely loaded before your code executes. Change your code to the following:

window.onload=function(){
    var confirmationResult = window.confirm('There are some open tasks left, '
        + 'which might get closed. Do you really want to lock this order?'); 

    if (confirmationResult) 
        __doPostBack('ConfirmLockOrder', confirmationResult);
}

*I broke the confirm string into two, just for display purposes.

If you use jQuery, $(document).ready is preferred over window.onload.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top