开发者

Losing the value of a FileUpload textbox when a dropdown list selection changes

I am having an dropdownlistbox and fileupload control.After selecting a file from upload control,if i change the dropdownlist value(postback occurs) then the file path i chosen earlier is shown empty. here is my code:

<asp:Label ID="lblupload" runat="server" Text="Upload a file: "></asp:Label>                
   <asp:FileUpload ID="upload" runat="server" Width="320" Height="18" BorderColor="Gray" BorderWidth="1" />
   <asp:Label ID="Perimission" runat="server" Text="Perimission"></asp:Label>
   <asp:DropDownList ID="ddlState" runat="server" OnSelectedIndexChanged="ddlState_SelectedIndexChanged" AutoPostBack="true" >
        <asp:ListItem Text="Everybody"  Value="Everybody"></asp:ListItem>
        <asp:ListItem Text="Students"  Value="Students"></asp:ListItem>
        <asp:ListItem Text="Selected Users"  Value="Selected Users"><开发者_运维百科/asp:ListItem>
    </asp:DropDownList>


Don't know if you really need that autopostback on the dropdownlist. But clear it and you're ok. FileUpload values won't be saved during postback..


Here's a quick workaround you can use:

Add a ScriptManager to your page.

Put the dropdownlists in an AJAX UpdatePanel and also display the buttons and listboxes in the UpdatePanel.

This will make the dropdownlists cause a partial, asynchronous postback rather than a full page post-back, thus the FileUpload control will be able to retain it's value.

I know that it's not the most efficient way to do it, but it's an easy way out, and also would be aesthetically better for the user as the page does not appear to reload just because of selecting a value in a dropdownlist.

But, remember to keep your FileUpload control outside the UpdatePanel, otherwise the FileUpload will not work.


The FileUpload control posts the selected file on the next postback. Because changing the dropdown initiates a postback, the file is uploaded when changing another item in the DropDownList.

You can test this by putting this code in your page load:

protected void Page_Load(object sender, EventArgs e)
{
    if (upload.PostedFile != null)
    {
        Response.Write("<p>" + upload.PostedFile.FileName + "</p>");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