Link to home
Start Free TrialLog in
Avatar of VBdotnet2005
VBdotnet2005Flag for United States of America

asked on

clear button - vb2005

 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
Avatar of VBdotnet2005
VBdotnet2005
Flag of United States of America image

ASKER

Set the property "causesvalidation" of that button to false? What else can you do it beside this?
Avatar of clockwatcher
clockwatcher

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>

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