• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1000
  • Last Modified:

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
0
ANAT2403
Asked:
ANAT2403
  • 6
  • 5
1 Solution
 
Sammy AgeilCommented:
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
0
 
ANAT2403Author Commented:
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.


0
 
ANAT2403Author Commented:
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

0
 
Sammy AgeilCommented:
ignore the last post, I dont think it will work
0
 
Sammy AgeilCommented:
Now this works for me in a formview all code should be in Formview itemcreated in order for it to work

 Protected Sub FormView1_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.ItemCreated
        Dim TempTxt As TextBox = CType(FormView1.FindControl("TBCV"), TextBox)
        Me.Button1.Attributes.Add("onClick", "javascript:ClearIT('" & TempTxt.ClientID & "')")
    End Sub

It drove me crazy to get it to work but it was an experiece to learn from :-)

Good luck
0
 
ANAT2403Author Commented:
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 + "')");
0
 
Sammy AgeilCommented:
I am glad its working for you.
Dont forget to answer this question please so others can use it

Good Luck
0
 
ANAT2403Author Commented:
terribly sorry. I accepted  the answer before but somehow it didn't catch.
Anat.
0
 
Sammy AgeilCommented:
Thats Ok Anat :-)
0
 
ANAT2403Author Commented:
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now