0

I want READONLY on text box using Boundsfield. So far enabled is working but it not carry value in rowupdating

 protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        var nopoject = ddlproject.SelectedValue.ToString();
        Int32 curntMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
        int Mont1 = curntMonth - 1;
        var Comtext = "Rst" + Mont1.ToString();
        GridView1.EditIndex = e.NewEditIndex;
        BindData();

        foreach (GridViewRow row in GridView1.Rows)
        {              
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                String headertext = GridView1.Columns[i].HeaderText;
                String cellText = row.Cells[i].Text;

                if (Comtext == "Rst1")
                {
                     GridView1.Rows[e.NewEditIndex].Cells[i].Enabled = true;                         
                }}}

Below code not working :

GridView1.Rows[e.NewEditIndex].Cells[i].Attributes.Add("readonly", "readonly");
GridView1.Rows[e.NewEditIndex].Cells[i].CssClass = "read-only";

Please advise. I want readonly on boundsfield programically when inline edit Thank you

user3538475
  • 83
  • 1
  • 12

2 Answers2

2

You can use this snippet.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            TextBox textBox = e.Row.Cells[0].Controls[0] as TextBox;
            textBox.Enabled = false;
        }
    }
}
VDWWD
  • 35,079
  • 22
  • 62
  • 79
1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate)
      {
       //on you condition
        TextBox txt = (TextBox)e.Row.FindControl("ControlID");
        if(txt !=null)
        {
          txt.Attributes.Add("readonly", "readonly");           

        }
      }
    }

You can also make textbox readonly in row editing event as below.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        var nopoject = ddlproject.SelectedValue.ToString();
        Int32 curntMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
        int Mont1 = curntMonth - 1;
        var Comtext = "Rst" + Mont1.ToString();
        GridView1.EditIndex = e.NewEditIndex;
        BindData();

        foreach (GridViewRow row in GridView1.Rows)
        {              
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                String headertext = GridView1.Columns[i].HeaderText;
                String cellText = row.Cells[i].Text;

                if (Comtext == "Rst1")
                {
                     //GridView1.Rows[e.NewEditIndex].Cells[i].Enabled = true;                         
                     TextBox tx_chdets = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl(“TextBox1”);
                     if(tx_chdets!=null)
                    {
                        tx_chdets.Readonly=true;
                    }
                }
            }
        }
    }
jignesh
  • 1,639
  • 1
  • 16
  • 18