0

I am trying to clear data of multiple ASP Textboxes on clicking Clear button. I was trying with one textbox initially. It is clearing the the textbox data on button-click, but it is also calling the Page_Load method. I want to clear textboxes without calling Page_Load method.

Here is the code, I tried:

<body>
    <form id="form1" runat="server">
                <script type="text/javascript">
                    function Clear() {
                        document.getElementById("<%=txt.ClientID %>").value = "";
                return true;
            }
   </script>
    <div>

        <asp:TextBox ID="txt" runat="server"></asp:TextBox>
        <asp:Button ID="btn" runat="server" Text="click" OnClientClick="return Clear();" />    
    </div>
    </form>
</body>
1
  • Return false in your function or add return false to the end of your OnClientClick declaration. Commented Jan 13, 2015 at 16:33

1 Answer 1

1

You can avoid page load by changing the return of your function:

function Clear() {
    document.getElementById("<%=txt.ClientID %>").value = "";
    return false;
}

And you can use classes to clear multiple textboxes, like this:

<asp:TextBox ID="txt1" runat="server" CssClass="txts"></asp:TextBox>
<asp:TextBox ID="txt2" runat="server" CssClass="txts"></asp:TextBox>
<asp:TextBox ID="txt3" runat="server" CssClass="txts"></asp:TextBox>
<asp:TextBox ID="txt4" runat="server" CssClass="txts"></asp:TextBox>

And the JS:

function Clear() {
    var txts = document.getElementsByClassName('txts');

    for (var i = 0; i < txts.length; i++)
        txts[i].value = '';

    return false;
}

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.