0

I'm trying to set the checked property of checkboxes with jQuery, the jQuery code is working fine, but I got the problem in codebehind:

particular.click(function () { company.removeAttr('checked'); particular.attr('checked', 'true'); }); 

Company and particular are the names of checkboxes, the jQuery code is working fine, my problem is in codebehind (.cs file)

if (particular.Checked) { company_name_blank.EnableClientScript = false; cif_blank.EnableClientScript = false; } 

This is not working because Checked property is set on false when actually I'm setting it on true as you can see in the jQuery code, so where is the mistake?? For more explanation, I'm trying to disable some RequiredFieldValidators depending on which checkbox is checked, but as I told, Checked property is always set on false unless I set it on true by default in the element Checked=true, or particular.Checked=true in codebehind file, but that is not what I want to. I want to set on true the Checked property when I click the checkbox that's why I used jQuery.

UPDATE

My code

protected void Page_Load(object sender, EventArgs e) { if (particular.Checked) { company_name_blank.EnableClientScript = false; cif_blank.EnableClientScript = false; contact_name_blank.EnableClientScript = false; contact_cognames_blank.EnableClientScript = false; } } 
9
  • Can you add a little bit more code that stands around the code you've pasted? Can you also show your page_load function please? Commented Jul 26, 2013 at 9:37
  • protected void Page_Load(object sender, EventArgs e) { if (particular.Checked) { company_name_blank.EnableClientScript = false; cif_blank.EnableClientScript = false; contact_name_blank.EnableClientScript = false; contact_cognames_blank.EnableClientScript = false; } } Commented Jul 26, 2013 at 9:40
  • If particular is checkbox, use change event instead of click event Commented Jul 26, 2013 at 9:40
  • Have you tried using .prop() instead of .attr() ? Commented Jul 26, 2013 at 9:41
  • How do you get to your code behind? Do you have a button to submit your form? Commented Jul 26, 2013 at 9:42

2 Answers 2

1

Try to use :checked filter

particular.is(':checked') 
Sign up to request clarification or add additional context in comments.

Comments

0

In your website you will have a button like this:

<asp:Button runat="server" ID="btnClick" OnClick="btnClick_Click" Text="Click here" /> 

In your code behind you should have something like this:

protected void btnClick_Click(object sender, EventArgs e) { if (particular.Checked) { company_name_blank.EnableClientScript = false; cif_blank.EnableClientScript = false; contact_name_blank.EnableClientScript = false; contact_cognames_blank.EnableClientScript = false; } } 

You can remove the code in Page_Load and move it to the button click function

UPDATE - Correct solution for question

The correct solution is using the ValidationEnable Javascript function to disable/enable RequiredFieldValidators.

Code for this solution:

var emailEmpty = $("#registerContent").find('[id$=email_blank]'); //This is a jQuery object ValidationEnable(emailEmpty[0], false); //This will disable the validator. 

The reason we use "[0]" is because we need the DOM Element that is inside the jQuery object (see: http://api.jquery.com/get/?rdfrom=http%3A%2F%2Fdocs.jquery.com%2Fmw%2Findex.php%3Ftitle%3DCore%2Fget%26redirect%3Dno)

6 Comments

think it won't slove my problem because I need to disable some RequiredFieldValidator depending on which checkbox is selected because I have some of them hidden so for example when I click particular checkbox I show some textboxes, if I click company checkbox I'll show other textboxes. So my problem is that I have all the RequiredFieldValidators in the same page so when I try to submit the form I need to disable(or force to validate them) the RequiredFieldValidators which are managing the hidden textboxes. Do you understand what I'm trying to??
Okey, got it :) You need to disable your validators via Javascript. You can use the function ValidatorEnable that is provided in Javascript when you have validators present. An example: geekswithblogs.net/jonasb/archive/2006/08/11/87708.aspx Hope that is what you are looking for!
Ty, that's exactly what I'm looking for, I've just tried this: company.change(function () { $("#part").css("background-color", ""); $("#comp").css("background-color", "rgb(159,209,207)"); particular.removeAttr('checked'); company.attr('checked', 'checked'); userName.hide(); cogname.hide(); companyName.show(); cif.show(); contactName.show(); contactCogname.show(); ValidatorEnable(emailEmpty, false); }); But the ErrorMessage is still fire, so it means that the Validator is still able, what am I doing bad??? Ty again ^^
How did you define emailEmpty ?
var emailEmpty = $("#registerContent").find('[id$=email_blank]'); I made sure it works fine because I tried with emailEmpty.hide() and it disappeared, so I think the problem is in ValidatorEnable(emailEmpty, false) why?? I don't know, hope you can help me to fix that.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.