Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET Website Programming Problem-Design-Solution
This is the forum to discuss the Wrox book ASP.NET Website Programming: Problem - Design - Solution, Visual Basic .NET Edition by Marco Bellinaso, Kevin Hoffman; ISBN: 9780764543869
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET Website Programming Problem-Design-Solution section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 29th, 2003, 05:29 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Two buttons, One page, + req~validators?

Here is the scenario:
You have two buttons on a page, one of the buttons uses required~Validators to validate before postback. However, you don't want the page to validate if the other button is clicked.
How do you get around this?
For example: the myaccount.aspx page validates before using the LinkButton SignOut, when all you want is the user to sign out. But you need the page to validate when the user clicks the SubmitChanges button.

Any idea how to get around this?
d.

  #2 (permalink)  
Old September 30th, 2003, 02:15 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

This scenario is pretty common, but not supported by the framework directly.

Here's what I do to solve this:

1. Create a function that disables all validators on the page. You can use code like this for that:
Code:
    for (var i = 0; i < Page_Validators.length; i++)
    {
        Page_Validators[i].enabled = false;
    }
    2. As parameters for this function, allow the names of the controls you *do* want to validate from one button click:
Code:
    for(var j = 0; j <= arguments.length - 1; j++)
Code:
    {
        document.getElementById(arguments[j]).enabled = true;
    }
    3. Add an onclick attribute to the button that causes validation (use: MyButton.Attributes.Add) and have it call the function from step 1. As arguments, pass one or more ClientIDs to this function.4. Repeat this steps for other buttons you have on your site / page. If you want, you can add the JavaScript function to its own .js page, and link to that in your ASPX page.

Now, what happens is this: The page loads, and, let's say, two buttons have the behavior I described. Once you click button 1, you call the JavaScript function and pass 'valUserName' as the control you want to validate. The code in the function disables all validators, and then reenables the valUserName validator. The same way, if you click button 2 and pass valSearch, all validators are enabled, except for valSearch.

You may need to repeat this process for the Validation Summaries (you'll find an array called Page_ValidationSummaries at the end of your page that contains all their names). In the onclick of the button, disable them all, except for the one that is attached to the button.

You'll also need to repeat this for browsers that don't use the client side JavaScript. However, this may be easier. In the Button_Click event of each button, you can easily figure out what the user is trying to do, then disable the validators you don't need and call Page.Validate().

Hope this helps, and if you have any questions, feel free to ask them here. Working with the validators from client side script isn't the most intuitive thing to do.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #3 (permalink)  
Old September 30th, 2003, 11:18 AM
Registered User
 
Join Date: Sep 2003
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If u only want 1 button to validate then you only have to turn off validation for each button. this is really simple and requires almost no coding.

<asp:button id="button1" runat="server" Text="Button" CausesValidation="False"></asp:Button>

Notice the "CausesValidation" above, turn on and off validation by specifying true or false.


  #4 (permalink)  
Old September 30th, 2003, 11:19 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Imar and reps for the very infomative posts.

The CausesValidation="False" actually accomplished what I was hoping to accomplish in a quick and easy way. Did the architects of Visual Studio.Net think of everything?


They sure came close....
d.

  #5 (permalink)  
Old October 1st, 2003, 01:20 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Right, I think I misunderstood your requirements. The thing with CausesValidation is that it actually disables one of the two validation processes.

If you have a form with a Login Box and a Search box for example, each with their own button, you want both buttons to fire validation when clicked. But when you click, say, the Login button, you don't want to validate the Search text box and vice versa.

With my solution, both validations will still work, but only one at the time. By using CausesValidation, you just disable one of the two buttons altogether. From your question I gathered you were looking for a solutions with two or more validators at the same time on the page.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.


Similar Threads
Thread Thread Starter Forum Replies Last Post
2 buttons going to same ASP page chrscote Classic ASP Basics 1 June 1st, 2005 03:00 PM
Page 1-2-3-4 etc.... Next and Previous buttons crmpicco Classic ASP Basics 1 March 18th, 2005 12:00 PM
Error: 'Page Validators' is undefined camelia_flower C# 3 September 20th, 2004 08:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.