Stop execution of a page

12,848

Don't do Response.End(). Just return without doing anything.

Share:
12,848
Kash
Author by

Kash

Updated on June 04, 2022

Comments

  • Kash
    Kash almost 2 years

    I have a task board, some person is working on some task, if task is assigned to another person by his manager the first person who is working on the task board, his execution should be stopped, and a message should be displayed that "This task is assigned to some one else."

    I tried using following in page load.

    //Code Behind
    if (!Owner)
    {
        SomecontrolsToHide();
        MessageDisplay();    // JavaScript function call using RegisterStartupScript()
        Response.End();    
    }
    
    protected void MessageDisplay()
    {
        string dbMessage = "Task is assigned to someone else.";
        ClientScriptManager cs = Page.ClientScript;
        cs.RegisterStartupScript(typeof(Page), "ShowMessageWrapup_" + UniqueID, "showMessageDisplay('','" + dbMessage + "');", true);   
    }
    
    // JavaScript function that displays message.
    function showMessageDisplay(args, displayMessage) {
        if (displayMessage != "") {                        
            document.getElementById("spanMessage").innerHTML = displayMessage;
            document.getElementById("spanMessage").style.display = 'inline';
        }
    }
    

    It stops the execution but message is not displayed and Controls are not hidden too.

    What should I do?

  • Kash
    Kash over 12 years
    how, as i have different events on the page. for example user clicked a button, then how can i return without executing button's click event.
  • Piotr Perak
    Piotr Perak over 12 years
    That's where you should put if (!Owner).
  • Kash
    Kash over 12 years
    you mean like if(!Owner) { return; }
  • Kash
    Kash over 12 years
    return will just return from page_load event but button's click event will still fire.
  • Piotr Perak
    Piotr Perak over 12 years
    if you do sth on button click that's where you should do what should be done - in btn click handler. Not in page_load. In click handler you check if (!Ownher) and hide controls and return.
  • Kash
    Kash over 12 years
    I have multiple events on page, then I need to check (!Owner) condition in every event. I want to avoid that, so I want to do that in page_load.
  • Piotr Perak
    Piotr Perak over 12 years
    You should put logic in one place.