【问题标题】:How to set readonly property in BoundFiled Cell in true?如何在 BoundFiled Cell 中设置只读属性为 true?
【发布时间】:2015-07-15 14:22:54
【问题描述】:

我是这样做的:

 if (e.Row.Cells[3].Text != null && e.Row.Cells[3].Text != " " 
          && e.Row.Cells[3].Text != "")
 {
     e.Row.Cells[3].Attributes.Add("readonly", "true");
 }

但它不起作用。当单元格为空或包含“ ”时,我需要将 readonly 属性设置为 true。

【问题讨论】:

    标签: asp.net gridview boundfield


    【解决方案1】:

    没有direct way 可以将Gridview column 设置为readonly
    但是您可以在 Gridivew 的RowDataBound 事件中设置该列中的controls to readonly。例如

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == DataControlRowState.Alternate)
        {
            TextBox txt = (TextBox)e.Row.FindControl("ControlID");
            txt.ReadOnly = true;
        }
    }
    

    【讨论】:

      【解决方案2】:

      对于绑定字段,您可以假设 Controls(0) 是您想要的控件。

      因此这是可行的:

          Protected Sub DropDownListNoVariation_SelectedIndexChanged(sender As Object, e As EventArgs)
          Dim sourceGridViewRow As GridViewRow
          Dim sourceDropDown As DropDownList
          Dim TargetTextBox As TextBox
      
          sourceGridViewRow = sender.Parent.Parent
          sourceDropDown = sender
          If sourceDropDown.Text = "True" Then
              TargetTextBox = sourceGridViewRow.Cells(4).Controls(0)
              TargetTextBox.ReadOnly = True
              TargetTextBox = sourceGridViewRow.Cells(5).Controls(0)
              TargetTextBox.ReadOnly = True
          Else
              TargetTextBox = sourceGridViewRow.Cells(4).Controls(0)
              TargetTextBox.ReadOnly = False
              TargetTextBox = sourceGridViewRow.Cells(5).Controls(0)
              TargetTextBox.ReadOnly = False
          End If
      
      End Sub
      

      【讨论】:

        【解决方案3】:

        这行得通吗?

        if (e.Row.Cells[3].Text != null && e.Row.Cells[3].Text != " " && e.Row.Cells[3].Text != "")
         {
             e.Row.Cells[3].ReadOnly = true;
         }
        

        似乎BoundField 有一个ReadOnly property。但我现在不确定e.Row.Cells[3] 是否属于BoundField 类型。也许您可能需要将其转换为 BoundField

        【讨论】:

        • 是的。但我怎么能改变它。 e.Row.Cell[3].ReadOnly 不正确。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-06
        • 2013-02-26
        • 1970-01-01
        相关资源
        最近更新 更多