tags:

views:

25

answers:

3

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 choosen 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>
+1  A: 

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

riffnl
if the value of the dropdownlistbox is selected users,then i am showing 2 button controls and 2 listbox controls which are not shown at the pageload time
Abhimanyu
Might want to do that client-side; already build the buttons and listbox controls (with runat="server") and display them using javascript
riffnl
A: 

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>");
    }
}
Tom Vervoort
A: 

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.

Anchit