470,563 Members | 2,426 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,563 developers. It's quick & easy.

validating portions of asp.net page.

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..
Nov 18 '05 #1
7 1475
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 ?

"ani" <an*******@discussions.microsoft.com> wrote in message
news: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..

Nov 18 '05 #2
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
"ani" <an*******@discussions.microsoft.com> wrote in message
news: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..

Nov 18 '05 #3
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..


-----Original Message-----
All validators will fire on a submit unless they have been enabled. One wayI've found around this, or to set CausesValidation=False for one of thesubmit buttons, and do it's validations on the server side. Not really thecleanest solution since you forgoe the advantages of the validators.
However, you can't disable a certain set of validators on a submit, sincethey fire before the postback. Perhaps you can disable a set of validatorsbased on some condition during page_load. That might not work for yoursolutions though.
But ... May I ask why you're using two buttons to submit two differentcontrols ?

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

.

Nov 18 '05 #4
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/de...pplusvalid.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
"ani" <an*******@discussions.microsoft.com> wrote in message
news: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..

Nov 18 '05 #5
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: PL****@PeterBlum.com

"ani" <an*******@discussions.microsoft.com> wrote in message
news: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..

Nov 18 '05 #6
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 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..

.

Nov 18 '05 #7
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 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
"ani" <an*******@discussions.microsoft.com> wrote in

message
news: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..

.

Nov 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by The Plankmeister | last post: by
6 posts views Thread by mike | last post: by
12 posts views Thread by Claire Lescarret | last post: by
7 posts views Thread by Armand Karlsen | last post: by
2 posts views Thread by Raghu R | last post: by
reply views Thread by Gary Shell | last post: by
17 posts views Thread by stathis gotsis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.