为什么我无法从代码背后访问图像
本文关键字:背后 访问 图像 代码 为什么 | 更新日期: 2023-09-27 18:21:17
我有以下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
时,它找不到它。
我该如何解决此问题?
正如KidBilly所说,您无法直接访问它们。此外,请注意,它们的ID在运行时会被篡改,因为不允许在一个页面上有多个相同的ID(即GridView的每行一个按钮),在这种情况下,您需要先查看它们是如何被篡改的,然后才能知道要传递给FindControl()什么。
例如,我有一个id为"edit"的LinkButton,当它被渲染时,它变成了id为"MainContent_LoginView2_existingAccounts_edit_0"
由于项目模板是在运行时创建的,因此您无法访问它们。试试之类的东西
var imgExpCol= yourTasksGV.FindControl("imgExpCol") as Image;