On server side:
Give all your validators a grouping id. Such as Group1_val1, Group1_val2,
Group2_val1, Group2_val2.
Add an OnMousedown event to the button (I did it in Page_Load) that will
cause validation for each group.
btnGroup1.Attributes.Add("OnMousedown","EnableVali dators("Group1_);")
btnGroup2.Attributes.Add("OnMousedown","EnableVali dators("Group2_);")
Make sure your page contains a hidden textbox who's id=txtEnableVals.
Add the following routine to your project in a class or module so it can be
used for whatever page you like:
Public Sub EnableServerValidators(ByVal strEnableList As String, ByVal
objVals As System.Web.UI.ValidatorCollection)
'v 1/2/04
'Call in page load event on postback.
'Enables validators in the comma delimited string 'strEnableList' and
disable all others
'For more info see 'AddDgrdEnableValidators' and javascript function
'EnableValidators'
'If the list is empty, all validators are disabled
'Enumeration taken from help/MSDN articles.
Dim strEnableVals() As String
Dim strVal As String
Dim shrIdx As Short
Dim shrVals As Short
strEnableVals = Split(strEnableList, ",")
shrVals = UBound(strEnableVals)
' Get 'Validators' of the page to collVals.
Dim collVals As System.Web.UI.ValidatorCollection = objVals
' Get the Enumerator.
Dim enumVals As IEnumerator = collVals.GetEnumerator()
While enumVals.MoveNext()
'enumVals.Current have all the expected properties, methods expected of
validators.
'They just don't show up with intellisense
strVal = enumVals.Current.clientid
For shrIdx = 0 To shrVals
If strVal = strEnableVals(shrIdx) AndAlso enumVals.Current.enabled = False
Then
enumVals.Current.enabled = True
Exit For
End If
Next
If shrIdx > shrVals AndAlso enumVals.Current.enabled = True Then
enumVals.Current.enabled = False
End If
End While
End Sub
Add the following to the page_load event:
If IsPostBack Then
EnableServerValidators(txtEnableVals.Value, Page.Validators)
End If
Client side:
function EnableValidators(strEnable)
{
/************************************************** ********************
v 1/2/04
Enable validators whose clientid contains the string passed in strEnable
Disable all others.
This is done utilizing the .NET supplied validator array 'Page_Validators'
and
'ValidatorEnable' function.
Useful for when there are multiple submit buttons on a page and different
validators should be enabled for each button.
The buttons' OnMouseDown event should be:
OnMousedown="EnableValidators('stringToEnable');"
Usuall added by server side code
All validators should be enabled by design on server side.
Enabling on client side causes validators to validate so it is not desirable
to enable any validators on client side that should not be evaluated at that
time.
Since this only affects client side validation, the Enabled validators
clientid's
are passed back to the server through a hidden text box (txtEnableVals) so
they can be
Enabled prior to server side validation.
************************************************** *********************/
var i;
var strID;
var strEnableVals = "";
var txtEnable;
txtEnable = document.getElementById("txtEnableVals");
if (txtEnable != null)
{
for (i = 0; i < Page_Validators.length; i++)
{
strID = Page_Validators[i].id;
if (strID.indexOf(strEnable)>-1)
{
ValidatorEnable(Page_Validators[i], true);
if (strEnableVals != "")
{
strEnableVals += ",";
}
strEnableVals += Page_Validators[i].id;
}
else
{
ValidatorEnable(Page_Validators[i], false);
}
}
txtEnable.value = strEnableVals
}
}
When btnGroup1 causes the submit, only validators whose id contains 'Group1'
will be evaluated and likewise when btnGroup2 submits, only validators whose
id contains 'Group2' will be evaluated.
--
Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"ani" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl...
Could you please give me a sample.
Thanks.
-----Original Message-----
I just went through that having two user controls on a
page and wanting tovalidate only the set of controls associated with the
user controlcontaining the button that was clicked.
The following article was very helpful.
http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/dnaspp/html/aspplusvalid.asp
What I basically came up with is:
Validators can be disabled on the client side using
javascript, but willstill be evaluated when the form is posted back.
Therefore you need a methodof determining which validators were disabled on the
client side and disablethe same ones on the server side.
In my case I was working with editable datagrids and the
validators werecontained in them. The clientId of all the grid's child
controls includesthe ID of the datagrid.
In the OnMouseDown event of the 'submit' button I call a
javascript functionand pass it the id of the datagrid.
The javascript function would loop through all the
validators on the pageand enable only those whose id (ClientID on server side)
contains the idpassed to the function.
A comma separated list of enabled validators is placed in
a hidden text box.
Then on the server side in the page load event (if
postback), I retrieve thelist of enabled validators from the hidden text box
enable the validators inthe list and disable all others, before any are evaluated.
HTH.
--
Alphonse Giambrone
Email: a-giam at customdatasolutions dot us
"ani" <an*******@discussions.microsoft.com> wrote in
messagenews:03****************************@phx.gbl... I have two submit buttons in my form which need to
validate two different controls on the page . How do I
validate portions of the asp.net page. The two submit
buttons should validate their respective controls. If
anyone has got any articles or sample pages, please
forward..
Thanks..
.