1

I have a class. There is only deleteRecord function

protected virtual void DeleteRecord
{
    if(..)
    {}
    else(..)
    {
         Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language='javascript'>alert('Are you sure?')</script>", true);
    }
}

I want to show javascript message. But I think I made a mistake. How can I do it?

1
  • Can you be a little more specific? Have you debugged with your browser to see the resulting output? Commented Jun 27, 2013 at 14:43

2 Answers 2

1

You've added true to the last parameter on Page.ClientScript.RegisterStartupScript which is addScriptTags. See http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

Therefore you have essentially added <script> within a <script>

Try this:

Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Are you sure?')", true);

Also ensure the key parameter is unique to the page. If you already have a StartupScript with the key of "Alert" then this can also stop it from calling the JavaScript code.

Sign up to request clarification or add additional context in comments.

2 Comments

You could also mention that this makes no sense at all. The user wants to delete a record, so he clicks on a delete-button, a postback happens, the server sends a javascript to the client with an alert('Are you sure?'). He clicks ok or cancel but nothing will happen in either way.
Very true @TimSchmelter. To be honest I skipped that only because the code example provided isn't very clear on the implementation. If this server-side approach is essential then @StackOver consider using a return confirm('are you sure'); to only postback when the dialog is confirmed.
0

I created this static class that I can call from any web page: (I use AJAX Toolkit for script manager, but you can use the default one in ASP.NET as well)

public static class ClientJS {
    public static void send(string js) {
        Page page = HttpContext.Current.Handler as Page;
        ToolkitScriptManager.RegisterStartupScript(page, page.GetType(), Guid.NewGuid().ToString(), "setTimeout(function(){" + js + "},1);", true);
    }
}

Use it like so:

ClientJS.send("alert('Are you sure?');");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.