开发者

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

开发者 https://www.devze.com 2022-12-14 10:58 出处:网络
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\')\")

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开发者_C百科 is true then I want the message box with ok/cancel to appear.

f.e.:

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.

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? Then rather than writing the JavaScript yourself, you can just set the Enabled property in your code-behind e.g.

<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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消