Hi David,
Thank you for your post!
Based on my understanding, the question is: How to enable or disable
individual validators based on which button the user clicks. If I have
misunderstood anything, please feel free to post here.
ASP.NET 2.0 introduces a new "ValidationGroup" property on validation and
input controls. The validation groups help you to group the controls in a
single page and have separate submit buttons for each group, so that you
can submit each group individually. Each group can have separate validation
controls.
To see more detailed information about "ValidationGroup", see following
article written by Scott Guthrie:
http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx
However, your issue still cannot be resolved using this "ValidationGroup"
feature. Since currently "ValidationGroup" property can only specify one
group.
Besides Nathan's suggestion about using server-side events to manually
validate controls, we can use another approach which requires client-side
javascript being enabled.
Here's a sample WebForm which demostrates this technique using your
requirements:
=====================
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btn1" OnClientClick="javascript:enableValidators();"
runat="server" Text="btn1" />
<br />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator1" runat="server"
ControlToValidate="txt1" ErrorMessage="required
txt1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn2" />
<asp:Button ID="btn3"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn3" />
<br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator2" runat="server"
ControlToValidate="txt2" ErrorMessage="required
txt2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn4"
OnClientClick="javascript:enableValidators(validator1, validator2);"
runat="server" Text="btn4"/>
</form>
<script language="javascript">
enableValidators();
function enableValidators() {
for (i = 0; i < Page_Validators.length; i++) {
ValidatorEnable(Page_Validators
, false);
}
for (i = 0; i < arguments.length; i++) {
ValidatorEnable(arguments, true);
}
}
</script>
</body>
</html>
=====================
Hope this helps. If there's anything unclear, please feel free to post here.
Regards,
Walter Wang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.