0
votes

I have few Textbox, FileUpload and button controls within the same page. I had tried enabling the EnableViewState to True but it is still not working. The Textbox will lose its value when clicking the button to upload attachments. May I know what's wrong here, should I use ViewState to retain the values of the textboxes?

<div class="form-row">
    <div class="form-group col-md-6" >
            <asp:Label ID="lblConfiguration" runat="server" Text="Configuration*"></asp:Label>
        <asp:TextBox ID="txtConfiguration" runat="server" type="text" class="form-control" EnableViewState="true"  ViewStateMode="Enabled" ></asp:TextBox>
    </div>

    <div class="form-group col-md-12">
        <div class="col-md-6">
        <asp:FileUpload ID="fuUpload" runat="server" CssClass="nv-file-select uploader" multiple="true" /> 
            <br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" CssClass="btn btn-secondary" />  
    </div>
</div>
1
did you enable viewstate on page level also? - Homungus
I can't find such parameter in the Page and master page. It is still not working even I enable it at page level and master page. - JL_Coder
the parameter is shown in Aristos answer. does the textbox maintain its value, if you strip down your page to the minimum, e.g. only textbox and button? - Homungus
I haven't done that. I will resort to bind the values of the controls into ViewState as the solution. - JL_Coder

1 Answers

3
votes

Possible reasons

1) You have disable the Viewstate on full page - Check on top of aspx page if its enable or not

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FileName.ascx.cs" EnableViewState="true" %>

2) At some point on code behind you change the value on post back - so check to not change it by adding this check

if (!Page.IsPostBack)
{
    TextBox.Text = "";
}

3) For some reason after your post back you do redirect on same page.

4) For some reason you do not make post back, but reload