我有一个从用户上传的excel文件填充的gridview。代码:
<asp:GridView ID="gvData" runat="server" SkinID="GridView" Width="100%" AllowPaging="True" AllowSorting="false"
EmptyDataText="Data not found." AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
OnRowEditing="gvData_RowEditing" OnRowCancelingEdit="gvData_RowCancelingEdit" OnRowUpdating="gvData_RowUpdating" OnRowDeleting="gvData_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="Date" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date", "{0:MM/dd/yyyy}")%>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDate" runat="server" Text='<%#Eval("Date", "{0:MM/dd/yyyy}")%>' CssClass="datepicker"/>
<asp:RequiredFieldValidator ID="rfvDate" runat="server" CssClass="validator" ErrorMessage="Required" Display="Dynamic" ControlToValidate="txtDate"/>
<asp:CompareValidator id="dateValidator" runat="server" CssClass="validator" Type="Date" Operator="DataTypeCheck" ControlToValidate="txtDate" ErrorMessage="Please enter a valid date."/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ref Number" HeaderStyle-ForeColor="White">
<ItemTemplate>
<asp:Label ID="lblRefNumber" runat="server" Text='<%#Eval("Ref Number")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtRefNumber" runat="server" Text='<%#Eval("Ref Number")%>' />
<asp:RegularExpressionValidator ID="revRefNumber" runat="server" ControlToValidate="txtRefNumber" CssClass="validator" Display="Dynamic" ErrorMessage="Numbers only"
ValidationExpression="^[0-9]+$"/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
正如你所看到的,我在每个字段上都没有几个验证器,它们只有在相关文本框被编辑时才会起作用。例如,在用户完成编辑后,我确保日期列中的文本框包含有效的日期值。问题是,当数据首次从 excel 文件上传时,用户可以将任何数据插入这些单元格 - 验证器完全“绕过”。
我想知道是否有办法对上传的数据应用和强制执行这些验证器。