为什么我无法从代码隐藏中访问图像

时间:2014-09-10 13:15:21

标签: c# asp.net

我有以下GridView:

<asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="You currently have no tasks assigned to you" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Image ID="imgExpCol" AlternateText="plus" ClientIDMode="Static" ImageUrl="~/theImages/subTaskPlus.png" runat="server" CssClass="imgExpCol" />
                <asp:Panel ID="pnlSubTasks" runat="server" CssClass="pnlSubTasks" ClientIDMode="Static">
                    <asp:GridView ID="gvSubTasks" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
                        <Columns>
                            <asp:BoundField DataField="Task Name" HeaderText="Task Name" />
                            <asp:BoundField DataField="Due Date" HeaderText="Due Date" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:HyperLinkField Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ImageUrl="~/theImages/Dependencies.png" CssClass="gvTaskDep btnShowDepend" runat="server" ID="btnShowDepend" OnCommand="btnShowDepend_Command" CommandName="TaskDepend" AlternateText='<%#Eval("Object") + "," + Eval("FK") %>' CommandArgument='<%#Eval("Object") + "," + Eval("FK") %>' ToolTip="Click to view Dependencies" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Site" HeaderText="Site" SortExpression="Site" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Practice" HeaderText="Practice" SortExpression="Practice" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Provider" HeaderText="Provider" SortExpression="Provider" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Roles" HeaderText="Roles" SortExpression="Roles" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Object" HeaderText="Object" SortExpression="Object" ItemStyle-CssClass="hideTag" HeaderStyle-CssClass="hideTag" />
        <asp:BoundField DataField="FK" HeaderText="FK" SortExpression="Object" ItemStyle-CssClass="hideTag" HeaderStyle-CssClass="hideTag" />
      </Columns>
</asp:GridView>

当我尝试从代码隐藏中访问imgExpCol时,它无法找到它。

如何解决此问题?

2 个答案:

答案 0 :(得分:2)

由于项目模板是在运行时创建的,因此您无权访问它们。尝试像

这样的东西
var imgExpCol= yourTasksGV.FindControl("imgExpCol") as Image;

答案 1 :(得分:2)

正如KidBilly所说,你无法直接访问它们。此外,请注意,他们的ID将在运行时受到损坏,因为不允许在单个页面上具有多个相同的ID(即GridView的每一行都有一个按钮),在这种情况下,您需要查看在你知道要传递给FindControl()的内容之前,它们是如何被破坏的。

例如,我有一个ID为&#34的LinkBut​​ton;编辑&#34;当它被渲染时,它变成id =&#34; MainContent_LoginView2_existingAccounts_edit_0&#34;