Reputation: 105
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);
Upvotes: 0
Views: 18898
Reputation: 55200
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):
}
}
Upvotes: 0
Reputation: 4170
Add OnClientClick
to your button
<asp:Button OnClientClick="return confirm('Are you sure, you want to Approve this Record?')" runat="server" />
Upvotes: 1
Reputation: 3314
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.
Upvotes: 3
Reputation: 54212
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.
Upvotes: 6