3 Replies - 13129 Views - Last Post: 08 November 2012 - 02:42 AM

#1 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Issue with clearing textbox controls after submission

Posted 24 March 2012 - 10:22 AM

Hi there,

I have developed a contact form and I can successfully send a email when the user clicks the submit button but when the user clicks the back button in the browser the form fields are not being cleared. I am using AJAX Script Manager, UpdatePanel and ContentTemplate

I've spent 2 hours trying to fix it but I am not getting anywhere. I was hoping someone could take a look at my code please and assist me which would be greatly appreciated.

ASP.NET Code:
<asp:ScriptManager ID="MainScriptManager" runat="server" />
    <div id="contactform" class="cform">
    <asp:UpdatePanel ID="ContactPanel" runat="server">
    <ContentTemplate>
        <p>
            Please fill in the below form. Thanks.
        </p>
        <asp:Label ID="txtNameLabel" runat="server" Text="Name"></asp:Label>
        <br />
        <asp:TextBox ID="txtName" Width="200" Height="25" BackColor="#FBF5FF" ToolTip="Enter Name" MaxLength="50" runat="server" />
        <asp:RequiredFieldValidator ControlToValidate="txtName" ID="NameRequiredFieldValidator"
            ErrorMessage="(*)" runat="server" Height="25" ToolTip="Name is required" ForeColor="Red"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:Label ID="txtSubjectLabel" runat="server" Text="Subject"></asp:Label>
        <br />
        <asp:TextBox ID="txtSubject" Width="200" Height="25" MaxLength="50" ToolTip="Enter Subject" BackColor="#FBF5FF" runat="server" />
        <asp:RequiredFieldValidator ControlToValidate="txtSubject" ID="SubjectRequiredFieldValidator"
            ErrorMessage="(*)" runat="server" Height="25" ToolTip="Subject is required" ForeColor="Red"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:Label ID="txtTelephoneLabel" runat="server" Text="Telephone Number"></asp:Label>
        <br />
        <asp:TextBox ID="txtTelephone" Width="200" Height="25" MaxLength="25" ToolTip="Enter primary contact number" runat="server" BackColor="#FBF5FF" />
         <asp:RequiredFieldValidator ControlToValidate="txtTelephone" ID="TelephoneRequiredFieldValidator"
            ErrorMessage="(*)" runat="server" ToolTip="Telephone Number is required" Height="25" ForeColor="Red">
         </asp:RequiredFieldValidator>
          <asp:RegularExpressionValidator ID="TelephoneRegularExpressionValidator" runat="server"
            ControlToValidate="txtTelephone" ErrorMessage="(*)" Height="25" ForeColor="Red"
            SetFocusonerror="True" ToolTip="Invalid Telephone number" ValidationExpression="^\d+$">
        </asp:RegularExpressionValidator>
        <br/>
        <br />
        <asp:Label ID="txtEmailLabel" runat="server" Text="Email"></asp:Label>
        <br />
        <asp:TextBox ID="txtEmail" Width="200" Height="25" BackColor="#FBF5FF" ToolTip="Enter Email" runat="server" />
        <asp:RequiredFieldValidator ControlToValidate="txtEmail" ID="EmailRequiredFieldValidator"
            ErrorMessage="(*)" runat="server" ToolTip="Email is required" ForeColor="Red"></asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator ID="EmailRegularExpressionValidator" runat="server"
            ControlToValidate="txtEmail" ErrorMessage="(*)" Height="25" ForeColor="Red"
            SetFocusonerror="True" ToolTip="Invalid Email Address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
        </asp:RegularExpressionValidator>
        <br />
        <br />
        <asp:Label ID="txtMessageLabel" runat="server" Text="Message"></asp:Label>
        <br />
        <asp:TextBox ID="txtMessage" Width="200" Height="79" runat="server" 
            BackColor="#FBF5FF" TextMode="MultiLine" ToolTip="Enter message"
            MaxLength="256" />
        <asp:RequiredFieldValidator ControlToValidate="txtMessage" ID="MessageRequiredFieldValidator"
            ErrorMessage="(*)" ToolTip="Message is required" runat="server" ForeColor="Red" Height="79"></asp:RequiredFieldValidator>
        <br />
        <br />
          <asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
    </ContentTemplate>
    <Triggers>
         <asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
      </Triggers>
    </asp:UpdatePanel>
    </div>



Here is the C# code:

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SendEmail();
            ResetFormFields();

            Response.Redirect(Page.ResolveClientUrl("~/confirmation"));
        }

        private void ResetFormFields()
        {
            this.txtName.Text = string.Empty;
            this.txtEmail.Text = string.Empty;
            this.txtTelephone.Text = string.Empty;
            this.txtSubject.Text = string.Empty;
            this.txtMessage.Text = string.Empty;
            //this.ContactPanel.Update();
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Issue with clearing textbox controls after submission

#2 evaaggy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 12-June 12

Re: Issue with clearing textbox controls after submission

Posted 13 June 2012 - 02:57 AM

When a form is submitted, the browser automatically adds it to its history list. That is why when the back button is pressed, the page is displayed as it was saved. The work around here should be to prevent the browser from keeping the page in its history. You can do it this was:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;

}

Was This Post Helpful? 1
  • +
  • -

#3 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: Issue with clearing textbox controls after submission

Posted 07 July 2012 - 12:22 AM

Hi,

I am new to asp.net but I think this will be help ful to you.

When a form is submitted in ASP .NET, the form reappears in the browser window together with all form values. How come? This is because ASP .NET maintains your ViewState. The ViewState indicates the status of the page when submitted to the server. The status is defined through a hidden field placed on each page with a <form runat="server"> control.

Maintaining the ViewState is the default setting for ASP.NET Web Forms. If you want to NOT maintain the ViewState, include the directive <%@ Page EnableViewState="false" %> at the top of an .aspx page or add the attribute EnableViewState="false" to any control.

I found above content from [url="http://www.w3schools.com/aspnet/aspnet_viewstate.asp"]w3school[/url]

I think I could help u .
Was This Post Helpful? 0
  • +
  • -

#4 MarmiteX1  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 159
  • Joined: 17-October 09

Re: Issue with clearing textbox controls after submission

Posted 08 November 2012 - 02:42 AM

Thanks for that. That appears to have worked evagaggy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1