0

I want to execute following code on button click but the follwing code executes when I refresh the page as well. I don't want that behaviour. Please help me.

string str = "Are you sure, you want to Approve this Record?";
this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);
2
  • Just to make sure. You want when you click a button to get a confirm message first. Is that right? Commented Jul 15, 2011 at 9:58
  • yes but i dont want when i refresh the page Commented Jul 15, 2011 at 9:58

4 Answers 4

6

your question in quite unclear. I assumed you use ASP.NET C# , here is the way:

public static class ClientMessageBox
{

    public static void Show(string message, Control owner)
    {
        Page page = (owner as Page) ?? owner.Page;
        if (page == null) return;

        page.ClientScript.RegisterStartupScript(owner.GetType(),
            "ShowMessage", string.Format("<script type='text/javascript'>alert('{0}')</script>",
            message));

    }

}

then, in your page (remember to reference the class above):

protected void Page_Load(object sender, EventArgs e)
{
    ClientMessageBox.Show("Hello World", this);
}

See if it helps in your case.

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

Comments

3

You should look at putting your alert in to the OnClientClick method of the button.

E.g. (add in your other required attributes and click handler)

<asp:Button ID="ApproveButton" runat="server" CausesValidation="False"
    OnClientClick="return confirm('Are you certain you want to approve this record?');" />

If the user clicks OK your normal click code will run but if they cancel the function returns false so the click won't be processed.

2 Comments

it is working fine on button click but sorry i am using gridveiw control. when i press delete event on gridview control then i want confirmation message is it possible
@user746909 Have a look at this answer stackoverflow.com/questions/2458351/…. I think it solves the same problem
1

Add OnClientClick to your button

<asp:Button OnClientClick="return confirm('Are you sure, you want to Approve this Record?')" runat="server" />

Comments

0

You should be using marto'ss and Chris W's code. If you want a clean HTML Markup, use this.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string script = "javascript:return confirm('Are you certain you want to approve this record?');";
        //id of your button
        button.Attributes.Add("onclick", script):
    }
}

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.