Non Editable Fields in ASP.NET GridView

In this article, let us see how to make some fields editable and non editable on CheckBox selection/unselection inside a GridView.

In this example, I have 3 columns inside a GridView. Column 1 with CheckBoxes, 2 and 3 with TextBoxes. If I check the box, the TextBox inside column 3 should become non-editable.

    <asp:GridView AutoGenerateColumns="false" ID="GridView_Classes" runat="server" OnRowDataBound="GridView_Classes_RowDataBound">

        <Columns>

            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="3%" HeaderStyle-BackColor="steelblue"

                HeaderStyle-ForeColor="White" HeaderStyle-BorderWidth="0" HeaderStyle-BorderStyle="Dashed"

                HeaderStyle-BorderColor="Black">

                <ItemTemplate>

                    <asp:CheckBox ID="CheckBox_Select" runat="server" /></ItemTemplate>

            </asp:TemplateField>

            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Editable" ItemStyle-Width="30%"

                HeaderStyle-BackColor="steelblue" HeaderStyle-ForeColor="White" HeaderStyle-BorderWidth="0"

                HeaderStyle-BorderStyle="Dashed" HeaderStyle-BorderColor="Black">

                <ItemTemplate>

                    <asp:TextBox ID="TextBox_Editable" runat="server" /></ItemTemplate>

                <HeaderStyle HorizontalAlign="Left" />

                <ItemStyle Width="10%" />

            </asp:TemplateField>

            <asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="NonEditable" ItemStyle-Width="30%"

                HeaderStyle-BackColor="steelblue" HeaderStyle-ForeColor="White" HeaderStyle-BorderWidth="0"

                HeaderStyle-BorderStyle="Dashed" HeaderStyle-BorderColor="Black">

                <ItemTemplate>

                    <asp:TextBox ID="TextBox_NonEditable" runat="server" /></ItemTemplate>

                <HeaderStyle HorizontalAlign="Left" />

                <ItemStyle Width="10%" />

            </asp:TemplateField>

        </Columns>

    </asp:GridView>

I am going to use a JavaScript method in which I will be ing a client id of CheckBox and TextBox. Inside the method, I will write logic to disable the TextBox if the CheckBox is checked.

<script type="text/javascript" language="javascript">
    function checkitem(checkBox, textBox1) {

        var e = document.getElementById(textBox1.id);
        var f = document.getElementById(checkBox.id);
        if (f.checked == true) {
            e.disabled = true;
        }
        else {
            e.disabled = false;
 
        }
 
    }
    </script
>

In the RowDataBound event of the GridView, I am going to get the corresponding row's CheckBox and TextBox id and using that am going to call the JavaScript above on click of CheckBox by adding an attribute to the CheckBox as below.

        protected void GridView_Classes_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                string chkClientId = e.Row.FindControl("CheckBox_Select").ClientID;
                string txt2ClientId = e.Row.FindControl("TextBox_NonEditable").ClientID;
                ((CheckBox)e.Row.FindControl("CheckBox_Select")).Attributes.Add("onclick", "javascript:checkitem(" + chkClientId + "," + txt2ClientId + ");");
            }


So whenever the user clicks on the CheckBox, the JavaScript will be called and if the CheckBox is selected, then TextBox will be disabled and vice versa.

For the complete source code, please find the attached solution.
 

Up Next
    Ebook Download
    View all
    Learn
    View all