我这样做:
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");
}
但它不起作用。当单元格为空或包含“”时,我需要将只读属性设置为 true。
我这样做:
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");
}
但它不起作用。当单元格为空或包含“”时,我需要将只读属性设置为 true。
没有direct way
设置Gridview column
为readonly
。
但是您可以在您的 Gridivew 事件中设置controls to readonly
该列中的内容。RowDataBound
例如
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;
}
}
对于绑定字段,您可以假设 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
这行得通吗?
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
属性。但我现在不确定是否e.Row.Cells[3]
是BoundField
类型。也许你可能不得不把它投射到BoundField