Validation Control

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I have a validation control in my page that upon any invalid data, it
disables all buttons in the page. basicly i don't have any postback in the
page if the validator finds any error.

How can have the validator just disable certain control's postback and other
part of page continue their functionality.

Thanks,
Ali
 
Thanks for reply

The problem is i have 3 buttons each button has its own controls to be
validated. So i need each Button validate it's own controls and ignore other
controls' validation

Ali
 
Check out my post in the thread 'validating portions of asp.net page' on Jan
5.
It might be what you are looking for.
 
Hi A.M,


Thank you for using MSDN Newsgroup! My name is Steven, and I'll be
assisting you on this issue.
From your description, you've a ASP.NET web page which as serveral entry
fields, validator controls and also buttons on it. And you want each
button's click event(both client side and serverside?) cause only its
related validator to work rather than notify all the validators on a page.
If there is anything I misunderstood, please feel free to let me know.

First, I'd like to suggest you have a view on the tech article Alphonse
Giambrone has provided:
#ASP.NET Validation in Depth
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspplusvalid.asp?frame=t
rue
This article has detailedly description the background of the ASP.NET
validation controls and the details how they work both on clientside and
serverside. I think you may get many good ideas via this articles.

As for the problem in this issue, I've do some researches based on the
above article. Here is some suggestions on it:

If clientside validation of a validator control is enabled, the ASP.NET
page will link some build in client side scripts to the page. It contains a
group of clientside APIS which control the clientside's validation
operation. Here is three important client APIS(quote from MSDN):

Name
ValidatorValidate(val):
Takes a client-validator as input. Makes the validator check its input and
update its display.

ValidatorEnable(val, enable):
Takes a client-validator and a Boolean value. Enables or disables a client
validator. Being disabled will stop it from evaluating and it will always
appear valid.

ValidatorHookupControl(control, val):
Takes an input HTML element and a client-validator. Modifies or creates the
element's change event so that it updates the validator when changed. This
can be useful for custom validators that depend on multiple input values.


What we should is just to make use of these APIs, for example the
ValidatorEnable(val, enable): can control which validator control to be
enable or not. So if we what only part of the validators to be enable when
a certain button is clicked. We can use this fuction to disable the other
validators in the certain button's onmousedown event.
For example, if we have two validators named "rfvLeft" and "rfvRight" ,when
a button is clicked, we want only "rfvLeft" to work, then we could use the
following code:

function LeftValidator()
{
ValidatorEnable(document.all('rfvLeft'), true);
ValidatorEnable(document.all('rfvRight'), false);
}

Thus, we can disable the "rfvRight" 's client validation when the button is
clicked(only rfvLeft will take validation action).
Also, it's easier to make this for server side Validation, just add the
below code in the certain button's serverside click event:
private void btnVLeft_Click(object sender, System.EventArgs e)
{

rfvLeft.Enabled = true;
rfvRight.Enabled = false;
}

To make it clearly, I've also made a simple page to show how to implement
this, the page have two textboxes, two requiredfieldvalidator and two
buttons, each button will cause only one validator to work, here is the
page and its code behind :

-----------------------------aspx page-----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Validation</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function LeftValidator()
{
ValidatorEnable(document.all('rfvLeft'), true);
ValidatorEnable(document.all('rfvRight'), false);


}

function RightValidator()
{
ValidatorEnable(document.all('rfvLeft'), false);
ValidatorEnable(document.all('rfvRight'), true);

}


</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td colspan="2"></td>
</tr>
<tr>
<td>
<asp:TextBox id="txtLeft1" runat="server"></asp:TextBox></td>
<td>
<asp:TextBox id="txtRight1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button id="btnVLeft" runat="server" Text="Validate Left
Column"></asp:Button></td>
<td>
<asp:Button id="btnVRight" runat="server" Text="Validate Right
Column"></asp:Button></td>
</tr>
<tr>
<td>
<asp:RequiredFieldValidator id="rfvLeft" runat="server"
ErrorMessage="RequiredFieldValidator Left"
ControlToValidate="txtLeft1"></asp:RequiredFieldValidator>
</td>
<td>
<asp:RequiredFieldValidator id="rfvRight" runat="server"
ErrorMessage="RequiredFieldValidator Right"
ControlToValidate="txtRight1"></asp:RequiredFieldValidator>
</td>
</tr>
</table>
</form>
</body>
</HTML>
----------------------------------------------------------------------------
-------------

-----------------------code behind page
class-----------------------------------------
public class Validation : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtLeft1;
protected System.Web.UI.WebControls.TextBox txtRight1;
protected System.Web.UI.WebControls.Button btnVLeft;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvLeft;
protected System.Web.UI.WebControls.RequiredFieldValidator rfvRight;
protected System.Web.UI.WebControls.Button btnVRight;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
btnVLeft.Attributes.Add("onmousedown","LeftValidator()");
btnVRight.Attributes.Add("onmousedown","RightValidator()");



}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnVLeft.Click += new System.EventHandler(this.btnVLeft_Click);
this.btnVRight.Click += new System.EventHandler(this.btnVRight_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnVLeft_Click(object sender, System.EventArgs e)
{

rfvLeft.Enabled = true;
rfvRight.Enabled = false;
}

private void btnVRight_Click(object sender, System.EventArgs e)
{
Response.Write("<br>Right Field is OK!");
rfvLeft.Enabled = false;
rfvRight.Enabled = true;
}
}

--------------------------------------------------------------------------

Please check out the preceding tech article and try out my code. If you
have any questions on it, please feel free to let me know.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Thanks Steven for help.
I have tested your way, it worked.
I also tried to have 2 <form></form> tags, so validators of each form
doesn't validate the other.

It didn't work.

Am i missing something or my idea doesn't work.

Thanks,
Ali
 
Hi Ali,

Thanks for your response. As for the using multi <form> tag failed
condition, I think this is also due to the mechanism of the ASP.NET
validator controls. Since in ASP.NET web page. Only one "runat=server"
<form> is allowed, all the server control or other buildin server
components or other functionalities are all based on one server form. The
validator control is the same, too. Also, as I mentioned in the last reply,
the validator controls's client validation depends on a group of client
javascript APIS, these APIS also based on the one server form(the form
which is set as "runat=server"). So if we manually add multiform to use the
validator control, there will be something incorrect with the validator
controls' internal mechanism.
Also, since the suggestion I provided in last reply (the sample page) which
use one form to contain multi validators to work separately, do you think
it appropriate to implement your question on that mode?

Also, if you do want some solution on mulit <form> tag validation, you may
look for some third party components which may provide such functionality.
And I think the link Ken Cox provided last time is a good 3th-party
validation control, you may have a look at it.:
http://www.peterblum.com/VAM/Home.aspx

If you have any questions, please feel free to post here.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top