FileUpload clears on Insert

T

TD is PSP

I have a FileUpload control within a FormView (although I tried it outside
the FormView and got the same result) in the InsertTemplate. When the
<Insert> button is clicked and the Insert Command is executed the FileUpload
is cleared of it's value and hence during Insert there is no file to
"insert". I even tried this with a separate button that was both inside and
outside the form view and not connected to the Insert Command, and still the
FileUpload get's cleared.

How can I either stop the value from being cleared or what should I do
differently to accomplish the same result?
 
A

Alexey Smirnov

I have a FileUpload control within a FormView (although I tried it outside
the FormView and got the same result) in the InsertTemplate.  When the
<Insert> button is clicked and the Insert Command is executed the FileUpload
is cleared of it's value and hence during Insert there is no file to
"insert".  I even tried this with a separate button that was both insideand
outside the form view and not connected to the Insert Command, and still the
FileUpload get's cleared.

How can I either stop the value from being cleared or what should I do
differently to accomplish the same result?

Your button causes a postback and the FileUpload Control lost its
value. To solve this, you could use the UpdatePanel and wrap that
around the controls that are causing the postback.
 
T

TD is PSP

Your button causes a postback and the FileUpload Control lost its
value. To solve this, you could use the UpdatePanel and wrap that
around the controls that are causing the postback.

Actually the FormView is already in an UpdatePanel. Here's the code:

<%@ Page Language="C#" MasterPageFile="~/EMFT.Master" AutoEventWireup="true"
CodeBehind="PublicUpload.aspx.cs" Inherits="EMailFileTransfer.PublicUpload"
Title="EMail File Transfer - Public Upload" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<asp:ScriptManager ID="smUpload" runat="server"/>
<asp:UpdateProgress ID="uprUpload" runat="server"
AssociatedUpdatePanelID="upUpload">
<ProgressTemplate>
File transfer in Progress . . .
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="upUpload" runat="server">
<ContentTemplate>
<table>
</table>
<asp:FormView id="fvTFiles" runat="server"
DataSourceID="dsTFiles" DataKeyNames="biID" DefaultMode="Insert">
<InsertItemTemplate>
<table cellpadding="5">
<tr>
<td>From</td>
<td><asp:TextBox ID="tbFrom" runat="server"
Width="300px"></asp:TextBox></td>
</tr>
<tr>
<td>From EMail (also for confirmation email)</td>
<td><asp:TextBox ID="tbFromEMail" runat="server"
Width="300px"></asp:TextBox></td>
</tr>
<tr>
<td>Send To</td>
<td><asp:DropDownList id="dlSendTo"
runat="server" Width="300px" DataTextField="sName" DataSourceID="dsTUsers"
DataValueField="sUserName" /></td>
</tr>
<tr>
<td>File Title</td>
<td><asp:TextBox id="sFileTitleTextBox"
runat="server" Text='<%# Bind("sFileTitle") %>'></asp:TextBox></td>
</tr>
<tr>
<td>Select File<br />
<asp:RequiredFieldValidator ID="rfvUpload"
runat="server" ControlToValidate="fuUpload"

ErrorMessage="RequiredFieldValidator">You must select a file to
upload.</asp:RequiredFieldValidator></td>
<td><asp:FileUpload id="fuUpload" runat="server"
Width="661px"></asp:FileUpload></td>
</tr>
<tr>
<td><asp:Button id="bUpload" runat="server"
Text="Upload" CausesValidation="true" CommandName="Insert"
OnClick="bUpload_Click"></asp:Button></td>
<td>
</td>
</tr>
</table>
</InsertItemTemplate>
</asp:FormView>
<asp:SqlDataSource id="dsTFiles" runat="server"
ConnectionString="<%$ ConnectionStrings:EMailFileTransferConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="spTFilesSelectByUserName"
InsertCommandType="StoredProcedure"
InsertCommand="spTFilesInsertWithUserName" OnInserting="dsTFiles_Inserting"
OnInserted="dsTFiles_Inserted">
<SelectParameters>
<asp:parameter Type="String" Name="sUserName"
DefaultValue="" />
</SelectParameters>
<InsertParameters>
<asp:parameter Type="Int64" Direction="InputOutput"
Name="biID"></asp:parameter>
<asp:parameter Type="String" Name="sUserName"/>
<asp:parameter Type="String"
Name="sFileTitle"></asp:parameter>
<asp:parameter Type="String"
Name="sFileName"></asp:parameter>
<asp:parameter Type="DateTime"
Name="dExpirationDateTime"></asp:parameter>
<asp:parameter Type="String" Direction="InputOutput"
Name="sUserEMail"></asp:parameter>
<asp:parameter Type="String" Direction="InputOutput"
Name="sPassword"></asp:parameter>
</InsertParameters>
</asp:SqlDataSource> <asp:SqlDataSource id="dsTUsers"
runat="server" ConnectionString="<%$
ConnectionStrings:EMailFileTransferConnectionString %>"
SelectCommandType="StoredProcedure"
SelectCommand="spTUsersSelect"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
 
