Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 792
  • Last Modified:

Can I use AJAX to clear a TextBox before its contents are sent to the server

I have a website developed using VC#.NET and ASP.NET 2.0

The site contains  a page that contains 2 text boxes and a button.

In the 1st text box the user types or pastes the input data. This can be a lot of text data typically over 20K
When the user press the button the text in the 1st text box is sent to the server where it is processed. The answer is returned to the client and displayed in the 2nd text box. Again the amount of data sent to , and displayed in, the 2nd text box can be a large amount maybe over 20K.

Im trying to minimize the amount of data sent to the server. For example if the user presses the button AGAIN then the data in the 2nd text box (the results data as a result of the previous button press) is sent to the server even though thats not required. Its only the data in the 1st text box (the input data) that should be sent. But thats how ASP.NET works. Setting ENABLEVIEWSTATE to false for the textbox doesnt work because text boxes are handled differently. The contents of the textbox are sent anyway.

So what I want is this. If there is data in the 2nd text box then, when the user presses the button, I want it to be somehow cleared so that NO DATA  is sent to the server from this text box. Can this be done using AJAX. If so how and which control would I use?

PLEASE NOTE: Ive already tried the obvious thing which is
 TextBox2.Text = "" in the button_click event. This of course doesnt work!

2 Solutions
You don't necessarily need to use AJAX, just need to use Javascript. Set the button's OnClientClick attribute = to a javascript function called something like "ClearTextbox2" and then in that javascript function, clear the text box.


function ClearTextBox()
            var txt = document.getElementById('TextBox1');
            txt.value = '';
            return true;

Your button would look something like this:
<asp:Button ID="Button1" runat="server" OnClientClick="javascript: return ClearTextBox();"
            Text="Button" />
as TSmooth said, you dont really need ajax to clear the textbox but if you really want to use then....

drag and dropt he updatepanel from ajax tool and inside the updatepanel add controls like textbox or any other then wire your event and in that event you write something like this:

textbox.text = string.empty(); or textbox.text = "";


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now