RegularExpressionValidator for textbox inside GridView not working

泪湿孤枕 提交于 2019-12-11 04:50:34

问题


I have GridView:

<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
                                AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName"  />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
  <asp:DropDownList ID="dl_situation" runat="server">
   </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
 <ItemTemplate>
         <asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>  
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
 <asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
 </asp:RequiredFieldValidator>
 </ItemTemplate>
</asp:TemplateField>     
</Columns>
</asp:GridView>

It work fine, but when I add RegularrExpressionValidator for textbox with id="tbDate" mygrid does not work This regular expression must accept datetime in format "dd.MM.yyyy"

 <asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
                                AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName"  />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
  <asp:DropDownList ID="dl_situation" runat="server">
   </asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField>
 <ItemTemplate>
         <asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>  
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
 <asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
 <asp:RegularExpressionValidator ID="RegularCert" runat="server" 
      ControlToValidate="tbDate" ErrorMessage="Incorect Data" 
 ValidationExpression=
"(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}" 
      Display="Dynamic"></asp:RegularExpressionValidator>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
      ErrorMessage="*" ControlToValidate="tbDate">
   </asp:RequiredFieldValidator>
 </ItemTemplate>
</asp:TemplateField>     
</Columns>
</asp:GridView>

回答1:


I think you typed "tbData" instead of "tbDate" in the ControlToValidate property

<asp:RegularExpressionValidator ID="RegularCert" runat="server" 
  ControlToValidate="tbDate" ErrorMessage="Incorect Data"   ValidationExpression= "(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}" 
  Display="Dynamic"></asp:RegularExpressionValidator>



回答2:


I changed ClientIDMode to Predictable for TextBox, RegularExpressionValidator and RequiredFieldValidator and began to work




回答3:


change ControlToValidate="tbData" to ControlToValidate="tbDate" for <asp:RegularExpressionValidator




回答4:


Have you tried placing the <asp:RegularExpressionValidator ...> inside the <ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</ItemTemplate> also you need to indicate some text to display if it fails the regex validation

actually what Hallie said is more than likely the answer ha



来源:https://stackoverflow.com/questions/12003490/regularexpressionvalidator-for-textbox-inside-gridview-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!