Make CheckBox ReadOnly in ASP.NET

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Web;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using System.Configuration;  
  9.   
  10. public partial class AttributesProperty: System.Web.UI.Page {  
  11.     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constring"].ConnectionString);  
  12.     protected void Page_Load(object sender, EventArgs e) {  
  13.         if (!IsPostBack == true) {  
  14.             BindData();  
  15.         }  
  16.     }  
  17.     protected void BindData() {  
  18.         try {  
  19.             string strQuery = "select ID, Attributes, Properties from tblAttributes";  
  20.             SqlCommand cmd = new SqlCommand(strQuery, con);  
  21.             cmd.CommandType = CommandType.Text;  
  22.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  23.             DataTable dt = new DataTable();  
  24.             da.Fill(dt);  
  25.             AttributeGrid.DataSource = dt;  
  26.             AttributeGrid.DataBind();  
  27.         } catch (Exception ex) {  
  28.             //throw ex;    
  29.         } finally {  
  30.             con.Close();  
  31.         }  
  32.   
  33.     }  
  34.     protected void checkProperties_CheckedChanged(object sender, EventArgs e) {  
  35.         CheckBox check = (CheckBox) AttributeGrid.FindControl("checkAttributes");  
  36.         GridViewRow GrdRW = (GridViewRow) check.Parent.Parent;  
  37.         Response.Write(AttributeGrid.DataKeys[GrdRW.RowIndex].Value.ToString());  
  38.         if (check.Checked == true) {}  
  39.     }  
  40.     protected void AttributeGrid_RowDataBound(object sender, GridViewRowEventArgs e) {  
  41.         //    
  42.   
  43.         {  
  44.   
  45.             if (e.Row.RowType == DataControlRowType.DataRow) {  
  46.                 //Label a = (Label)DataBinder.Eval(e.Row.DataItem, "lblDept1");    
  47.                 var a = (Label) e.Row.FindControl("lblDept1");  
  48.                 string b = a.Text;  
  49.                 if (b == "0") {  
  50.                     ((CheckBox) e.Row.FindControl("checkProperties")).Checked = true;  
  51.                 } else if (b == "1") {  
  52.                     ((CheckBox) e.Row.FindControl("checkProperties")).Checked = false;  
  53.                 } else Response.Write("inavlid entry");  
  54.   
  55.             }  
  56.   
  57.         }  
  58.     }  
  59. }