You can do it easily with jQuery. With a little change, you can make it work the way you want it to.
First add the following to the <head> :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $(".ddlClass").change(function () { var txt = $(this).closest("tr").find(".txtClass"); if ($(this).val() == 0) { txt.css("background", "#cccccc"); txt.attr("disabled", "disabled"); } else { txt.css("background", "#ffffff"); txt.attr("disabled",""); } }); });
Then create your gridview and add template columns for your text box and drop-down list. In the code below, note that the "ddlClass" class was assigned to the drop-down list, and the "txtClass" class was provided to the text box. You can change them as you wish.
<asp:gridview runat="server" ID="gvw" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="field1" /> <asp:BoundField DataField="field2" /> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:TextBox runat="server" ID="txtName" CssClass="txtClass"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <select class="ddlClass"> <option value="1">Enabled</option> <option value="0">Disabled</option> </select> </ItemTemplate> </asp:TemplateField> </Columns> </asp:gridview>
The .ready function attaches a click event to each drop-down list with the class "ddlClass". When changing, the code will find a text field with the class "txtClass" on the same line as the drop-down list, and then enable / disable accordingly.
source share