使用验证器验证从excel文件上传到gridview的数据

时间:2012-10-23 13:45:27

标签: c# asp.net validation

我有一个gridview,它是从用户上传的excel文件中填充的。代码:

<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文件上传数据时,用户可以将任何数据插入到这些单元格中 - 验证器完全被“绕过”。

我想知道是否有办法在上传的数据上应用和执行这些验证器。

0 个答案:

没有答案