validating portions of asp.net page.

A

ani

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

George Durzi

All validators will fire on a submit unless they have been enabled. One way
I've found around this, or to set CausesValidation=False for one of the
submit buttons, and do it's validations on the server side. Not really the
cleanest solution since you forgoe the advantages of the validators.

However, you can't disable a certain set of validators on a submit, since
they fire before the postback. Perhaps you can disable a set of validators
based on some condition during page_load. That might not work for your
solutions though.


But ... May I ask why you're using two buttons to submit two different
controls ?
 
P

Prodip Saha

The easiest way would be adding code at the beginning of Button_Click event.
You can write a bool Private function to do this. If the function returns
false, then you can throw a new exception and catch it in the Button_Click,
and print some message to a label control.

Prodip Saha
 
A

ani

Thanks for the response.

This is a search page that i am working on and I have one
button for each text box that I need to validate. There
are two textboxes and each has got its own button to
search. How do I validate each of the controls for two
different submit buttons.


Thanks..
 
A

Alphonse Giambrone

I just went through that having two user controls on a page and wanting to
validate only the set of controls associated with the user control
containing 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 will
still be evaluated when the form is posted back. Therefore you need a method
of determining which validators were disabled on the client side and disable
the same ones on the server side.
In my case I was working with editable datagrids and the validators were
contained in them. The clientId of all the grid's child controls includes
the ID of the datagrid.
In the OnMouseDown event of the 'submit' button I call a javascript function
and pass it the id of the datagrid.
The javascript function would loop through all the validators on the page
and enable only those whose id (ClientID on server side) contains the id
passed 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 the
list of enabled validators from the hidden text box enable the validators in
the list and disable all others, before any are evaluated.

HTH.
 
P

Peter Blum

This concept is known as "Validation Groups" and will be a feature of
ASP.NET 2.0.

Until then, several third party products provide this feature. Mine is
"Professional Validation And More".
(http://www.peterblum.com/vam/home.aspx). With it, you simply define a name
for each group of controls and their related submit button. Assign that name
to the Group property on the validators and the button. It works fully on
the client side without writing any additional code.

--- Peter Blum
www.PeterBlum.com
Email: (e-mail address removed)
 
A

ani

Could you please give me a sample.

Thanks.

-----Original Message-----
I just went through that having two user controls on a page and wanting to
validate only the set of controls associated with the user control
containing 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 will
still be evaluated when the form is posted back. Therefore you need a method
of determining which validators were disabled on the client side and disable
the same ones on the server side.
In my case I was working with editable datagrids and the validators were
contained in them. The clientId of all the grid's child controls includes
the ID of the datagrid.
In the OnMouseDown event of the 'submit' button I call a javascript function
and pass it the id of the datagrid.
The javascript function would loop through all the validators on the page
and enable only those whose id (ClientID on server side) contains the id
passed 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 the
list of enabled validators from the hidden text box enable the validators in
the list and disable all others, before any are evaluated.

HTH.
--

Alphonse Giambrone
Email: a-giam at customdatasolutions dot us





.
 
A

Alphonse Giambrone

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","EnableValidators("Group1_);")
btnGroup2.Attributes.Add("OnMousedown","EnableValidators("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.id;

if (strID.indexOf(strEnable)>-1)

{

ValidatorEnable(Page_Validators, true);

if (strEnableVals != "")

{

strEnableVals += ",";

}

strEnableVals += Page_Validators.id;

}

else

{

ValidatorEnable(Page_Validators, 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top