Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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
 
Old January 23rd, 2011, 03:37 PM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default checkbox validation

I need help with the following:

I'm using a detailsview to edit data; one of the data-controls is a checkbox.
On submitting I want to make sure that the checkbox is checked. requierd field validator doesn't work and also a custom validator doesn't work:

Code:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim bool As CheckBox = DetailsView1.FindControl("BeterGemeld")

        If bool.Checked = True Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If
    End Sub
WHY????
 
Old January 23rd, 2011, 04:36 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
and also a custom validator doesn't work:
Why not? The code you have shoud work fine when editing an item in the DetailsView.

Are you sure ServerValidate is called? Did you hook up that method using something like this in your markup: OnServerValidate="CustomValidator1_ServerValidate" ?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 23rd, 2011, 05:03 PM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Code:
<asp:CustomValidator ID="CustomValidator1" runat="server" 
                        CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="CustomValidator" 
                        onservervalidate="CustomValidator1_ServerValidate" 
                        ValidationGroup="CheckBox"></asp:CustomValidator>
yep, here it is...

i get this error message:

Code:
Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 17:         Dim bool As CheckBox = CType(DetailsView1.FindControl("BeterGemeld"), CheckBox)
Line 18: 
Line 19:         If bool.Checked = True Then
Line 20:             args.IsValid = True
Line 21:         Else
from which line 19 is red
 
Old January 23rd, 2011, 06:19 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

And how does the code for the DetailsView look? Somehow, this code can't find it so it must be placed in a different location than this code expects.....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
bertus (January 24th, 2011)
 
Old January 24th, 2011, 01:47 AM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default got it!

Code:
<EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" 
                        Checked='<%# Bind("BeterGemeld") %>' />
                    <asp:CustomValidator ID="CustomValidator1" runat="server" 
                        CssClass="ErrorMessage" Display="Dynamic" ErrorMessage="CustomValidator" 
                        onservervalidate="CustomValidator1_ServerValidate" 
                        ValidationGroup="CheckBox"></asp:CustomValidator>
                </EditItemTemplate>
I had to use "CheckBox1" in findControl....

And now it works fine...





Similar Threads
Thread Thread Starter Forum Replies Last Post
CheckBox Validation maheshoo7 Pro JSP 3 March 10th, 2008 07:55 AM
checkbox validation on client side using Javascrip sansircar ASP.NET 1.0 and 1.1 Professional 0 October 17th, 2005 07:43 PM
Checkbox list validation lily611 General .NET 6 December 10th, 2004 03:33 AM





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