【问题标题】:how to disable all rows in gridview in asp.net如何在asp.net中禁用gridview中的所有行
【发布时间】:2015-05-27 18:16:27
【问题描述】:

我正在尝试根据我为网格视图设置的某些条件禁用网格视图中的所有行,如文本框和下拉控件。我现在有了它,它可以改变颜色,但我也想锁定它或禁用这些控件我该怎么做?

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblEndDate = (Label)e.Row.FindControl("lblStudyEndDate");
            DateTime EndDate = DateTime.Parse(lblEndDate.Text);
            if (EndDate < DateTime.Today)
            {
               //make all rows to read only here.. 

            }
        }
    }

【问题讨论】:

  • 禁用是什么意思?你想禁用复制内容吗?
  • 不,我想锁定文本框,这样没有人可以输入任何内容,所以基本上让它只读。
  • 那么那将是 Enabled = false 看起来不像你在任何地方都在这样做你想要什么文本框是只读的,你需要更具体
  • 你有没有试过:dataGridView1.ReadOnly = true; dataGridView1.Enabled = false;
  • 我刚刚更新了我发布的代码

标签: c# asp.net gridview


【解决方案1】:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblEndDate = (Label)e.Row.FindControl("lblStudyEndDate");
            TextBox tbSomeTB = e.Row.FindControl("tbSomeTB") as TextBox;

            DateTime EndDate = DateTime.Parse(lblEndDate.Text);
            if (EndDate < DateTime.Today)
            {
                e.Row.BackColor = System.Drawing.Color.DarkGray;
                tbSomeTB.Enabled = false;
            }
        }
    }

直截了当,cmet里的人已经说过了,这里的代码你显然已经知道怎么写了。

【讨论】:

    【解决方案2】:
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
         {
            Label lblEndDate = (Label)e.Row.FindControl("lblStudyEndDate");
            TextBox txt_1 = (TextBox)e.Row.FindControl("txt_1");
            DropDownList ddl_1 = (DropDownList)e.Row.FindControl("DropDownList1");
    
            DateTime EndDate = DateTime.Parse(lblEndDate.Text);
            if (EndDate < DateTime.Today)
            {
                txt_1.Enabled = false;
                ddl_1.Enabled = false;
                e.Row.CssClass = "setColorClass"; // css class to set bgcolor , forecolor etc 
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-14
      • 1970-01-01
      • 2021-08-25
      • 2014-09-14
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多