选中GridView复选框时启用GridView列

时间:2010-10-14 13:16:52

标签: c# javascript asp.net gridview

在GridView(ASP.NET / C#)中,我有许多模板字段 - 与问题相关的2个是'checkbox1'和'quantity'。对于每一行,复选框以未开启的方式开始,并且数量从每行的禁用开始。但是当用户勾选其中一个行复选框时,我需要一段JavaScript或其他东西来检查是否选中了相关行复选框,如果是,则启用行数量文本框。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

确保从禁用数量开始。我假设它是一个TextBox:

<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="checkbox1" runat="server" />
    </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
    <ItemTemplate>
        <asp:TextBox ID="quantity" runat="server" Enabled="false" />
    </ItemTemplate>
</asp:TemplateField>

在您的网页上添加此javascript:

<script type="text/javascript">

    function ChangeQuantityEnable(id, enable) {
        document.getElementById(id).disabled = !enable;
    }

</script>

然后在gridview的RowDataBound事件处理程序中,添加

if (e.Row.RowType == DataControlRowType.DataRow)
{
    CheckBox chk = (CheckBox)e.Row.FindControl("checkbox1");
    TextBox txt = (TextBox)e.Row.FindControl("quantity");

    chk.Attributes["onclick"] = string.Format("ChangeQuantityEnable('{0}', this.checked);", txt.ClientID);
}
相关问题