0

I'm having few checkboxes which are dynamically created using c# code- behind. At client side using jquery I want to make the checkbox as checked, but the checked value is not reflected on the UI. I have browsed for the solution and it suggest something related to VIEWSTATE. Here is my C# code and Jquery where I'm setting the Checkbox value.

C#. Code

CheckBox checkbox = new CheckBox();
            checkbox.ID = "CheckBox" + i++;
            checkbox.InputAttributes["class"] = "skin-line-grey icheck-label form-label";
            checkbox.ClientIDMode = ClientIDMode.Static;
            checkbox.Text = item.Text;
            PnlEventList.Controls.Add(checkbox);

Jquery. Checkbox1 is the ID for checkbox been dynamically created. Same I have 7 more checkboxes.

$('#CheckBox1').prop('checked', true);
7
  • Did you try using class instead of id? Commented Feb 1, 2017 at 9:41
  • Have you included jquery reference file in your page ? Commented Feb 1, 2017 at 9:47
  • Take care of case sensitivity. Checkbox1 or CheckBox1 Commented Feb 1, 2017 at 9:49
  • 1
    Make sure jQuery is included and when code is executed the checkbox is present in DOM, $('#CheckBox1').length should be one if it is present in dom. Commented Feb 1, 2017 at 9:53
  • Yes I have tried using a class instead of id and I have also given jquery reference file as I have already used a lot of javascript into my application. Commented Feb 1, 2017 at 9:53

2 Answers 2

2

CheckBox property, lying not in properties or attribute collection. Use this:

$('#CheckBox1')[0].checked = true;

Or this:

document.getElementById("CheckBox1").checked = true;
Sign up to request clarification or add additional context in comments.

3 Comments

As I already mentioned before. The Checked attribute is getting true but it is not reflecting on the UI. The checkbox is still shown un-checked and vice-versa.
@Sergio $('#CheckBox1').checked = true; would give error as jQuery object returned by selector does not have property .checked. It could be $('#CheckBox1')[0].checked = true;
This works, There was a problem with my css style so the value was not reflecting with my UI.
-1

Try this: $('#Checkbox1').prop('checked', true); This is case sensitive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.