문제

I am developing a website using ASP.Net and VB.

On one of the pages, there are certain fields (TextBoxes and a DropDownList). User is supposed to fill out those fields and click 'Submit' button. Also, there is a button 'Clear Values', which if clicked by user should clear the fields on the webpage.

The code I have written for clearing the fields was:

txtBox1.Text =  String.Empty

for all the TextBoxes. But, that didn't work, as I have used few validators like RequiredFieldValidator, RangeValidator, RegularExpressionValidator etc. to validate the fields. So, when the user fills out few fields and then clicks on 'Clear Values' button in order to clear all the fields, then the fields don't get cleared. Insted of that the validations work and the error messages appear as per those validations.

I also tried the following code to clear all the fields:

 Response.Redirect("UserData.aspx");

(where 'UserData.aspx' is name of that webpage only). But, still it gives the same validation error messages.

How to tackle this?

도움이 되었습니까?

해결책

The button for "Clear Values"

You have to set its property CausesValidation = "false"

다른 팁

After your user clicks the Submit or Clear button you can do something like this in a different method, and then call that method in your button's event handler:

public void ClearAll()
{
    textbox1.Text = "";
    textbox2.Text = "";
    textbox3.Text = "";

}

Make sure CausesValidation property is set to false for each of the buttons so that your validators aren't fired.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top