Wednesday, January 2, 2008

Verify a condition is true before posting asp:checkbox

If you want to verify a condition before the AutoPostBack event is fired for an asp:checkbox.

Set up the javascript function of what you want to verify:
<script language="javascript">
  function Reviewed_Click(o, e)
  {
    var oResponse = document.getElementById("Response");
    if (oResponse != null)
    {
      var reg = /\s+/g;
      var value = oResponse.innerText.toLowerCase().replace(reg, "");
      if (value == "yes"
      && !confirm("A question to ask?\nIf \"Yes\" click OK, otherwise click CANCEL."))
      {
        event.returnValue = false;
        return false;
      }
    }
  return true;
}
</script>

--------------------------------------------------------------
Now add the asp:checkbox...

<asp:checkbox id="Reviewed" Runat="server" AutoPostBack="True" Text="Information Reviewed" onclick="if (!Reviewed_Click(this, event)) return false;"></asp:checkbox>

No comments: