Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am using <asp:TextBox> which is having values of eg:100. When i click <asp:Button> on ClientClick i am clearing the <asp:TextBox> value.
But same <asp:Button> OnClick event <asp:TextBox> contains the same value. Its not cleared.

How to clear the <asp:TextBox> onClientClick(javascript> event. It should empty in OnClick event.

Please help me.

Regards,
Thillai.C
Posted

XML
<form id="form1" runat="server" >
   <asp:TextBox ID="txt" runat="server"></asp:TextBox>
   <asp:Button ID="btn" runat="server" Text="click" OnClientClick="return Clear();" />
   </form>
   <script type="text/javascript">
       function Clear() {
           document.getElementById("<%=txt.ClientID %>").value = "";
           return true;
       }
   </script>
 
Share this answer
 
You need to return "true" after you clear the TextBox value.

Just add this line at the end of your JavaScript function which you call onClientClick of the button.
JavaScript
return true;


Also, it's important to call the JavaScript function like this:

XML
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Width="70px" OnClick="btnSubmit_Click" OnClientClick="return validateData();" />


Above call is in the form return JavaScriptFunction();.

JavaScript function:
JavaScript
function validateData()
{
    //clear textbox
    return true;
}


Hope this helps!
 
Share this answer
 
Comments
Hiren solanki 13-Nov-10 1:26am    
I haven't tested it yet, but if it's working then it's very good solution. and will update my opinion accordingly.
XML
<head runat="server">
    <title>Home</title>
    <script type="text/javascript" language="javascript">
    function test(){
    //alert("test");
    var htText=document.getElementById("TextBox1");
    var seText=document.getElementById("TextBox2");
    htText.value="";
    seText.value="";
    }
    </script>
</head>



XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" OnClientClick="test()" runat="server" Text="Button" />
 
Share this answer
 
v3
For you kind information,

Javascript only clears Client side controls, I mean if you are clearing textbox from client side though it's viewstate value is still stored at for server side use, So finally you will not get cleared text box at server side.
:)
 
Share this answer
 
v2
Comments
Ankur\m/ 13-Nov-10 1:24am    
It's possible Hiren. See my answer.

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