If you want an easy way to remind your user to check if all the information on a form is correct after clicking the button, or if you want a second "are you sure?" question to appear after clicking a button before a significant event is carried out (deleting a large amount of records), then you can use this code to add a JavaScript alert pop-up to any ASP.NET button control. Works in IE, don't know about the others yet.
<%@ Page Language="C#" %>
<script runat="server">
public void PageLoad(Object sender, EventArgs E) {
btnSubmit.Attributes.Add("onclick","javascript:if(confirm('Are you sure everything is correct?')== false) return false;");
}
void btnSubmitClick(object sender, EventArgs e) {
Message.Text = "You entered your name as: " + txtName.Text;
}
</script>
<html>
<head>
</head>
<body>
<form runat="server">
Name: <asp:Textbox id="txtName" runat="server"/>
<asp:Button id="btnSubmit" onclick="btnSubmit_Click" runat="server" Text="Submit"></asp:Button><br/>
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
Comments