A

Alexey Smirnov

Actually the FormView is already in an UpdatePanel.  Here's the code:

<%@ Page Language="C#" MasterPageFile="~/EMFT.Master" AutoEventWireup="true"
CodeBehind="PublicUpload.aspx.cs" Inherits="EMailFileTransfer.PublicUpload"
Title="EMail File Transfer - Public Upload" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
    <asp:ScriptManager ID="smUpload" runat="server"/>
    <asp:UpdateProgress ID="uprUpload" runat="server"
AssociatedUpdatePanelID="upUpload">
        <ProgressTemplate>
            File transfer in Progress . . .
        </ProgressTemplate>
    </asp:UpdateProgress>
    <asp:UpdatePanel ID="upUpload" runat="server">
        <ContentTemplate>
            <table>
            </table>
            <asp:FormView id="fvTFiles" runat="server"
DataSourceID="dsTFiles" DataKeyNames="biID" DefaultMode="Insert">
                <InsertItemTemplate>
                    <table cellpadding="5">
                        <tr>
                            <td>From</td>
                            <td><asp:TextBox ID="tbFrom" runat="server"
Width="300px"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td>From EMail (also for confirmation email)</td>
                            <td><asp:TextBox ID="tbFromEMail" runat="server"
Width="300px"></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td>Send To</td>
                            <td><asp:DropDownList id="dlSendTo"
runat="server" Width="300px" DataTextField="sName" DataSourceID="dsTUsers"
DataValueField="sUserName" /></td>
                        </tr>
                        <tr>
                            <td>File Title</td>
                            <td><asp:TextBox id="sFileTitleTextBox"
runat="server" Text='<%# Bind("sFileTitle") %>'></asp:TextBox></td>
                        </tr>
                        <tr>
                            <td>Select File<br/>
                                <asp:RequiredFieldValidator ID="rfvUpload"
runat="server" ControlToValidate="fuUpload"

ErrorMessage="RequiredFieldValidator">You must select a file to
upload.</asp:RequiredFieldValidator></td>
                            <td><asp:FileUpload id="fuUpload" runat="server"
Width="661px"></asp:FileUpload></td>
                        </tr>
                        <tr>
                            <td><asp:Button id="bUpload" runat="server"
Text="Upload" CausesValidation="true" CommandName="Insert"
OnClick="bUpload_Click"></asp:Button></td>
                            <td>
                                </td>
                        </tr>
                    </table>
                </InsertItemTemplate>
            </asp:FormView>  
            <asp:SqlDataSource id="dsTFiles" runat="server"
ConnectionString="<%$ ConnectionStrings:EMailFileTransferConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="spTFilesSelectByUserName"
InsertCommandType="StoredProcedure"
InsertCommand="spTFilesInsertWithUserName" OnInserting="dsTFiles_Inserting"
OnInserted="dsTFiles_Inserted">
                <SelectParameters>
                    <asp:parameter Type="String" Name="sUserName"
DefaultValue="" />
                </SelectParameters>
                <InsertParameters>
                    <asp:parameter Type="Int64" Direction="InputOutput"
Name="biID"></asp:parameter>
                    <asp:parameter Type="String" Name="sUserName"/>
                    <asp:parameter Type="String"
Name="sFileTitle"></asp:parameter>
                    <asp:parameter Type="String"
Name="sFileName"></asp:parameter>
                    <asp:parameter Type="DateTime"
Name="dExpirationDateTime"></asp:parameter>
                    <asp:parameter Type="String" Direction="InputOutput"
Name="sUserEMail"></asp:parameter>
                    <asp:parameter Type="String" Direction="InputOutput"
Name="sPassword"></asp:parameter>
                </InsertParameters>
            </asp:SqlDataSource> <asp:SqlDataSource id="dsTUsers"
runat="server" ConnectionString="<%$
ConnectionStrings:EMailFileTransferConnectionString %>"
SelectCommandType="StoredProcedure"
SelectCommand="spTUsersSelect"></asp:SqlDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

The FileUpload Control must be out of the UpdatePanel, otherwise it
will be updated once button is clicked. Or, you have to perfom a file
upload when you click on the button.
 
M

marss

I have a FileUpload control within a FormView (although I tried it outside
the FormView and got the same result) in the InsertTemplate. When the
<Insert> button is clicked and the Insert Command is executed the FileUpload
is cleared of it's value and hence during Insert there is no file to
"insert". I even tried this with a separate button that was both inside and
outside the form view and not connected to the Insert Command, and still the
FileUpload get's cleared.

How can I either stop the value from being cleared or what should I do
differently to accomplish the same result?

Here are a few tips how to use FileUpload within UpdatePanel.
http://marss.co.ua/FileUploadAndUpdatePanel.aspx

Regards,
Mykola
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top