3

我想从服务器端在 asp.net 中显示确认框:

我想从服务器端调用它,因为我必须从服务器端获取客户消息。我正在使用下面的代码

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

现在它正在显示弹出窗口,无论我单击什么,无论是“确定”还是“取消”,我的代码都在执行。

请让我知道当用户在确认框中选择“取消”时如何限制要执行的代码。

4

2 回答 2

6

一探究竟:

代码背后:

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;    
    }
于 2011-02-25T07:27:14.933 回答
1

检查以下示例代码。

<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>

在服务器端......

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

于 2015-10-04T13:10:07.553 回答