VBdotnet2005
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
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
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>&nb sp;
<asp:Button id="Button2" runat="server" onclick="Button2_Click" Text="Validator Enabled"></asp:Button></P>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextB ox>
<asp:RequiredFieldValidato r id="RequiredFieldValidator 1" runat="server" ErrorMessage="RequiredFiel dValidator "
ControlToValidate="TextBox 1" EnableClientScript="False" ></asp:Req uiredField Validator> </P>
</form>
</body>
</HTML>
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
Me.TextBox1.Text = ""
Me.RequiredFieldValidator1
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
Me.TextBox1.Text = ""
Me.RequiredFieldValidator1
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>&nb
<asp:Button id="Button2" runat="server" onclick="Button2_Click" Text="Validator Enabled"></asp:Button></P>
<P>
<asp:TextBox id="TextBox1" runat="server"></asp:TextB
<asp:RequiredFieldValidato
ControlToValidate="TextBox
</form>
</body>
</HTML>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER