Gridview 绑定选定值时出现RequiredValidator错误

Gridview 绑定选定值时出现RequiredValidator错误,gridview,drop-down-menu,telerik,requiredfieldvalidator,Gridview,Drop Down Menu,Telerik,Requiredfieldvalidator,在网格的编辑模式下,有一个下拉列表供用户选择 对于新记录,它将选择默认消息“请选择” 对于现有记录,它将数据绑定db中的值以成为选定值 我添加了requiredvalidator以确保用户选择了dropdownlist。 当用户创建新记录时,它会工作,如果用户未选择,它将显示错误消息 但当用户编辑记录时,即使dropdownlist选择了值,它也会出错。 它仍然显示错误消息 <%@ Page Title="" Language="VB" MasterPageFile="~/LRDB

在网格的编辑模式下,有一个下拉列表供用户选择

对于新记录,它将选择默认消息“请选择” 对于现有记录,它将数据绑定db中的值以成为选定值

我添加了requiredvalidator以确保用户选择了dropdownlist。 当用户创建新记录时,它会工作,如果用户未选择,它将显示错误消息

但当用户编辑记录时,即使dropdownlist选择了值,它也会出错。 它仍然显示错误消息

    <%@ Page Title="" Language="VB" MasterPageFile="~/LRDB.master" AutoEventWireup="false" CodeFile="Admin_dictionary_BU.aspx.vb" Inherits="Admin_Admin_dictionary_BU" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
        <br />
        <strong><span class="style4">Business Unit</span><br /> </strong><br />

                                                            <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
                                                            <script type="text/javascript">
                                                                function RowDblClick(sender, eventArgs) {
                                                                    sender.get_masterTableView().editItem(eventArgs.get_itemIndexHierarchical());
                                                                }

                                                                function conformbox() {
                                                                    var con = confirm("Are you sure want to delete?");
                                                                    if (con == true) {
                                                                        return true;
                                                                    }
                                                                    else {
                                                                        return false;
                                                                    }
                                                                }
                                                            </script>
                                                        </telerik:RadCodeBlock>

                                                        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
                                                        </telerik:RadAjaxLoadingPanel>
                                                            <telerik:RadGrid ID="RadGrid_BU" runat="server" CellSpacing="0" 
                                                            DataSourceID="LDS_BU" width="1000"  PageSize="15"
                                                                    GridLines="None" style="margin-top: 0px" AllowFilteringByColumn="True" 
                                                                    AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
                                                                    ShowStatusBar="True" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
                                                            AllowAutomaticUpdates="True">
                                                                    <GroupingSettings CaseSensitive="false" />
                                                                <MasterTableView AutoGenerateColumns="False" 
                                                                    DataKeyNames="BU_ID" DataSourceID="LDS_BU" 
                                                                    AllowFilteringByColumn="False" AllowPaging="False" 
                                                                    CommandItemDisplay="Top" OverrideDataSourceControlSorting="true" >

                                                                    <CommandItemSettings ExportToPdfText="Export to PDF" />
                                                                    <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" 
                                                                        Visible="True">
                                                                    </RowIndicatorColumn>
                                                                    <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" 
                                                                        Visible="True">
                                                                    </ExpandCollapseColumn>
                                                                    <Columns>
                                                                        <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                                                                        </telerik:GridEditCommandColumn>
                                                                        <telerik:GridBoundColumn DataField="BU_ID" 
                                                                            FilterControlAltText="Filter BU_ID column" HeaderText="BU_ID" 
                                                                            SortExpression="BU_ID" UniqueName="BU_ID" Visible="false">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="BU_Name" 
                                                                            FilterControlAltText="Filter BU_Name column" HeaderText="BU Name" 
                                                                            SortExpression="BU_Name" UniqueName="BU_Name">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="shortname" 
                                                                            FilterControlAltText="Filter shortname column" HeaderText="Shortname" 
                                                                            SortExpression="shortname" UniqueName="shortname">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="bu_addr" 
                                                                            FilterControlAltText="Filter bu_addr column" HeaderText="Address" 
                                                                            SortExpression="bu_addr" UniqueName="bu_addr">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="BU_Tel" 
                                                                            FilterControlAltText="Filter BU_Tel column" HeaderText="Telephone" 
                                                                            SortExpression="BU_Tel" UniqueName="BU_Tel">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="BU_TelPre" 
                                                                            FilterControlAltText="Filter BU_TelPre column" HeaderText="Telephone Prefix" 
                                                                            SortExpression="BU_TelPre" UniqueName="BU_TelPre">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="BU_Fax" 
                                                                            FilterControlAltText="Filter BU_Fax column" HeaderText="Fax No." 
                                                                            SortExpression="BU_Fax" UniqueName="BU_Fax">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="BU_FaxPre" 
                                                                            FilterControlAltText="Filter BU_FaxPre column" HeaderText="Fax No. Prefix" 
                                                                            SortExpression="BU_FaxPre" UniqueName="BU_FaxPre">
                                                                        </telerik:GridBoundColumn>
                                                                        <telerik:GridBoundColumn DataField="BU_SpeedDial" 
                                                                            FilterControlAltText="Filter BU_SpeedDial column" HeaderText="SpeedDial" 
                                                                            SortExpression="BU_SpeedDial" UniqueName="BU_SpeedDial">
                                                                        </telerik:GridBoundColumn>

                                                                    </Columns>
                                                                    <EditFormSettings EditFormType="Template">
                                                                        <EditColumn FilterControlAltText="Filter EditCommandColumn1 column" 
                                                                            UniqueName="EditCommandColumn1">
                                                                        </EditColumn>
                                                                    <FormTemplate>

                                                        <table id="Table2" cellspacing="2" cellpadding="1" width="50%" border="1" rules="none"
                                                            style="border-collapse: collapse;">
                                                            <tr class="EditFormHeader">
                                                            <td style="width:150px">
                                                            <asp:Label ID="Label5" Text="Company" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                                <telerik:RadDropDownList ID="rdl_company" runat="server" DataSourceID="LDS_Company"
                                                                 DataValueField="CompID" DataTextField="CompanyName" DefaultMessage="Please select..." >
                                                                </telerik:RadDropDownList>
                                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="rdl_company" 
                                                                     ErrorMessage="Please select a Company." ></asp:RequiredFieldValidator>   
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="lb" Text="BU Name" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_BU_name" runat="server" Text='<%# Bind("BU_Name") %>' TabIndex="2"></asp:TextBox>                                                            
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="Label1" Text="ShortName" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_shortname" runat="server" Text='<%# Bind("shortname") %>' TabIndex="2"></asp:TextBox>                                                            
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="Label2" Text="Address" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_BU_addr" runat="server" Text='<%# Bind("BU_addr") %>' TabIndex="2" Width="300" TextMode="MultiLine"></asp:TextBox>                                                            
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="Label3" Text="Telephone" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_tel" runat="server" Text='<%# Bind("bu_tel") %>' TabIndex="2"></asp:TextBox>                                                            
                                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Display="Dynamic" 
                                                            ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_tel">
                                                            </asp:RegularExpressionValidator>
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="Label4" Text="Telephone Prefix" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_telpre" runat="server" Text='<%# Bind("bu_telpre") %>' TabIndex="2"></asp:TextBox>                                                            
                                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" Display="Dynamic" 
                                                            ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_telpre">
                                                            </asp:RegularExpressionValidator>
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="Label6" Text="Fax No." runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_fax" runat="server" Text='<%# Bind("bu_fax") %>' TabIndex="2"></asp:TextBox>                                                            
                                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" Display="Dynamic" 
                                                            ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_fax">
                                                            </asp:RegularExpressionValidator>
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="Label7" Text="Fax No. Prefix" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_faxpre" runat="server" Text='<%# Bind("bu_faxpre") %>' TabIndex="2"></asp:TextBox>                                                            
                                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" Display="Dynamic" 
                                                            ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_faxpre">
                                                            </asp:RegularExpressionValidator>
                                                            </td>
                                                            </tr>
                                                            <tr >
                                                            <td >
                                                            <asp:Label ID="Label8" Text="SpeedDial" runat="server"></asp:Label>                                                            
                                                            </td>
                                                            <td>
                                                            <asp:TextBox ID="tb_speeddial" runat="server" Text='<%# Bind("bu_speeddial") %>' TabIndex="2"></asp:TextBox>                                                            
                                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server" Display="Dynamic" 
                                                            ErrorMessage="Please, enter valid phone number." ValidationExpression="\d+" ControlToValidate="tb_speeddial">
                                                            </asp:RegularExpressionValidator>
                                                            </td>
                                                            </tr>
                                                               <tr>
                                                                <td align="right" colspan="2">
                                                                    <asp:Button ID="btnUpdate" Text='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "Insert", "Update") %>'
                                                                        runat="server" CommandName='<%# IIf((TypeOf(Container) is GridEditFormInsertItem), "PerformInsert", "Update")%>' >
                                                                    </asp:Button>&nbsp;
                                                                    <asp:Button ID="btnDelete" Text="Delete" runat="server" CausesValidation="False"
                                                                        CommandName="Delete" OnClientClick="return conformbox();"></asp:Button>
                                                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                                        CommandName="Cancel"></asp:Button>

                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </FormTemplate>
                                                </EditFormSettings>
                                                                    <PagerStyle PageSizeControlType="RadComboBox" />
                                                                </MasterTableView>
                                                                <PagerStyle PageSizeControlType="RadComboBox" />
                                                                <FilterMenu EnableImageSprites="False">
                                                                </FilterMenu>

                                                        </telerik:RadGrid>

                                                <asp:LinqDataSource ID="LDS_Company" 
                                                    runat="server" ContextTypeName="dcLRDBDataContext" EntityTypeName="" 
                                                    TableName="db_Companies">
                                                </asp:LinqDataSource>

                                                        <asp:LinqDataSource ID="LDS_BU" 
                                                    runat="server" ContextTypeName="dcLRDBDataContext" EntityTypeName="" 
                                                    TableName="db_Business_units">
                                                </asp:LinqDataSource>
</asp:Content>

您必须在RequiredFiedValidator中使用验证组

    <asp:RegularExpressionValidator ID="RegularExpressionValidator7" runat="server" ValidationGroup="drop" ControlToValidate="TextBox_UserName" Text="*" ErrorMessage="Username must contain atleast 6 characters!" ForeColor="Red" ValidationExpression="[^\s]{6,30}"></asp:RegularExpressionValidator>

然后你必须在你想要运行验证的按钮上使用相同的验证名称,如下所示

    <asp:Button ID="Button_Add" CssClass="btn btn-primary" Width="40px" runat="server" ValidationGroup="drop" CommandName="Add" OnClick="Button_Add_Click" Text="Add" /><br />

    <asp:Button ID="Button_Add" CssClass="btn btn-primary" Width="40px" runat="server" ValidationGroup="drop" CommandName="Add" OnClick="Button_Add_Click" Text="Add" /><br />