【问题标题】:How to apply "Readonly" in gridview textbox using BoundField when edit mode如何在编辑模式下使用 BoundField 在gridview文本框中应用“只读”
【发布时间】:2016-11-28 16:33:20
【问题描述】:

我希望使用 Boundsfield 在文本框上 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";

请指教。内联编辑时,我希望以编程方式在边界字段上 readonly 谢谢

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    你可以使用这个sn-p。

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

    【讨论】:

      【解决方案2】:
      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;
                          }
                      }
                  }
              }
          }
      

      【讨论】:

      • 我只想在行编辑时实现。
      • 抱歉回复晚了... .FindControl(“TextBox1”) = 如何循环查找?
      • @user3538475 - 我已经编辑了上面的代码。请。回顾一下。
      • 谢谢..但是... tx_chdets 返回 NULL 值。
      • TextBox tx_chdets = (TextBox)GridView1.Rows[e.NewEditIndex].FindControl(“TextBox1”);在此代码中,将 Textbox1 替换为您的文本框 ID
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 2018-08-13
      • 2011-08-31
      • 1970-01-01
      相关资源
      最近更新 更多