如何在BoundFiled Cell中设置readonly属性为true?

时间:2013-02-13 05:12:28

标签: asp.net gridview boundfield

我这样做:

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

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

3 个答案:

答案 0 :(得分:0)

没有direct wayGridview column设置为readonly 但是,您可以在Gridivew的controls to readonly事件中设置该列中的RowDataBound。 e.g。

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

答案 1 :(得分:0)

对于绑定字段,您可以假设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

答案 2 :(得分:-1)

这会有用吗?

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

相关问题