我这样做:
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。
答案 0 :(得分:0)
没有direct way
将Gridview 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