2

I'd like to disable all elements within a <tr> element when a checkbox inside it is checked. My code is as follows:

<tr>
    <td style="width:25px" id="td_CheckBoxClickable">
        <asp:CheckBox runat="server" ClientIDMode="Static" ID="CheckBox_EnableRow" OnCheckedChanged="CheckBox_EnableRow_CheckedChanged" />
    </td>
    <td>
        <asp:TextBox runat="server" ID="textBox_Count" />
    </td>
    <td>
        <asp:TextBox runat="server" ID="textBox_Value" />
    </td>
</tr>

and currently the CheckBox_EnableRow_CheckedChanged event is empty.

I was thinking it would be possible to look at the parent of the parent of the checkbox like this:

protected void CheckBox_EnableRow_CheckedChanged(object sender, EventArgs e) {
    TableRow row = (TableRow)((CheckBox)sender).Parent.Parent;
    row.Enabled = false;
}

but that doesn't seem to work.

4

3 回答 3

2

I would recommend using jquery. Using jquery you can find the parent tr element of the checkbox, then you change the attribute of all input elements inside the tr element to disabled=true. First you have to give tr a class, say class="trclass", then you have to get the parent tr for the checkbox.

html part:

<input id="Checkbox1" type="checkbox" onchange="DsiableRow(this)" />

javascript part:

function DsiableRow(MyCHeckbox)
{
   var trElement = $(MyCHeckbox).parents(".trclass");
   $("trElement  :input").attr("disabled", true);
}
于 2013-08-26T07:22:41.033 回答
1

I would solve it like this:

<input id="theCheckBox" type="checkbox" onchange="DisableRow(this)" />

function DisableRow(checkBox)
{
   var $trElement= $(checkBox).closest("tr");
   $trElement.find("input").attr("disabled", "disabled");
}

Differences Between HTML and XHTML
In XHTML, attribute minimization is forbidden, and the disabled attribute must be defined as .
Source: http://www.w3schools.com/tags/att_input_disabled.asp

Also note, if you use the default submitting method in the browser then:

Tip: Disabled elements in a form will not be submitted.
Source: http://www.w3schools.com/tags/att_input_disabled.asp

于 2013-08-26T07:45:39.773 回答
0

Use javascript to get the <tr> and make it as display:none

于 2013-08-26T07:41:07.983 回答