Reputation: 7611
in a GridView (ASP.NET/C#), I have a number of template fields - the 2 ones relevant to the question are 'checkbox1' and 'quantity'. The checkbox starts off as unticked for every row, and the quantity starts off as disabled for every row. But when the user ticks one of the rows checkboxes, I need a piece of JavaScript or something to check if the relevant rows checkbox is checked, and if so enable to the rows quantity textbox.
How do I do this?
Upvotes: 0
Views: 3881
Reputation: 3775
Make sure you start with your quantity disabled. I am assuming it is a 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>
Add this piece of javascript on your page:
<script type="text/javascript">
function ChangeQuantityEnable(id, enable) {
document.getElementById(id).disabled = !enable;
}
</script>
Then in the RowDataBound event handler for your gridview, add
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);
}
Upvotes: 1