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.)