Solved

clear button - vb2005

Posted on 2006-07-11
3
247 Views
Last Modified: 2010-08-05
 I have RequiredFieldValidator on these four textboxes. The code  does not work right. Please help. When I click clear button, some of the required field show right next to the textboxes.  

Protected Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click

        Me.TextBox1.Text = Nothing
        Me.TextBox2.Text = Nothing
        Me.TextBox3.Text = Nothing
        Me.TextBox4.Text = Nothing

    End Sub
0
Comment
Question by:VBdotnet2005
3 Comments
 

Author Comment

by:VBdotnet2005
Comment Utility
Set the property "causesvalidation" of that button to false? What else can you do it beside this?
0
 
LVL 25

Expert Comment

by:clockwatcher
Comment Utility
I'm not sure why you don't want to use CausesValidation, but you can do it another way.

If you disable the client-side validation on your validator control then you could also disable/re-enable it manually on the individual validator controls at the server-side (if, for example, you wanted to disable some validators but leave others active -- for some reason):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

   Me.RequiredFieldValidator1.Enabled = False
   Me.TextBox1.Text = ""
   Me.RequiredFieldValidator1.Enabled = True

End Sub


Here's a sample without code-behind.  The one button turns off the validation before it makes the change.  The other doesn't.

sample.aspx
--------------
<%@ Page Language="vb"  %>
<script language="vb" runat="server">

    Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.RequiredFieldValidator1.Enabled = False
        Me.TextBox1.Text = ""
        Me.RequiredFieldValidator1.Enabled = True
    End Sub

    Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.TextBox1.Text = ""
    End Sub
   

</script>

<HTML>
     <body>
          <form id="Form1" method="post" runat="server">
               <P>
                    <asp:Button id="Button1" runat="server" onclick="Button1_Click" Text="Validator Disabled"></asp:Button>&nbsp;
                    <asp:Button id="Button2" runat="server" onclick="Button2_Click" Text="Validator Enabled"></asp:Button></P>
               <P>
                    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator"
                         ControlToValidate="TextBox1" EnableClientScript="False"></asp:RequiredFieldValidator></P>
          </form>
     </body>
</HTML>

0
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
Comment Utility
actually you cant reset the controls due to viewstate preserving the values.
the only way to clear the values is by using a client script

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.btnClear.Attributes.Add("onclick", "location.href= location.href; return false")
    End Sub

This will redirect the page to itself without any posting and it will work with any browser.

HTH
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now