Search code examples
asp.netvisual-studio-2010asp.net-controls

asp.net (Visual Studio 2010) form validation controls


Afternoon All,

I have a web form that i wish my user to fill out. On the form i have three fields that i wish to set validation on. One of these is a normal textbox, the other a textbox associated with a calendar icon and my last field is a drop down list that has data held in it populated from an Enum list.

Im not too worried about my dropdown list yet, but i am getting slightly frustrated with my other two textboxes. I have used a 'Required Filed Validator' on both of these and only want the validation to kick in when the users clicks the submit button. At the moment if you tab or click between these two fields the validation kicks in.

Here is my web form....

          <table id="table-3">
          <tr>
                <td style="width: 385px"> 
                    <asp:Label ID="lblOrganiser" runat="server" Text="Meeting Organiser:">
                    </asp:Label></td>
                <td class="style4" style="width: 23px">                 
                   <asp:TextBox ID="txtOrganiser" runat="server" >
                    </asp:TextBox>      
                </td>
               <td>
                    <asp:RequiredFieldValidator ID="RequiredFieldVal0"
                     ControlToValidate="txtOrganiser" 
                     ErrorMessage="Meeting Organiser"
                     Text="*" 
                     runat="server"/>
               </td>
              <td>
                 <asp:Label ID="lblDate" runat="server" Width="40px" Text="Date:">
                 </asp:Label>
              </td>   
              <td class="style5">
                  <asp:TextBox ID="txtAgendaDate" runat="server" ForeColor="Black" >
                  </asp:TextBox>
                  <asp:ImageButton runat="Server" ID="ImageButton1" ImageUrl="~/images/contact_date_SM3.png" 
                AlternateText="Click here to display calendar" ImageAlign="AbsMiddle"  /> 
                  <asp:calendarextender ID="CalendarExtender3" runat="server" 
                    TargetControlID="txtAgendaDate" PopupButtonID="ImageButton1" Format="dd/MM/yyyy"></asp:calendarextender>
              </td>
              <td>
                 <asp:RequiredFieldValidator ID="RequiredFieldVal1"
                 ControlToValidate="txtAgendaDate" 
                 ErrorMessage="Date"
                 Text="*" 
                 runat="server"/>
              </td>
            </tr>

             <tr>
                <td style="width: 385px"><asp:Label ID="lblAgendaTypeDescription" runat="server" Text="Type Of Meeting:"></asp:Label></td>
                <td class="style4" style="width: 23px">
                    <asp:TextBox ID="txtAgendaTypeDescription" runat="server" Text="Monthly" BackColor="#F4F4F4" ReadOnly="True"></asp:TextBox>
                 </td>
                   <td></td>
           <td class="style7" style="width: 24px"><asp:Label ID="lblTime" runat="server" Text="Time"></asp:Label></td>
                <td><asp:TextBox ID="txtTime" runat="server" 
                        Text="4pm" style="margin-top: 2px"></asp:TextBox></td>
                 <td></td>
            </tr>
            <tr>
                <td style="width: 385px">
                    <asp:Label ID="lblVenue" runat="server" Text="Venue"></asp:Label>
                </td>
                <td class="style4"  colspan="6"><asp:TextBox ID="txtVenue" runat="server" 
                        Text="Room 1"  Width="550px" TextMode="SingleLine" ></asp:TextBox></td>
            </tr>
            <tr>
                <td style="width: 385px"><asp:Label ID="lblRead" runat="server" Text="Please Read:"></asp:Label></td>
                <td class="style4" colspan="6">
                    <asp:TextBox ID="txtRead" runat="server" 
                        Width="550px" TextMode="SingleLine" Font-Names="Verdana"  ></asp:TextBox></td>         
            </tr>
             <tr>
                <td style="width: 385px"><asp:Label ID="lblBring" runat="server" Text="Please Bring:"></asp:Label></td>
                <td class="style4" colspan="6">
                    <asp:TextBox ID="txtBring" runat="server" 
                        Width="550px" TextMode="SingleLine" Font-Names="Verdana"  ></asp:TextBox></td>         
            </tr>

            <tr>
                <td style="width: 385px"><asp:Label ID="lblAgendaStatus" runat="server" Text = "Agenda Status" ></asp:Label></td>
                <td class="style4" style="width: 23px">
                    <asp:DropDownList ID="AgendaStatus" 
                        runat="server" Height="24px" Width="125px"> </asp:DropDownList>        
                </td>    
                <td> <asp:RequiredFieldValidator ID="RequiredFieldVal2"
                 ControlToValidate="AgendaStatus" 
                 ErrorMessage="Agenda Status"
                 Text="*" 
                 runat="server"/>
                </td>                  
          </tr>
       </table>

        <br />
           <asp:ValidationSummary ID="ValidationSummary2"
            HeaderText="You must enter a value in the following fields:"
            DisplayMode="BulletList"
            EnableClientScript="true"
            runat="server"/>
            <br />

    <div style="text-align: right;">
                  <asp:ImageButton runat="Server" ID="btnAddMonthlyTemplate" ImageUrl="~/images/Addbutton.png" 
                AlternateText="Add Weekly Template"  /> 
          </div>

If anyone cant point me in the right direction that would be great. Many thanks in advance.

Betty


Solution

  • You can set the EnableClientScript property of the RequiredFieldValidator to false:

      <asp:RequiredFieldValidator ID="RequiredFieldVal1"
                     ControlToValidate="txtAgendaDate" 
                     ErrorMessage="Date"
                     Text="*" 
                     EnableClientScript="false"
                     runat="server"/>
    

    that way the validation will accur only after the user posts the form. Hope this helps,

    Shai.