문제

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

도움이 되었습니까?

해결책

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

다른 팁

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;
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top