Question

<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.

Was it helpful?

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();

OTHER TIPS

I've tried to do this in the past, but never found a better way than writing a function that loops all columns and find the one i want to modify by it's SortExpression.

This is the function i used to hide/show columns by their SortExpression:

public void ShowHideGridColumnBySortExpression(string sortExpression, bool show)
{
    for (int i = 0; i < gvProducts.Columns.Count; i++)
    {
        if (gvProducts.Columns[i].SortExpression != null && gvProducts.Columns[i].SortExpression == sortExpression)
        {
            gvProducts.Columns[i].Visible = show;
            break;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top