简体   繁体   English

在ASP.NET中调用弹出的“确认”消息框

[英]Invoking a pop-up 'confirm' message box in ASP.NET

I know how to this when a button is clicked. 单击按钮后,我知道该怎么做。 For example: 例如:

imgBtnDelete.Attributes.Add("onclick", "return confirm('Please confirm you want to delete the letter')")

My question is: 我的问题是:

Say I have a piece of code-behind not related to clicking,calculating the value of a boolean, and if the boolean is true then I want the message box with ok/cancel to appear. 假设我有一段与单击无关的代码,计算布尔值,如果布尔值为true,那么我希望显示“确定/取消”消息框。

fe: fe:

bool hasMoney = ...

if (hasMoney)
{
\\message box..
}

How do I do it? 我该怎么做?

You can do something like this: 您可以执行以下操作:

private void OpenConfirmPrompt()
{
    string strScript = "<script language=JavaScript>alert('File Uploaded');</script>";

    if (!ClientScript.IsStartupScriptRegistered("open"))
    {
        ClientScript.RegisterStartupScript(typeof(Page), "open", strScript);
    }
}

And change the JS there to suit. 并在那里更改JS以使其适合。

So call that function from a server side event. 因此,请从服务器端事件中调用该函数。

Edit, much better explanation than me here 编辑,比我这里更好的解释

Why not use the Ajax Control Toolkit's ConfirmButton extender? 为什么不使用Ajax Control Toolkit的ConfirmButton扩展器? Then rather than writing the JavaScript yourself, you can just set the Enabled property in your code-behind eg 然后,您不必自己编写JavaScript,而只需在代码隐藏区中设置Enabled属性即可,例如

<asp:Button runat="server" ID="MyButton" Text="My Button" />
<ajaxtoolkit:ConfirmButtonExtender runat="server" id="MyButtonConfirmExtender" TargetControlID="MyButton" ConfirmText="Continue?" />

and

if (whatever)
{
    MyButtonConfirmExtender.Enabled = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM