[RESOLVED]FileUpload Inside a Repeater Can't Find File

My code is like this :

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource4" OnItemCommand="Repeater1_ItemCommand">
        <HeaderTemplate>
            <table ID="Table1">
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:BulletedList ID="BulletedList1" runat="server" DataSourceID="SqlDataSource6" DataTextField="WPName" DataValueField="WPName"></asp:BulletedList>
                    <asp:Button ID="Button2" runat="server" Text="Add KKP" CommandName="AddKKP"/>
                    <asp:Panel ID="Panel1" runat="server" Visible="false">
                        <asp:FileUpload ID="FileUpload1" runat="server" />
                        <asp:Button ID="Button3" runat="server" Text="Upload" CommandName="UploadKKP" />
                        <asp:Label ID="StatusLabel" runat="server" Text="Label"></asp:Label>
                    </asp:Panel>
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>

and CodeBehind :

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddKKP")
            {                      
                Panel myPanel = (Panel)e.Item.FindControl("Panel1");
                myPanel.Visible = true;                
            }

            if (e.CommandName == "UploadKKP")
            {
                Label myStatusLabel = (Label)e.Item.FindControl("StatusLabel");
                FileUpload myFileUpload = (FileUpload)e.Item.FindControl("FileUpload1");
                if (myFileUpload.HasFile)
                {
                    try
                    {
                        string filename = Path.GetFileName(myFileUpload.FileName);
                        myFileUpload.SaveAs(Server.MapPath("~/") + filename);
                        myStatusLabel.Text = "Upload Success";
                    }
                    catch (Exception ex)
                    {
                        
                        myStatusLabel.Text = "Upload Fail" + ex.Message;
                    }
                }
                else
                {
                    
                    myStatusLabel.Text = "myFileUpload Has No File";
                }
            }      
        }        

The problem is, it always throw myStatusLabel.Text = "myFileUpload Has No File";

how can i fix it ?

Thanks for your help.

Best Regards,

Use ajax file upload instead

http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/AjaxFileUpload/AjaxFileUpload.aspx

Fileupload control not work in update panel

I’m trying to use AJAXControlToolkit, but it get an error :

‘MsAjaxBundle’ is not a valid script name.  The name must end in ‘.js’.

how can i fix it ?

thanks.

Refer below thread,

http://forums.asp.net/t/1864395.aspx/1

Hope this will helps..!!

Leave a Reply