Selective Form Validation Using ASP.NET Validation Controls
-
Dear all i have a problem in a web form ,it contains two user controls ,each control has its own validation controls ,the problem is ,when I put the two user controls in the same page ,when I push a button in one of them the other validation controls in the other user controls fires even if the user control where I pushed the button is valid ,so please if any one knows an article opr a sample that discuss this issue please inform me Thanks
-
Dear all i have a problem in a web form ,it contains two user controls ,each control has its own validation controls ,the problem is ,when I put the two user controls in the same page ,when I push a button in one of them the other validation controls in the other user controls fires even if the user control where I pushed the button is valid ,so please if any one knows an article opr a sample that discuss this issue please inform me Thanks
Hi there Because you place the two controls in the same page, then at the client side they all are placed in a form element. So when you click a button, all validators on the form will be running. In this case, if you want to seperate validators, you are required to provide a bit more work. What you need to do is to keep the validators of each user control in an array, it's the same as ASP.NET emits the client-side scripts to hold all validators:
var Page_Validators = new Array(document.all["RequiredFieldValidator1"],document.all["RequiredFieldValidator2"]);
At the client side, depending on which button clicked by the user, you can enable/disable the validators of each user control by getting through the validator list and setting the enabled property of each invidual validator. To emit the client-side scripts in the page, you can take a quick look at the methods of the Page class. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuipagememberstopic.asp[^]
-
Hi there Because you place the two controls in the same page, then at the client side they all are placed in a form element. So when you click a button, all validators on the form will be running. In this case, if you want to seperate validators, you are required to provide a bit more work. What you need to do is to keep the validators of each user control in an array, it's the same as ASP.NET emits the client-side scripts to hold all validators:
var Page_Validators = new Array(document.all["RequiredFieldValidator1"],document.all["RequiredFieldValidator2"]);
At the client side, depending on which button clicked by the user, you can enable/disable the validators of each user control by getting through the validator list and setting the enabled property of each invidual validator. To emit the client-side scripts in the page, you can take a quick look at the methods of the Page class. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuipagememberstopic.asp[^]
I Already disabled it in the client side ,but I can't disable it at the server side ,when I click on the button in one control ,it fires the other validation controls at the other user control (after running the server side script under this butoon),!! ,so what can I do ?
-
I Already disabled it in the client side ,but I can't disable it at the server side ,when I click on the button in one control ,it fires the other validation controls at the other user control (after running the server side script under this butoon),!! ,so what can I do ?
Hi there, In this case, you need a way to determine which user control is submitted, then in the code behind you can enable/disable the validators of each control. For example, I use a hidden element in a user control, then depending on which button is clicked at the client side, I am going to set a value for the right hidden element. The sample code in the code behind of a user control looks something like this:
private void Page_Load(object sender, System.EventArgs e)
{
...
if(Page.IsPostBack)
{
string submitType = Request[this.hiddenElement.Name];
if(submitType=="")
{
EnableValidators(false);
}
}
...
}private void Page_PreRender(object sender, System.EventArgs e)
{
//By default, all validators are enabled.
EnableValidators(true);
}In the EnableValidators() method, you can enable/disable the validators based on the method parameter.
-
Hi there, In this case, you need a way to determine which user control is submitted, then in the code behind you can enable/disable the validators of each control. For example, I use a hidden element in a user control, then depending on which button is clicked at the client side, I am going to set a value for the right hidden element. The sample code in the code behind of a user control looks something like this:
private void Page_Load(object sender, System.EventArgs e)
{
...
if(Page.IsPostBack)
{
string submitType = Request[this.hiddenElement.Name];
if(submitType=="")
{
EnableValidators(false);
}
}
...
}private void Page_PreRender(object sender, System.EventArgs e)
{
//By default, all validators are enabled.
EnableValidators(true);
}In the EnableValidators() method, you can enable/disable the validators based on the method parameter.
sorry,can you please write more code for the client side and the server side,because it's so difficult to me to handle it thanks