【问题标题】:asp.net (Visual Studio 2010) form validation controlsasp.net (Visual Studio 2010) 表单验证控件
【发布时间】:2012-06-25 23:10:03
【问题描述】:

下午,

我有一个希望我的用户填写的网络表单。在表单上,​​我希望在三个字段上设置验证。其中一个是普通文本框,另一个是与日历图标关联的文本框,我的最后一个字段是一个下拉列表,其中包含从枚举列表填充的数据。

我还不太担心我的下拉列表,但我对另外两个文本框感到有些沮丧。我在这两个上都使用了“必需的归档验证器”,并且只希望在用户单击提交按钮时启动验证。目前,如果您在这两个字段之间选择或单击,则会启动验证。

这是我的网络表单....

          <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>

如果有人不能指出我正确的方向,那就太好了。非常感谢。

贝蒂

【问题讨论】:

标签: asp.net visual-studio-2010 asp.net-controls


【解决方案1】:

您可以将RequiredFieldValidator的EnableClientScript属性设置为false:

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

只有在用户发布表单后才会进行验证。 希望这会有所帮助,

夏。

【讨论】:

  • 当用户单击日历图标时,验证似乎也会同时启动两个文本框。有什么办法可以解决这个问题。我使用日历图标作为 Ajax 的一部分,这使用户可以通过文本框通过 Ajax 日历选择日期?
  • @Betty B - 如果您的日历图标是 ImageButton,您可以将其 CausesValidation 属性设置为 false。
  • @MTAdmin - 非常感谢这很有效。我还将取消按钮(Imagebutton)的 CausesValidation 设置为 false,因为这也有同样的问题。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
相关资源
最近更新 更多