简体   繁体   中英

Can't access asp.net control in aspx.cs file

In my aspx file I have :

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

and I want to access the result of <asp:FileUpload ID="imageupload" runat="server" /> in my aspx.cs file I can't access imageupload control to get the result .

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

You can access the control in gridviewrowdatabound

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM