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

Posted on 2007-10-18
Last Modified: 2013-11-26
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!

Question by:rangers99
    LVL 16

    Accepted Solution

    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" />
    LVL 7

    Assisted Solution

    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 = "";


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now