Question

Server side I want to use the HeaderText from a Templatefield from a DetailsView in a String which is to be returned as a text value for a label which is a grandchild of the Templatefield. How do I reference the HeaderText "Care Plan Intent" explicitly in the code behind by name rather than using an index, and then use it in a string to replace the text value for the label which is an Eval of a sql datasource if the value does not meet certain crieria. For instance in the example below, if the value of Eval("CarePlanIntent") is ok then all well and good, otherwise I want to replace it with something like "Either Care Plan Intent has not been entered or it does not meet the set criteria".

<asp:DetailsView ID="dvTest">
<Fields>
<asp:TemplateField HeaderText="Care Plan Intent:">
    <ItemTemplate>
        <asp:Label skinid="tablabelblack" id="lblIntent" runat="server" Text='<%# Eval("CarePlanIntent") %>' />
    </ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

No correct solution

OTHER TIPS

You can use LINQ to do this:

var field = dvText.Fields.First(i => i.HeaderText == "Care Plan Intent:");
var lbl = (Label)field.FindControl("lblIntent");

if (lbl.Text == "Y")
  lbl.Text = "New Text";

Something like that.... I may have some of the properties or methods wrong; but it should be something like that.

Based on your sample, you can get the label inside the details view instead of finding the proper template field by header text.

Dim lblIntent As Label = CType(dvTest.FindControl("lblIntent"), Label)
If lblIntent.Text <> "Your Condition" Then
    lblIntent.Text = "Some New Text"
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top