'Keep file name in FileUpload control after postBack

I have got problem with FileUpload control. I have this one, two drop down list, text box and button. If I select in first dropDownList "Yes" second one become disable and set value on NO (In second ddl I have two option YES or NO and in first one as well) however if I select NO in first dropDownList I posible to choose both option in second dropDownList. First ddl change second one on postBack using selectedIndexChanged evet and when it happends I loose file name in UploadFile control which I set before.

Code sample:

<asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />
<asp:DropDownListID="ddlSymmetry"runat="server" AutoPostBack="true"
      onselectedindexchanged="ddlSymmetry_SelectedIndexChanged">
                    <asp:ListItem Value="0">-- Select --</asp:ListItem>
                    <asp:ListItem Value="true">Yes</asp:ListItem>
                    <asp:ListItem Value="false">No</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
                    <asp:ListItem Text="-- Select --" Value="0"/>
                    <asp:ListItem Text="Yes" Value="1"  />
                    <asp:ListItem Text="No" Value="2"/>
                </asp:DropDownList>
<asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>

What should I do to keep file name in UploadFile control during changes selected options in drop down lists?



Solution 1:[1]

Try this, I added the label so you can see that the postback of the onselectedindexchange only affects the dropdown and not the file upload control, hope this helps.

        <asp:FileUpload ID="fuUploadGeometry" runat="server" Width="100%" />

      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
          <ContentTemplate>
        <asp:DropDownList ID="ddlSymmetry" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSymmetry_SelectedIndexChanged">
            <asp:ListItem Value="0">-- Select --</asp:ListItem>
            <asp:ListItem Value="true">Yes</asp:ListItem>
            <asp:ListItem Value="false">No</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddlModule" runat="server" Enabled="True">
            <asp:ListItem Text="-- Select --" Value="0" />
            <asp:ListItem Text="Yes" Value="1" />
            <asp:ListItem Text="No" Value="2" />
        </asp:DropDownList>
        <asp:TextBox ID="txtTopic" runat="server"></asp:TextBox>
              <asp:Label runat="server" ID="msgFromList" />
              </ContentTemplate>
          </asp:UpdatePanel>

protected void ddlSymmetry_SelectedIndexChanged(Object sender, EventArgs e) { msgFromList.Text = ddlSymmetry.SelectedItem.Value.ToString(); }

Solution 2:[2]

First thing keep in ur mind that is FileUpload Control Will became empty if any post back event occur on ur web page. So best solution is to put ur file upload control after all controll that can cause a post back like drop down list.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user2026349
Solution 2