Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Clearing all asp.net textboxes using Javascript
JavaScript
function ClearText() {
    var elements = document.getElementsByTagName("input");
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].type == "text") {
            elements[i].value = "";
        }
    }
}

its shows me empty textboxes
but when page Post backed to the server using submit button click
values are still same not get cleared

same for button when i tring to change button text .value="Update"
it get change but on code behind button text was Previous one
Posted
Comments
Rai Pawan 17-Jan-13 2:22am    
are your textboxes disabled, if so then even if you change their properties on client side, they will remain same when you access them on your code behind after a post back.
- pawan
Member-515487 17-Jan-13 3:08am    
dey aren't disable
Suvabrata Roy 17-Jan-13 3:57am    
please provide the textbox html

1 solution

XML
<div>
        <input type="text" value="txtName" id="txtName" runat="server" />
        <asp:Button Text="Click" ID="btnClick" runat="server"
            OnClientClick="javascript:return ClearText();" onclick="btnClick_Click" />
    </div>

C#
function ClearText() {
            var elements = document.getElementsByTagName("input");
            for (var i = 0; i < elements.length; i++) {
                if (elements[i].type == "text") {
                    elements[i].value = "";
                }
            }
            return true;
        }


use this code, and the values are cleared in the above scenario.
 
Share this answer
 
Comments
Member-515487 17-Jan-13 6:09am    
same issue i am facing i have done same Text getting cleared but when i am click on submit still values are showing in text box or coming from text which are clear
and i have taken all asp.net controls for text box and all buttons

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900