无法在aspx.cs文件中访问asp.net控件

时间:2011-09-19 15:16:08

标签: c# asp.net html

在我的aspx文件中,我有:

  <asp:GridView Width="100%" DataSourceID="LinqDataSource1" ID="GridView1" OnRowUpdating="GridView1_RowUpdating"
                runat="server" AutoGenerateColumns="False"
                AllowPaging="True" CssClass="gridclass" CellPadding="3" 
                AllowSorting="True" DataKeyNames="ID" >
                <FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
                <PagerStyle ForeColor="#000066" HorizontalAlign="Left" BackColor="White"></PagerStyle>
                <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="Gray"></HeaderStyle>
                <Columns>
                    <asp:CommandField ShowEditButton="True"></asp:CommandField>
                    <asp:CommandField ShowDeleteButton="true" />
                    <asp:BoundField HeaderText="ProductName" InsertVisible="False" DataField="Title">
                        <ItemStyle HorizontalAlign="Left"></ItemStyle>
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="ProductImage">
                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                        <EditItemTemplate>
                            <asp:FileUpload ID="imageupload" runat="server"  />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="#">
                                <asp:Image ID="product_image" runat="server" ImageUrl='<%# DataBinder.Eval(Container, "DataItem.Path")%>' Height="20px"
                                    Width="20px" />
                            </asp:HyperLink>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <SelectedRowStyle ForeColor="White" Font-Bold="True" BackColor="#669999"></SelectedRowStyle>
                <RowStyle ForeColor="#000066"></RowStyle>
            </asp:GridView>

我想在我的aspx.cs文件中访问<asp:FileUpload ID="imageupload" runat="server" />的结果我无法访问imageupload控件来获取结果。

2 个答案:

答案 0 :(得分:3)

您需要执行.FindControl("imageupload")来获取嵌套在GridView中的控件。

答案 1 :(得分:1)

您可以访问gridviewrowdatabound

中的控件
protected void GridView1RowDataBound(object sender, GridViewRowEventArgs e)
{
     var file = (FileUpload) e.Row.FindControl("imageupload");
}