Matthew Walk
Matthew Walk

Reputation: 1054

Is it possible to toggle ReadOnly on a CheckBox column in an ASP.NET GridView?

I have an asp:CheckBoxField column in a asp:GridView. I would like to make that column ReadOnly if it is already checked. That field gets populated from a bool value in the database.

So far, I've only been able to set the ReadOnly property of the column during design time. When I have attempted to set it dynamically in code, it doesn't seem to do anything.

enter image description here

Is there anyway to reference the "ReadOnly" property of the Checkboxes inside of the GridView?

enter image description here

Upvotes: 0

Views: 1266

Answers (1)

Sruthi Suresh
Sruthi Suresh

Reputation: 697

you can achive this through gridviews row data bound event

<asp:gridview id="Gridview1" runat="server" onrowdatabound="Gridview1_RowDataBound" ..........>

and in code behind

 protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chk = (CheckBox)e.Row.FindControl("chkBox");
            if (chk.Checked==true)
            {
                chk.Enabled = false;
            }
            else
            {
                chk.Enabled = true;
            }
        }
    }

Upvotes: 1

Related Questions