Server side confirmation window in asp.net

I want to show the confirmation window in asp.net from the server side:

I want to call it from the server side because I need to send a client message from the server. I am using the code below

    string str = "Are you sure, you want to Approve this Record?";
        ClientScript.RegisterStartupScript(typeof(Page), "Popup", "return confirm('" + str + "');",true);
// More code ....

Now it displays a pop-up window and no matter what I click, “ok” or “Cancel”, my code is executed.

Please let me know how I can limit code execution when the user selects "cancel" in the confirmation field.

+3
source share
3 answers

Check this:

CODE BEHIND:

string str = "Are you sure, you want to Approve this Record?";
        this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);

Aspx:

function ConfirmApproval(objMsg)
    {
        if(confirm(objMsg))
        {
            alert("execute code.");
            return true;
        }    
        else
            return false;    
    }
+6
source

check out the following code example.

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Button ID="btnOK" runat="server" Text="OK" onclick="btnOK_Click" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="lblID" PopupControlID="pnlConfirmation">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlConfirmation" runat="server" Style="background-color: #D4D0C8;
    border: 2px solid black; width: 300px; height: 100px;">
    <asp:Label ID="lblID" runat="server"></asp:Label>
    <table cellpadding="2" cellspacing="2" width="100%">
        <tr>
            <td>
                Your confirmation message here.
            </td>
        </tr>
        <tr>
            <td align="center">
                <asp:Button ID="btnOK1" runat="server" Text="OK" onclick="btnOK1_Click" /> <asp:Button ID="btncancel"
                    runat="server" Text="Cancel" onclick="btncancel_Click" />
            </td>
        </tr>
    </table>
</asp:Panel>

Server side .....

protected void btnOK_Click(object sender, EventArgs e)
{
    //some code here.
    //then show modal popup
    ModalPopupExtender1.Show();
}
protected void btnOK1_Click(object sender, EventArgs e)
{
    //If ok some code here to execute
    ModalPopupExtender1.Hide();
}
protected void btncancel_Click(object sender, EventArgs e)
{
    //hide modal popup
    ModalPopupExtender1.Hide();
}

http://forums.asp.net/t/1499789.aspx?confirmation+popup+from+server+side

+1

Javascript cannot access the code for variables / functions / events. Since Sukhi said that a workaround is to use a hidden field.

0
source

Source: https://habr.com/ru/post/1794646/


All Articles