disable preselected checkbox

190 Views Asked by At

I have these lines of code which select checkbox based on preselected flag

if (objX.PreSelected)
{
    string js = "if(document.getElementById('" + CheckBoxId + "').checked==false) document.getElementById('" + CheckBoxId + "').click();";
    if (!Page.ClientScript.IsStartupScriptRegistered(CheckBoxId))
         Page.ClientScript.RegisterStartupScript(this.GetType(), CheckBoxId, js, true);
}

after this is done I need to disable that preselected checkbox. I tried below but it did not work

string js = "if(document.getElementById('" + CheckBoxId + "').checked==false) {document.getElementById('" + CheckBoxId + "').click();document.getElementById('" + CheckBoxId + "').disabled ='disabled';}";

UPDATE - I could disable that checkbox but problem is it's not sent back to server. I am new to asp.net programming, please suggest.Any help would be appreciated.

1

There are 1 best solutions below

0
On BEST ANSWER

Do you need to do this with javascript? Otherwise you could do something like:

if (objX.PreSelected)
{
    checkBox.Checked = true;
    checkBox.Enabled = false;
}

or

checkBox.Checked = objX.PreSelected;
checkBox.Enabled = objX.PreSelected == false;

To make a CheckBox disabled using JavaScript you can write

document.getElementById('id').disabled = 'disabled';

or

document.getElementById('id').disabled = true;

No need to do both .click() and .checked = true;