Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to validate checkbox in my project.
Am using following code,


C#
protected void buttonOk_Click(object sender, EventArgs e)
        {
            if (!chkbox.Checked)
            {
                 buttonOkk.Attributes.Add("OnClick",
                "javascript:return chkbox()");
            }
            else
            {
                Response.Redirect(xxxxx, false);
            }
        }


XML
<head id="Head1" runat="server">
    <title>xxxx Page</title>

   <script type="text/javascript">
  function checkCheckBoxes()
  {
        alert ('Please check');
  }

 </script>

</head>



but while am running the code in my page when i check first time without checking check box it is not showing alert.But second time when i do same am getting alert.
Am getting alert message for both case (Checked/unchecked). I want to show alert only if checkbox is unchecked.
please let me know the solution fast.
please share if you have any good ideas.
Can i show error message using c# code?
Posted
Updated 15-May-13 20:11pm
v2
Comments
Am Gayathri 16-May-13 2:52am    
What could be the reason for above case?

Hi Kavitha,

If you can use jQuery in you project, then please look into the below link for example.
http://jsfiddle.net/M9dXx/[^]


Thank you,
Vamsi
 
Share this answer
 
Comments
Am Gayathri 16-May-13 2:52am    
What could be the reason for above case?
Am Gayathri 16-May-13 2:52am    
Why hitting the function on both time ?(check\uncheck)
RelicV 16-May-13 3:04am    
Hmm. looking at your code, only on button click, you are adding the onclick function to the button, but not on the pageload. So, if the checkbox is not checked and the first button click will add the onclick attribute to the button. From now onwards, the JS function will work. Put the onclick attribute in pageload inside if(!IsPostback) and check it.
RelicV 16-May-13 3:11am    
<table>
<tr>
<td><asp:CheckBox ID="chbxSelect" runat="server" /></td>
<td><asp:Button ID="btnSelect" runat="server" Text="Confirm"/></td>
</tr>
</table>

<script>
function checkCheckBoxes()
{
if(!document.getElementById('<%=chbxSelect.ClientID').checked)
{
alert ('Please check');
return false;
}
else
return true;
}
</script>

----------------------------------------------------------------

Code behind in .cs file

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnSelect.Attributes.Add("OnClick", "javascript:return checkCheckBoxes();");
}
}

protected void buttonOk_Click(object sender, EventArgs e)
{
Response.Redirect(xxxxx, false);
}

Try this one.!
RelicV 16-May-13 7:41am    
got it working..?
 
Share this answer
 
Comments
Am Gayathri 16-May-13 2:52am    
What could be the reason for above case?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900