Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 4th, 2011, 10:03 AM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default Validation

Hey everybody,

I built a detailsview which defaults to "insert" in order to insert data.(duh)
One of the requiered data is a date, therefore I used a 'Calendar' in the insertItem template. This works fine, but now I want to Validate that a date was selected; a 'requieredFieldValidator' doesn't work, I am guessing I should be using a CustomValidator, but how?

Bertus
 
Old January 4th, 2011, 02:14 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 Bertus,

You could use a CompareValidator with its type set to DataType and its DataType to date. That's what you would normally for a date entered in, say, a TextBox.

The problem, however, with a Calendar is that it is already set to a valid date: DateTime.MinValue. What you could do is indeed use a CustomValidator, and then in its ServerValidate method you can check if the date matches your criteria, whatever they are...

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!
The Following User Says Thank You to Imar For This Useful Post:
bertus (January 4th, 2011)
 
Old January 4th, 2011, 02:44 PM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default Validation (encore)

Ok, imar, but:

As the calendar is set (by default) to minValue, or in my case by getDate, which i think sets the date in the calendar to today, how can i validate that the user has selected the date instead of leaving the default date, so it's not a case of which date, but whether the user selected any date ( so didn't he/she leave it unchanged? ) or is there a way to set the calendar without any default date, so i can validate the selected date as a required field?

If i validate the calendar in code-behind, how do I address the calendar? With findControl?

Thanks for the help!

Bertus
 
Old January 4th, 2011, 03:02 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You have a few options:

1. Don't set the date and leave it to DateTime.MinValue. In ServerValidate, check that the SelectedDate is (much) larger than that.

2. Set a custom date such as 1/1/2000 and compare against that. Just make sure you don't choose a date that may be a valid user option

3. In ServerValidate, compare against the date you set on the Calendar.

And yes, DetailsView1.FindControl("Calendar1") is your ticket to the Calendar in ServerValidate.

Hope this helps,

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 5th, 2011)
 
Old January 6th, 2011, 04:55 PM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default Every journey starts with a first step....

Next step...do requiredFieldValidators and customValidators behave different in a detailsView? If i use only two reqFieldVal,rs for two textboxes in the top, the validation summary works perfect, if i use the customvalidator for the selected date in a calendar, the popup only shows the required field val,rs

If i quote-out the regularFieldVal,rs the calendar customvalidator doesn't trigger the popup screen, but the astrisk * (Customvalidator's text value) shows at the calendar....
 
Old January 7th, 2011, 08:43 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 guess it depends on your setup. If you only implement OnServerClick, you need a postback before validation is triggered....

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 7th, 2011, 03:05 PM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default desperate...

No, that wasn't it, all my validators have a runat="server" set, so please do help!

In the code snippets below (sorry for the large snippet) the requiredFieldValidators on the texboxes (Voornaam and Achternaam) work fine, they also 'trigger' the popup from the validationSummary.

But...as soon as I add a customValidator to the Calendar control (which only purpose is to decide whether any date is selected) I still only get a popup for the requiredFieldValidators...

If I comment out the requiredFieldValidators and then insert an empty detailsview I only get the asterisk (*) shown at the calendar control, so somehow it works a little, doesn't trigger the popup for the validationSummary...what;s going wrong?

My Validation summary:
Code:
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="ZiekMelding"
        CssClass="ErrorMessage" ShowMessageBox="True" ShowSummary="False" />
My detailsview:
Code:
    <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="600px" AutoGenerateRows="False"
        DataKeyNames="Id" DataSourceID="ZiekMeldingDataSource" DefaultMode="Insert" BackColor="White"
        BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
        <AlternatingRowStyle BackColor="#F7F7F7" />
        <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <Fields>
            <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True"
                SortExpression="Id" />
            <asp:TemplateField HeaderText="Voornaam" SortExpression="Voornaam">
                <InsertItemTemplate>
                    <asp:TextBox ID="Voornaam" runat="server" Width="220px" Text='<%# Bind("Voornaam") %>'
                        ToolTip="Voornaam van de zieke"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Voornaam"
                        CssClass="ErrorMessage" ErrorMessage="Vul de voornaam van de zieke in" ValidationGroup="ZiekMelding">*</asp:RequiredFieldValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Achternaam" SortExpression="Achternaam">
                <InsertItemTemplate>
                    <asp:TextBox ID="Achternaam" runat="server" Width="220px" Text='<%# Bind("Achternaam") %>'
                        ToolTip="Achternaam van de zieke"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="Achternaam"
                        CssClass="ErrorMessage" ErrorMessage="Vul de achternaam van de zieke in" ValidationGroup="ZiekMelding">*</asp:RequiredFieldValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Filiaal" SortExpression="Filiaal">
                <InsertItemTemplate>
                    <asp:DropDownList ID="FiliaalDDL" Width="220px" runat="server" AppendDataBoundItems="True"
                        DataSourceID="FilialenDataSource" DataTextField="Filiaal" DataValueField="Id"
                        SelectedValue='<%# Bind("Filiaal") %>' ToolTip="Filiaal waar de zieke normaal werkt"
                        CausesValidation="True">
                        <asp:ListItem Value="">Kies een filiaal</asp:ListItem>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="FilialenDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString999 %>"
                        SelectCommand="SELECT [Id], [Filiaal] FROM [Filiaal]"></asp:SqlDataSource>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Eerste Ziekdag" SortExpression="EersteZiekDag">
                <InsertItemTemplate>
                    <asp:Calendar ID="EersteZiekdag" runat="server" BackColor="White" BorderColor="#3366CC"
                        BorderWidth="1px" CellPadding="1" DayNameFormat="Shortest" Font-Names="Verdana"
                        Font-Size="8pt" ForeColor="#003399" Height="150px" SelectedDate='<%# Bind("EersteZiekDag") %>'
                        Width="220px" ToolTip="Klik hier op de eerste ziekdag">
                        <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
                        <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
                        <OtherMonthDayStyle ForeColor="#999999" />
                        <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
                        <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True"
                            Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
                        <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
                        <WeekendDayStyle BackColor="#CCCCFF" />
                    </asp:Calendar>
                    <asp:CustomValidator ID="CustomValidator1" runat="server" 
                        CssClass="ErrorMessage" ErrorMessage="Kies een eerste ziekdatum" 
                        onservervalidate="CustomValidator1_ServerValidate" 
                        ValidationGroup="ZiekMelding">*</asp:CustomValidator>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Verwachte beter-datum" SortExpression="VerwachteBeterDag">
                <InsertItemTemplate>
                    <asp:Calendar ID="VerwachteBeterDag" runat="server" BackColor="White" BorderColor="#3366CC"
                        BorderWidth="1px" CellPadding="1" DayNameFormat="Shortest" Font-Names="Verdana"
                        Font-Size="8pt" ForeColor="#003399" Height="150px" SelectedDate='<%# Bind("VerwachteBeterDag") %>'
                        Width="220px" ToolTip="Wanneer geeft de zieke ZELF aan weer beter te zijn?">
                        <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
                        <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
                        <OtherMonthDayStyle ForeColor="#999999" />
                        <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
                        <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" Font-Bold="True"
                            Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
                        <TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
                        <WeekendDayStyle BackColor="#CCCCFF" />
                    </asp:Calendar>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Omschrijving ziekte" SortExpression="OmschrijvingZiekte">
                <InsertItemTemplate>
                    <asp:TextBox ID="OmschrijvingZiekte" TextMode="MultiLine" Height="50px" Width="220px"
                        ToolTip="Hoe omschrijft de zieke <italic>zelf</italic> de ziekte?" runat="server"
                        Text='<%# Bind("OmschrijvingZiekte") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Voornaam aanmaker ziekmelding" SortExpression="GemeldDoorVoornaam">
                <InsertItemTemplate>
                    <asp:TextBox ID="GemeldDoorVoornaam" ToolTip="Hier jouw Voornaam" Width="220px" runat="server"
                        Text='<%# Bind("GemeldDoorVoornaam") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Achternaam aanmaker" SortExpression="GemedlDoorAchternaam">
                <InsertItemTemplate>
                    <asp:TextBox ID="GemeldDoorAchternaam" Width="220px" ToolTip="Hier jouw achternaam"
                        runat="server" Text='<%# Bind("GemedlDoorAchternaam") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="MeldDatum" HeaderText="MeldDatum" SortExpression="MeldDatum"
                Visible="False" />
            <asp:CheckBoxField DataField="BeterGemeld" HeaderText="BeterGemeld" SortExpression="BeterGemeld"
                Visible="False" />
            <asp:BoundField DataField="BeterMeldDatum" HeaderText="BeterMeldDatum" SortExpression="BeterMeldDatum"
                Visible="False" />
            <asp:TemplateField HeaderText="Alternatieve verblijfplaats zieke" SortExpression="AlernatiefAdres">
                <InsertItemTemplate>
                    <asp:TextBox ID="AlternatiefAdres" Width="220px" TextMode="MultiLine" Height="50px"
                        ToolTip="Indien NIET thuis, waar verblijft de zieke nu? naam ziekenhuis of ander adres"
                        runat="server" Text='<%# Bind("AlernatiefAdres") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Telefoonnummer alternatief adres" SortExpression="AlternatiefTelefoon">
                <InsertItemTemplate>
                    <asp:TextBox ID="AlternatiefTelefoon" Width="220px" ToolTip="Indien ander adres, op welk nummer is de zieke te bereiken?"
                        runat="server" Text='<%# Bind("AlternatiefTelefoon") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Rooster" ItemStyle-BackColor="Silver">
                <ItemStyle BackColor="Silver"></ItemStyle>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Maandag" SortExpression="MaandagVan">
                <InsertItemTemplate>
                    <asp:TextBox ID="MaandagVan" runat="server" Width="100px" Text='<%# Bind("MaandagVan", "{0:t}") %>'
                        ToolTip="Maandag van uu:mm"></asp:TextBox>
                    <asp:TextBox ID="MaandagTot" runat="server" Width="100px" Text='<%# Bind("MaandagTot", "{0:t}") %>'
                        ToolTip="tot uu:mm"></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Dinsdag" SortExpression="DinsdagVan">
                <InsertItemTemplate>
                    <asp:TextBox ID="DinsdagVan" Width="100px" ToolTip="van uu:mm" runat="server" Text='<%# Bind("DinsdagVan", "{0:t}") %>'></asp:TextBox>
                    <asp:TextBox ID="DinsdagTot" Width="100px" ToolTip="tot uu:mm" runat="server" Text='<%# Bind("DinsdagTot", "{0:t}") %>'></asp:TextBox></InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Woensdag" SortExpression="WoensdagVan">
                <InsertItemTemplate>
                    <asp:TextBox ID="WoensdagVan" runat="server" Width="100px" ToolTip="van uu:mm" Text='<%# Bind("WoensdagVan", "{0:t}") %>'></asp:TextBox>
                    <asp:TextBox ID="WoensdagTot" runat="server" Width="100px" ToolTip="tot uu:mm" Text='<%# Bind("WoensdagTot", "{0:t}") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Donderdag" SortExpression="DonderdagVan">
                <InsertItemTemplate>
                    <asp:TextBox ID="DonderdagVan" runat="server" Width="100px" ToolTip="van uu:mm" Text='<%# Bind("DonderdagVan", "{0:t}") %>'></asp:TextBox>
                    <asp:TextBox ID="DonderdagTot" runat="server" Width="100px" ToolTip="tot uu:mm" Text='<%# Bind("DonderdagTot", "{0:t}") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Vrijdag" SortExpression="VrijdagVan">
                <InsertItemTemplate>
                    <asp:TextBox ID="VrijdagVan" runat="server" Width="100px" ToolTip="van uu:mm" Text='<%# Bind("VrijdagVan", "{0:t}") %>'></asp:TextBox>
                    <asp:TextBox ID="VrijdagTot" runat="server" Width="100px" ToolTip="tot uu:mm" Text='<%# Bind("VrijdagTot", "{0:t}") %>'></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Zaterdag" SortExpression="ZaterdagVan">
                <InsertItemTemplate>
                    <asp:TextBox ID="ZaterdagVan" runat="server" Text='<%# Bind("ZaterdagVan", "{0:t}") %>'
                        ToolTip="van uu:mm" Width="100px"></asp:TextBox>
                    <asp:TextBox ID="ZaterdagTot" runat="server" Text='<%# Bind("ZaterdagTot", "{0:t}") %>'
                        ToolTip="tot uu:mm" Width="100px"></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Zondag" SortExpression="ZondagVan">
                <InsertItemTemplate>
                    <asp:TextBox ID="ZondagVan" runat="server" Text='<%# Bind("ZondagVan", "{0:t}") %>'
                        ToolTip="van uu:mm" Width="100px"></asp:TextBox>
                    <asp:TextBox ID="ZondagTot" runat="server" Text='<%# Bind("ZondagTot", "{0:t}") %>'
                        ToolTip="tot uu:mm" Width="100px"></asp:TextBox>
                </InsertItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ButtonType="Button" ShowInsertButton="True" CancelText="Annuleer"
                InsertText="Invoegen" ValidationGroup="ZiekMelding" />
        </Fields>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    </asp:DetailsView>
