In this example we are going to ask the user if he/she really wants to clear the input on button click
First, add this script to your code behind
<script type="text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Are you sure you want to clear all your input?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
</script>
Second, on your button, add the Confirm() class
<asp:button id="btnClear" runat="server" onclick="OnConfirm" onclientclick="Confirm()"
NOTE IT IS IMPORTANT TO SPECIFY THE onClick and onClientclick
Third and final step is right click your page and click view code and add this function:
public void OnConfirm(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
if (confirmValue == "Yes")
{
}
else
{
}
}
That's it, you're good to go!
SIDENOTE: There is no cancel button, since basically it has the same functionality with no button.