简体   繁体   中英

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?

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(); }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM