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 April 29th, 2004, 02:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default Validation Controls

I have validation controls and I'm wanting to create my own to make sure a checkbox is checked. Do I create a custom validator or use Javascript and if I use Javascript how do I go about making it validate along with the other validators?

 
Old May 1st, 2004, 02:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

u can use RequiredFieldValidator or JavaScript to validate, but I guess u cant use RequiredFieldValidator for checkbox! (I couldnt)
I hope this samle will help u to validate checkbox in JavaScript :
Code:
<Script Runat="Server">
    private void Page_Load(object sender, System.EventArgs e)
    {
        Button1.Attributes["OnClick"]="return test();";
    }
</Script>
<html>
    <head>
        <script language="javascript">
            function test()
            {
                if( Form1.checkbox.checked )
                    return true;
                else
                    return false;
            }
        </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <INPUT id="checkbox" type="checkbox">
            <asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
            <INPUT type="submit" value="Submit" onclick="return test();">
        </form>
    </body>
</html>
I use both HTML-Button & Web Control-Button for this samle. Page_Load is used bc I got error when I was trying for Web-Control button to validate.

HTH.

Always:),
Hovik Melkomian.
 
Old May 3rd, 2004, 07:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

The problem is I have existing Validation controls and I'm trying to make sure the check box is checked. The question is how do I add code to make sure the checkbox is checked without breaking the Validations controls already on the page?

Is there a way to add Javascript like melvik shows with the Validation controls or do I have to create a Custom Validation control.

 
Old May 3rd, 2004, 09:03 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

I guess u'd better to use Custom one, since u cant use requiredfieldvalidator & dont wanna use JavaScript.

Always:),
Hovik Melkomian.
 
Old May 3rd, 2004, 09:42 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Ahh... This is why your suggestion didn't work melvik.

"Using the CustomValidator server control in the way we’ve just done integrates better with the life-cycle of your page. I openly recommend playing with various implementations of custom server-side validation routines, and the .NET Framework makes it very easy to roll your own. However, if you’re of the programming flow

That we’ve populated our CheckBoxList control dynamically might make this tough for a client-side JavaScript routine, because we’d have to know ahead of time the specific values of the ID and Name attributes of the HTML Checkbox elements to access them, if we were to use the DHTML document object model (DOM), which would normally be the case. This is significantly easier using server-side logic. The .NET Framework automatically assigns each element in a WebForm a unique ID and/or Name, eliminating the chance for problems that would arise from clashes. However, this still doesn’t make it any easier to program against them, not knowing what they’ll be at design-time.

It’s not impossible – it would just take more work."

**I'm dynamically adding the checkboxs.

from - http://www.kuam.com/techtalk/customvalidatorclass.htm
 
Old May 3rd, 2004, 02:19 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Here is the solution:

<asp:checkbox id="isAgeOk" runat="server" text="I am over 18."></asp:checkbox>
<asp:customvalidator id="isAgeOkValidator" runat="server"
        errormessage="You must check the 'I am over 18.' checkbox."
        onservervalidate="isAgeOkValidator_ServerValidate"
        clientvalidationfunction="ValidateCheckbox" />

-------------------------

<script runat="server">

    Sub isAgeOkValidator_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        args.IsValid = isAgeOk.Checked
    End Sub

</script>



<script language="javascript">

    function ValidateCheckbox(source, arguments)
    { arguments.IsValid = form1.isAgeOk.checked; }
</script>



 
Old May 3rd, 2004, 05:12 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi stu9820 (do you have another name as well??)

You can also take a look at Page.RegisterArrayDeclaration. This method allows you to create a client side array that is inserted at the end of the page. You could dump the ClientIDs of the controls you're interested in into this array, so you can iterate over this array from any JavaScript function to check their state, value etc.

I have used it in the past a couple of times for some complex client side validation, and it has served me well.

Cheers,

Imar
 
Old May 4th, 2004, 07:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
quote:Originally posted by Imar
 (do you have another name as well??)
What do you mean?

 
Old May 4th, 2004, 07:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I was wondering if you had a "real name" as well. That is, I can't believe you were named stu9820 at birth.... ;)

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Girl You Have No Faith In Medicine by The White Stripes (Track 13 from the album: Elephant) What's This?
 
Old May 4th, 2004, 08:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I am trying the Page.RegisterArrayDeclaration Method and I keep getting 'obj' is undefined.

My code:
HTML
<TABLE id="Table1" cellSpacing="0" cellPadding="0" border="0">
    <TR>
        <TD width="90">First Name:</TD>
        <TD>
        <asp:TextBox id="txtFirstName" runat="server"></asp:TextBox></TD>
    </TR>
    <TR>
        <TD>Last Name:</TD>
        <TD>
        <asp:TextBox id="txtLastName" runat="server"></asp:TextBox></TD>
    </TR>
    <TR>
        <TD>Email:</TD>
        <TD>
        <asp:TextBox id="txtEmail" runat="server"></asp:TextBox></TD>
    </TR>
    <TR>
        <TD>Phone:</TD>
        <TD>
        <asp:TextBox id="txtPhone" runat="server"></asp:TextBox></TD>
    </TR>
    <TR vAlign="top">
        <TD>Comments:</TD>
        <TD>
        <asp:TextBox id="txtComments" runat="server" Width="208px" Height="80px" TextMode="MultiLine"></asp:TextBox></TD>
    </TR>
    <TR>
        <TD></TD>
        <TD>
        <asp:Button id="Button1" runat="server" Text="Send"></asp:Button></TD>
    </TR>
</TABLE>

CODE BEHIND:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim scriptString As String = "<script language=JavaScript> function doClick() {"
        scriptString += "for(var index=0;index < myArray.length;index++)"
        scriptString += " myArray[index].show(); } <"
        scriptString += "/" + "script>"

        RegisterStartupScript("arrayScript", scriptString)
        RegisterArrayDeclaration("myArray", "new obj('txtFirstName'),new obj('txtLastName'),new obj('txtEmail')")
    End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation controls and postback sdi126 ASP.NET 2.0 Basics 5 November 11th, 2007 12:08 PM
Validation controls in datalist anujrathi ASP.NET 1.0 and 1.1 Professional 4 July 26th, 2006 03:32 PM
Validation Controls andyj00 ASP.NET 1.0 and 1.1 Professional 3 June 9th, 2005 09:19 AM
validation involving 2 controls Renu ASP.NET 1.0 and 1.1 Basics 24 December 21st, 2004 06:33 AM





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