Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 May 8th, 2005, 06:55 PM
sh sh is offline
Registered User
 
Join Date: Jan 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validating portion of form

hi,

I am trying to get this code running in asp.net vb,
could someone please ceck it and let me know what is wrong with it spcially functions DisableValidators and EnableValidators....

Thanks in advance

<%@ Import NameSpace="System.Configuration" %>
<%@ Import NameSpace="System.Web.UI" %>
<%@ Import NameSpace="System.Web" %>
<%@ Import Namespace="System.Text" %>

<script language="VB" runat="server">
Sub submit_clicked(Src As Object, E As EventArgs)

    lblmsg.text = "Entering Validation"
    DisableValidators()

    If Box1.Checked = True then
        lblmsg.text = "If box 1 do Validation <br>"
        EnableValidators(Group1)
    End If

    If Box2.Checked = True then
        lblmsg.text = "If box2 do Validation <br>"
        EnableValidators(Group2)
     End If

    Page.Validate()

    If Page.IsValid then
        lblmsg.text = "The page is valid"
    End If

End Sub
Function DisableValidators()

    lblmsg.text = "Disabling all validator controls on page"
    Dim bv as BaseValidator
    For Each bv in Page.Validators
        bv.Enabled = false
    Next

End Function

Function EnableValidators(Control)

    Dim c as Control
    For Each c in Control
       If TypeOf c is RequiredFieldValidator Then
           lblmsg.text = "Enabling " & c.ID.ToString()
          CType(c, RequiredFieldValidator).Enabled = True
       End If
    Next

End Function
</script>
 
Old May 9th, 2005, 05:51 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

We're not here to proofread code. Are you getting a specific error message or it is just not working?

What are Group1 and Group2?

Note that you will never see the messages you are assigning to lblmsg except for the last assignment encountered. Only that value will be sent to the page. If you are attempted to debug with that label you may not think it's working when it is.

-Peter
 
Old May 10th, 2005, 04:18 AM
sh sh is offline
Registered User
 
Join Date: Jan 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI,

I am trying to validate a portion of asp.net form, each portion has it's checkbox and if checkox is ticked the validation for that portion only should take place.
I have added the html as well.
Group1/2 are place holders id.

Here is a sample problem I have:
When I tick the first checkbox(which is associated with group1 only), a group1(only) validation should happen and validation for second group(Group2) should be ignored. but the result is different, I get validation error message for all textboxes from all groups and submission doesn't happen until I do validate textboxes from unticked portion of the form.....

I appreciate if anyone can help I have a very very...very long form with millions of optional validations...(If I may refer it as such).

Many Thanks

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" debug="true" %>
<%@ Import NameSpace="System.Configuration" %>
<%@ Import NameSpace="System.Web.UI" %>
<%@ Import NameSpace="System.Web" %>
<%@ Import Namespace="System.Text" %>

<script language="VB" runat="server">
Sub validate(Src As Object, E As EventArgs)

    DisableValidators()

    If Box1.Checked then
        lblmsg.text = "If box 1 do Validation <br>"
        EnableValidators(Group1)
    End If

    If Box2.Checked then
        lblmsg.text = "If box2 do Validation <br>"
        EnableValidators(Group2)
     End If

    Page.Validate()

    If Page.IsValid then
        lblmsg.text = "The page is valid"
    End If

End Sub
Function DisableValidators()

    lblmsg.text = "Disabling all validator controls on page"
    Dim bv as BaseValidator
    For Each bv in Page.Validators
        bv.Enabled = false
    Next

End Function

Function EnableValidators(Control)

    Dim c as Control
    For Each c in Control
       If TypeOf c is RequiredFieldValidator Then
           lblmsg.text = "Enabling " & c.ID.ToString()
          CType(c, RequiredFieldValidator).Enabled = True
       End If
    Next

End Function
Sub submit_clicked(Src As Object, E As EventArgs)

        response.redirect("test2.aspx?sv=" & Text1.Text &"&st=" & Text2.Text)

End Sub
</script>
<html>
<body>
<form runat="server" >
<asp:label id="lblmsg" runat="server" />
  <table border=1>
    <tr>
      <td>
      <asp:CheckBox id="Box1" Text="Validate Group 1" runat="server"/></td>
      <td>
          <asp:Placeholder id="Group1" runat="server">
        Surname:<asp:TextBox id="Text1" runat="server" width="100"></asp:TextBox>
        <asp:RequiredFieldValidator id="RequiredFieldValidator1"
             ControlToValidate="Text1"
             Display="Static"
             InitialValue=""
             Width="100%"
             runat="server">*Please enter a value for surname
        </asp:RequiredFieldValidator>
        </asp:Placeholder>
     </td>
    </tr>
    <tr>
      <td> <asp:CheckBox id="Box2" Text="Validate Group 2" runat="server"/> </td>
      <td>
          <asp:Placeholder id="Group2" runat="server">
        ZIP Code: <asp:TextBox id="Text2" runat="server" width="100"></asp:TextBox>
        <asp:RequiredFieldValidator id="RequiredFieldValidator2"
                ControlToValidate="Text2"
                Display="Static"
                InitialValue="" Width="100%" runat="server">Please enter a value for zip code
        </asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server"
            ControlToValidate="Text2"
            ValidationExpression="^\d{5}$"
            Display="Static"
            Font-Name="verdana"
            Font-Size="10pt">
            Zip code must be 5 numeric digits
        </asp:RegularExpressionValidator>

    </asp:Placeholder>
    </td>
    </tr>
  </table>
<asp:Button id="Button1" text="Validate" OnClick="validate" runat="server"/>
<input type="submit" id="submitID2" name="submitID"
                                    text="Apply" value="Submit"
                                    onserverclick="submit_clicked" runat="server"
                                    causeValidation="false" />
</form>
</body>
</html>
 
Old May 10th, 2005, 03:03 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I've found that control validators are often all or nothing. It's easier to set it up to validate on the server side when you need to do "section" or "block" validation because you can turn the validators on and off based on the necessary conditions before the Page.Validate method is called. However, on the server this is much more difficult because of the lack of ability to turn off certain validators from client-side code. Without setting up a test page and playing with it myself I don't know what the best approach is.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML portion of aspx file SKhna ASP.NET 2.0 Basics 3 March 25th, 2008 10:52 AM
In Access - look at portion of string usvinyl Access 1 September 23rd, 2004 03:06 PM
validating fields in windows form ranakdinesh BOOK: Professional C#, 2nd and 3rd Editions 2 May 29th, 2004 08:28 AM
Validating a form field against a database microchip Classic ASP Basics 2 April 24th, 2004 04:07 PM
form validating erin VBScript 0 October 27th, 2003 02:02 PM





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