and finally, the code behind for the customValidator:
Code:
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
        Dim Datum As Calendar = DirectCast(DetailsView1.FindControl("EersteZiekdag"), Calendar)
        Dim FoutDatum As Date = Date.MinValue

        If Datum.SelectedDate > FoutDatum Then
            args.IsValid = True
        Else
            args.IsValid = False

        End If
    End Sub
 
Old January 7th, 2011, 05:54 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

That's not what I meant. Each server control must have a runat="server" attribute or it won't be treated as a control and won't operate correctly.

What I was referring to is the location where the validation takes place. A RequiredFieldValidator by default validates at the client (with JavaScript) and at the server (using .NET). So, if you click the submit button, the client validation kicks in even before the page posts back.

If you only hook up ServerValidate, the CustomValidator will only validate at the server *after* a postback.

Here's what happens:

1. You have a RequiredFieldValidator and a CustomValidator and click Submit. The RequiredFieldValidator validates (at the client), shows an error message and prevents the page from being submitted. You never see the CustomValidator at work until the client page is valid and the page posts back.

1. You only have a CustomValidator and click Submit. No client validation takes place, the page posts back, ServerValidate fires and then after the page is reloaded, the error message is shown (the asterisk in your case).

The client side alert window only appears in scenario 1 as it's only triggered from client side code, not after a postback.

Does this clarify things?

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 7th, 2011, 06:18 PM
Authorized User
 
Join Date: Jun 2010
Posts: 24
Thanks: 10
Thanked 0 Times in 0 Posts
Default

It does clarify, but i don't see how i could solve this...
 
Old January 8th, 2011, 03:58 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

But what is it that you need to solve? This is the built-in behavior of the controls...

Are you trying to display a message for the calendar instead of asterisk? If so, just change the Text and ErrorMessage properties...

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Validation using Validation Framework kalyangvd Struts 1 January 2nd, 2008 06:53 AM
validation dhaval229 ASP.NET 1.0 and 1.1 Professional 2 February 14th, 2006 02:10 PM
Validation g_vamsi_krish ASP.NET 1.0 and 1.1 Professional 2 January 11th, 2006 06:46 AM
Validation NiNe ASP.NET 1.x and 2.0 Application Design 1 July 5th, 2005 10:27 AM





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