Link to home
Start Free TrialLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

asked on

clear textbox in clientside

in ASP.NET 2.0 I have a page with a  formview and in it a textbox and a button.
I want that push on the button will clear the textbox in clientside in javascript.

If I didn't have the formview I could do something like:
<asp:TextBox ID="TBCV" runat="server"  Text='<%# Bind("CV") %>'></asp:TextBox>

 <asp:Button ID="Button1" runat="server"  Text="Button" OnClientClick="this.form.TBCV.value=''" />
 but as the texbox TBCV is a part of a formview control  so "this.form.TBCV.value"  is wrong.
How do I have to write it?

If I do it in server side I use the findcontrol but I don't want to use it because I want it to be quicker.

How do I do it?
ThankYou
Anat
Avatar of Sammy
Sammy
Flag of Canada image

Thats easy
add this script to the head of your aspx page
<script type="text/javascript">
function ClearIT(txtID)
 <!--
      {
      document.getElementById(txtID).value='';
      }
  // -->
</script>
and in your code behind page load add this
 Me.Button2.Attributes.Add("onClick", "javascript:ClearIT(TBCV')")

HTH
Avatar of ANAT2403

ASKER

Hi,
You did not react to the fact that I am having a formview. In formview you can not turn to the control directly
but through the formview.
To what you wrote in the code behind I know how to change and it should be:
        Button tempTB = (Button)FormView1.FindControl("TBCV'");
and then
tempTB.Attributes.Add("onclick","javascript:ClearIT('CV_containerTB')");
but to the javascript function - this is my problem how to write.
By the way if I do what you said I get the error:
A Runtime Error
Error: 'document.getElementById(....) is null or not an object.


Sorry but my comment was sent before I finished writing:
Ignore it.
Again

Hi,
You did not react to the fact that I am having a formview. In formview you can not turn to the control directly
but through the formview.
To what you wrote in the code behind I know how to change and it should be:
        Button tempTB = (Button)FormView1.FindControl("Button2");
and then
       tempTB.Attributes.Add("onclick","javascript:ClearIT('TBCV');

but to the javascript function - this is my problem how to write.
By the way if I do what you said I get the error:
A Runtime Error
Error: 'document.getElementById(....) is null or not an object.

ThankYou
Anat
Try this
Me.Button2.Attributes.Add("onClick", "javascript:ClearIT('" & Me.Txt1.ClientID & "')")

ignore the last post, I dont think it will work
ASKER CERTIFIED SOLUTION
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just Perfect !!!!
ThankYou
I add here the code in C# if anybody needs it.

        TextBox TBCV = (TextBox)FVSelfEntry.FindControl("CV_containerTB");
        Button btnClear = (Button)FVSelfEntry.FindControl("BtnClearCV");
        btnClear.Attributes.Add("onclick", "javascript:ClearIT('" + TBCV.ClientID + "')");
I am glad its working for you.
Dont forget to answer this question please so others can use it

Good Luck
terribly sorry. I accepted  the answer before but somehow it didn't catch.
Anat.
Thats Ok Anat :-)
Hi sammy
It is working for me the clear textbox but again it refreshes the screen and I don't know why?
Can you help?
Anat