Referencing the HeaderText of a TemplateField by text value rather than by index

StackOverflow https://stackoverflow.com/questions/21895812

  •  13-10-2022
  •  | 
  •  

سؤال

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>

لا يوجد حل صحيح

نصائح أخرى

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top