2016-11-25 99 views
0

我想要READONLY使用Boundsfield的文本框。到目前爲止,啓用了工作,但在rowupdating如何在編輯模式下使用BoundField在GridView文本框中應用「Readonly」

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;       
       }}} 

下面的代碼不工作也沒有攜帶價值:

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

請指教。我想上boundsfield只讀 programically當聯編輯 謝謝

回答

1

您可以使用此片段。

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; 
     } 
    } 
} 
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");   

     } 
     } 
    } 

您也可以讓文本框只讀行編輯事件如下。

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; 
        } 
       } 
      } 
     } 
    } 
+0

我只想在RowEditing時獲得成功。 – user3538475

+0

@ user3538475 - 請檢查更新的代碼 – jignesh

+0

對不起,對於遲到的答覆... .FindControl(「TextBox1」)=如何查找循環? – user3538475