I am having a problem and it is probably quite simple and I am missing it.
For my C# and ASP.NET programming class, I am creating a form for someone to sign up for an event online. It has the First and Last name fields, email and confirmation email fields, telephone fields, a preferred date field which the user selects a date from a calendar, an age verification field and a submit button. My problem is that if the user selects the date first, it posts before being able to complete the other fields. How do I stop this?
Thanks for the advice.

What date control are you using? And also you can use required field validator if you want user to must enter some fields?

I am using Visual Studio Web Developer 2008 Express. Below is part of the code. I am using validators to make sure that all required fields are filled in.

<h2>Individual Reservations</h2>
<p>
<asp:Label ID="message" runat="server" Visible="false"></asp:Label>
</p>
<form id="resForm" runat="server">
<p>
                                
<uc:StudentInfo ID="StudentInfo" runat="server" />

Preferred date&nbsp;<asp:RequiredFieldValidator ID="dateValidate" runat="server"
ControlToValidate="date" Text="**Required field**" /><br />
<asp:TextBox ID="date" runat="server" Width="265" /><br />
<br />

<asp:Calendar ID="dateCalendar" OnSelectionChanged = "dateSelected"
 EnableViewState ="false" runat="server" BackColor="White" 
 BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
 DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" 
 ForeColor="#003399" Height="120px" Width="185px">                                  
<SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SelectorStyle BackColor="#99CCCC" ForeColor="#336666" />
<WeekendDayStyle BackColor="#CCCCFF" />
<TodayDayStyle BackColor="#99CCCC" ForeColor="White" />
<OtherMonthDayStyle ForeColor="#999999" />
<NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" />
<DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" />
<TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" 
 Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" />
</asp:Calendar>

<asp:CompareValidator ID="compareDate" runat="server" ControlToValidate="date" Operator="DataTypeCheck" Display="Dynamic" Type="Date">**You did not enter a valid date**</asp:CompareValidator>
</p>
<p>
    Age (18 or older)
    <asp:TextBox ID="age" runat="server" Width="40" />&nbsp;
    <asp:Button ID="makeReservation" runat="server" Text="Submit" />
</p>
<p>
<asp:CompareValidator ID="ageCompare" runat="server" ControlToValidate="age" Operator="GreaterThanEqual" Display="Dynamic" ValueToCompare="18">**You must be 18 or older**</asp:CompareValidator>
</p>
                            </form>

Here is my event handler code as well;

protected void Page_LoadComplete(object source, EventArgs e)
    {

        if (Page.IsPostBack && Page.Request.Params.Get("_EVENTTARGET") != "dateCalendar")
        {
            Page.Validate();
            if (Page.IsValid)
            {
                resForm.Visible = false;
                message.Visible = true;
                message.Text = "<p><strong>Your reservation has been submitted. A confirmation message" +
                                " has been sent to your e-mail address.</strong></p>";
            }//End of IsValid
        }
    } 
        protected void dateSelected(object sender, EventArgs e)
    {
        date.Text = dateCalendar.SelectedDate.ToString("d");
    }

Your Page.Request.Params.Get("_EVENTTARGET") condition in if statement is returning NULL that's the reason on any postback, condition will be true and execute the inner block of if condtion.

Put this code in button onclick event instant of Page_LoadComplete event like:

protected void makeReservation_Click(object sender, EventArgs e)
    {
        Page.Validate();
        if (Page.IsValid)
        {
            resForm.Visible = false;
            message.Visible = true;
            message.Text = "<p><strong>Your reservation has been submitted. A confirmation message" +
                            " has been sent to your e-mail address.</strong></p>";
        }//End of IsValid
    }

Your problem will be solved. Remove the Page_LoadComplete event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.