Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

checkbox validation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ned Balzer - 19 Dec 2007 17:28 GMT
I posted this this morning but it never went through, so I am trying
again -- apologies for the duplication if so.

I need to validate several checkboxes on an asp.net 2.0 page.  I don't
need to validate that they are checked; instead, I am trying to
prevent potential SQL or code injection via checkbox controls.  These
checkboxes are inside a formview.

I found examples (e.g at http://msdn2.microsoft.com/en-us/library/aa479013.aspx)
of how to validate that at least one in a group of checkboxes, or one
of a checkboxlist, has been checked, but my requirement is a little
different.

What I'd like to do is write a server side and client side functions
that several customvalidation controls can share.  So the function
would determine which checkbox control is failing validation and
respond appropriately.

The example code looks like this:

<script runat="server">

   Sub CustomValidator1_ServerValidate(source As Object, _
      args As ServerValidateEventArgs)

      args.IsValid = (CheckBox1.Checked = True)
   End Sub

   Sub Button1_Click(sender As Object, e As EventArgs)
      If Page.IsValid Then
         Label1.Text = "Thank you for your donation!"
      Else
         Label1.Text = ""
      End If
   End Sub

</script>
...
           <asp:CheckBox id="CheckBox1" runat="server"
            Text="Donate $10"></asp:CheckBox>
           &nbsp;
           <asp:CustomValidator id="CustomValidator1"
            runat="server" ErrorMessage="Please donate $10"
            OnServerValidate="CustomValidator1_ServerValidate">
           </asp:CustomValidator>

The problem is that the checkbox control is referenced in Sub
CustomValidator1_ServerValidate by its ID, but I'd like the server
side validation subroutine to handle any number of different
checkboxes.

Is it possible to determine which checkbox is failing validation by
examining either of the two parameters passed to Sub
CustomValidator1_ServerValidate? (either "source" or
"ServerValidateEventArgs")?

Thanks.

-- Ned Balzer
Mohamad Elarabi - 19 Dec 2007 20:01 GMT
Ned, I don't think what you're trying to do is possible because in order for
the information about the checkbox to be passed into the arguments you will
have to set the ControlToValidate attribute on the CustomValidator, but then
that will lock you down to one control to validate.  However there might be a
workaround.

If you prefix the IDs of the checkboxes you want to validate with something
unique like "cbx" you can then loop through the page controls and identify
the controls with cbx in their ids and then do your validation on each one
and update the IsValid and the ErrorMessage accordingly. Your serverValidate
should look something like this

Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As _
ServerValidateEventArgs)
 For Each oControl As System.Web.UI.WebControls.WebControl In Page.Controls
   If Left(oControl.ID, 3) = "cbx" Then
     If CType(oControl, CheckBox).Checked Then
       args.IsValid = args.IsValid And (CType(oControl, CheckBox).Checked =
True)
       SysMessage.ErrorMessage &= oControl.ID & " is not valid.<br/>"
     End If
   End If
 Next
End Sub

Please let me know if this helps. Good luck.

Signature

Mohamad Elarabi
Lead Developer. MCTS, MCPD.

> I posted this this morning but it never went through, so I am trying
> again -- apologies for the duplication if so.
[quoted text clipped - 55 lines]
>
> -- Ned Balzer

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.