简体   繁体   中英

File Upload control inside GridView

I have two file upload controls inside GridViewControl for uploading files, I have a button for uploading files inside gridview, I am using its RowCommandEvent for uploading files. How can I get values of fileupload controls in RowCommandEvent? This is my GridView

   <asp:GridView ID="DgFiles" runat="server" AutoGenerateColumns="false" OnRowCommand="DgFiles_RowCommand">
                            <Columns>
                                <asp:TemplateField HeaderText="Crime" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>                                               
                                        <asp:Label ID="category" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Category") %>'                                ></asp:Label><br/>                                                
                                        <asp:FileUpload ID="file1" runat="server" /><br />
                                        <asp:FileUpload ID="file2" runat="server" />
                                        <asp:Button ID="btnupload" runat="server" CommandName="upload" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Date" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:Label ID="lbldate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Date") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="City" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:Label ID="lblcity" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "City") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>                                      
                            </Columns>
                        </asp:GridView>

and my RowCommandEvent

 protected void DgFiles_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "upload")
            {
            }
        }

I have to display records in GridView like structure, with file upload control inside it, If there is alternate way I can do it, kindly suggest.

First,
You have to add a command column with command name upload
second,
You have to find the control inside the row by using parent property.

FileUpload fl1 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File1")
FileUpload fl2 = (FileUpload)e.Row[rowindex].Cells<cellindex>.FindControl("File2")

Now You can use fl1 and fl2 as your filecontrols.

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