Search code examples
c#asp.net.nettemplatefielddetailview

How to Access TemplateField in Code Behind


<asp:DetailsView...
    <asp:TemplateField HeaderText="My CheckBox" SortExpression="DataID" >
         <InsertItemTemplate>
                <asp:CheckBox ID="T01cbx" runat="server" Visible ="false" Checked='<%# Bind("DataID") %>' AutoPostBack="True" " />
         </InsertItemTemplate>
    </asp:TemplateField>

I want give an ID or Name for the above TemplateField and access it from the code behind. Is there anyway to find the TemplateField by giving and ID or Name? This is inside a DetailView.


Solution

  • You could use LINQ to get a TemplateField by it's HeaderText:

    TemplateField cbField = DetailsView1.Fields.OfType<TemplateField>()
                           .Where(f => f.HeaderText == "My CheckBox")
                           .FirstOrDefault();