Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Friends,

I have two texboxes like txtname, txtPWD and one checkbox is like

<asp:CheckBox ID="chkTerms" runat="server" />Terms & Conditions

I have working with terms & conditions checkbox.

XML
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>

<script type="text/javascript">
        $(function () {
            $('#btnSubmit').click(function () 
            {
                if ($('#chkTerms').is(':checked')) {

                }
                else {
                    alert('Please accept terms & conditions')
                }
            })
        })
    </script>

<asp:CheckBox ID="chkTerms" runat="server" />Terms & Conditions

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

I have small login code in

C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
}

whenever try to click on submit button if chkTerms is not checked it's submitted successfully. I need alert message if chkTerms not checked.

Please help any one.

Thank you.
Posted
Updated 29-Oct-14 0:19am
v2
Comments
George Jonsson 29-Oct-14 6:23am    
Is there a reason why you use JavaScript for the check instead of doing it in the method btnSubmit_Click in c#?
Mathew Soji 29-Oct-14 6:31am    
What about disabling the submit button and enable the button only if terms and condition checked.

The ID gets changed in ASP.Net after the page is rendered to the browser.

Try this:

JavaScript
<script type="text/javascript">
        $(function () {
            $('#<%=btnSubmit.ClientID%>').click(function () 
            {
                if ($('#<%=chkTerms.ClientID%>').is(':checked')) {
 
                }
                else {
                    alert('Please accept terms & conditions')
                }
            })
        })
    </script>


Note: I haven't tested it . In case this doesn't solve your problem please reply.
 
Share this answer
 
Comments
Member 10021658 29-Oct-14 6:43am    
Dear Suvendu,

Thanks for your reply it's working fine. It's great help me.

Thank you sooo much.
Suvendu Shekhar Giri 29-Oct-14 6:56am    
The ID you assign to an asp.net control is just a programmatic identifier (http://msdn.microsoft.com/en-us/library/system.web.ui.control.id%28v=vs.110%29.aspx)
Actually, in asp.net the IDs get changed in process of converting to render as HTML content. You can check this by right clicking on a element in the browser and clicking "Inspect Element". You will notice that some prefix is added to that ID to make them unique throughout the page as you can have controls of same id in different panels/gridview/datalist etc in asp.net.

Hopefully I have clarified your doubts.
Praveen_P 29-Oct-14 6:44am    
good solution , 5 !
I think you are looking for some thing similar to Calling a JavaScript method in C# button Click Event using ClientScript[^]

You just need to modify this line
return confirm("Do you want to proceed? ");
with
return $('#chkTerms').is(':checked')


Note : Use Client ID of chkTerms in JQuery code or add ClientIDMode="Static" to its definition.
 
Share this answer
 
v2
Hi,

Try this code for your checkbox:
JavaScript
function checkValidity()
{
 var status = $('#chkTerms').is(':checked');
 if(status==false)
 {
  alert('Check Terms and Conditions');
  return false;
 }
 else
 {
  return true;
 }
}


And on clientClick of the button pass this javascript function to check the values.
HTML
OnClientClick="return checkValidity();"


Change your functin as per your requirements.
Hope this helps !! :)

Regards,
Praneet
 
Share this answer
 
Hi...
Write code in your button_click() like:
C#
MsgBox1.confirm("Are you sure want to proceed?");

Thank u.
 
Share this answer
 

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