在DetailsView中,我为HeaderText为“No Treatment Reason:”的TemplateField设置了visible = false。 在后面的代码中,我如何显式引用DataRow并根据其他TemplateField的值使用HeaderText“Care Plan Intent:”使其可见?
<asp:DetailsView ID="dvTest" FooterStyle-Width="100%" Font-Bold="false" BackColor="#A2C4EA"
FieldHeaderStyle-Width="35%" BorderWidth="1" CellSpacing="2" Font-Names="Arial, Helvetica, sans-serif"
Font-Size="12px" runat="server" AutoGenerateRows="False" AllowPaging="False" style="width:98%"
HeaderStyle-BackColor="White" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="center">
<Fields>
<asp:TemplateField HeaderText="Care Plan Intent:">
<ItemTemplate>
<asp:Label skinid="tablabelblack" id="lblIntent" runat="server" Text='<%# Eval("CarePlanIntent") %>' />
<asp:HiddenField runat="server" ID="hidCarePlanIntent" Value='<%# Eval("CarePlanIntent") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Treatment Reason:" Visible="false">
<ItemTemplate>
<asp:Label skinid="tablabelblack" id="lblNoTreatmentReason" runat="server" Text='<%# Eval("NoTreatmentReason") %>' />
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
答案 0 :(得分:0)
您必须使用DataBound
事件并将DataItem
转换为正确的类型(f.e. DataRowView
)。您可以使用FindControl
来获取控件并按照LINQ查询获取正确的列以使其显示(in):
protected void dvTest_DataBound(Object sender, EventArgs e)
{
var noTreatCol = this.dvTest.Fields.Cast<DataControlField>()
.First(field => field.HeaderText == "No Treatment Reason:");
DataRow row = ((DataRowView) dvTest.DataItem).Row;
Label tablabelblack = (Label)dvTest.FindControl("tablabelblack");
HiddenField hidCarePlanIntent = (HiddenField)dvTest.FindControl("hidCarePlanIntent");
string value = row.Field<string>("Value"); // just an example
noTreatCol.Visible = value == "SomeValueThatMakesItVisible";
}
是否有可能在VB中没有LINQ?
当然:
Protected Sub dvTest_DataBound(sender As Object, e As EventArgs)
Dim row As DataRow = DirectCast(dvTest.DataItem, DataRowView).Row
Dim tablabelblack As Label = DirectCast(dvTest.FindControl("tablabelblack"), Label)
Dim hidCarePlanIntent As HiddenField = DirectCast(dvTest.FindControl("hidCarePlanIntent"), HiddenField)
Dim value As String = row.Field(Of String)("Value") ' just an example '
Dim noTreatColIndex As Int32 = -1
For i As Int32 = 0 To Me.dvTest.Fields.Count - 1
Dim field = dvTest.Fields(i)
If field.HeaderText = "No Treatment Reason:" Then
noTreatColIndex = i
Exit For
End If
Next
If noTreatColIndex > -1 Then
dvTest.Fields(noTreatColIndex).Visible = value = "SomeValueThatMakesItVisible"
End If
End Sub