mercredi 2 décembre 2015

javascript postback event on disabled checkbox prevents serverside event

I am working on a asp.net page that has a user control that generates checkboxes. Certain checkboxes are dependant on other checkboxes. For example if checkbox1 is checked, checkbox2 needs to be checked and disabled. To store the checkbox checked value, I the viewstate is set using the serverside CheckedChanged event.

The client side javascript function is as following:

function runcheckboxcheck(checkbox1, checkbox2) {
     if (checkbox.checked) {
         checkbox2.checked = true;
         // checkbox2.disabled = true;
         __doPostBack(checkbox2.id, '');
     }
     else {
          checkbox2.disabled = false;
     }
}

the server side CheckedChanged event implements the following C# function:

void tx_CheckedChanged(object sender, EventArgs e)
{
    CheckBox box = sender as CheckBox;
    if(box !=null)
    {
        ViewState[box.ID] = box.Checked;
    }
}

I also need to disable the secondary checkbox. This is done in the commented out line: // checkbox2.disabled = true;. My problem is that if I this line is enabled, the server side CheckedChanged event no longer is raised, as the checkbox is recreated.

How can I disable the secondary checkbox, while still having the server side CheckedChanged event being raised for this checkbox? Or is there a beter way to store the information on client side for example? and how do I access this information on the server side again? Or can i enable or disable the checkbox in another way than i currently am?




Aucun commentaire:

Enregistrer un commentaire