0

我目前有一个TextBoxand ,他们每个人都在做ListBox某项活动来检查MSSQL DB. OnServerValidate然而,问题是只有最后一个Custom Validator有效,而第一个从未得到验证。

见代码:

 Company Name:        
    <asp:TextBox ID="CompanyNameTextBox" runat="server" 
        Height="25px" TabIndex="1" Width="285px"></asp:TextBox>
    <asp:CustomValidator ID="CompanyNameCustomValidator" runat="server" 
        ErrorMessage="Company name already exists" Display="None" ValidateEmptyText="true"
        ControlToValidate="CompanyNameTextBox" EnableClientScript="true"
        onservervalidate="CompanyNameCustomValidator_ServerValidate"></asp:CustomValidator>
    <asp:RequiredFieldValidator ID="CompanyNameRequiredFieldValidator" runat="server" 
        ControlToValidate="CompanyNameTextBox" Display="None"
        ErrorMessage="The company name field is required!" />        
    <ajaxToolkit:ValidatorCalloutExtender ID="CompanyNameRequiredFieldValidator_ValidatorCalloutExtender" 
        runat="server" Enabled="True" PopupPosition="Right" 
        TargetControlID="CompanyNameRequiredFieldValidator">
    </ajaxToolkit:ValidatorCalloutExtender>
    <ajaxToolkit:FilteredTextBoxExtender ID="CompanyNameTextBox_FilteredTextBoxExtender" 
        runat="server" Enabled="True" 
        Filtertype="UppercaseLetters, Lowercaseletters, Numbers, Custom" 
        TargetControlID="CompanyNameTextBox" ValidChars="- ">
    </ajaxToolkit:FilteredTextBoxExtender>  

&nbsp;Country:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:ListBox ID="CountryListBox" runat="server" 
        DataSourceID="CountryDataSource" DataTextField="CountryName" 
        DataValueField="CountryName" Width="285px" 
        SelectionMode="Multiple"></asp:ListBox>
    <asp:CustomValidator ID="CountryListBoxCustomValidator" 
        ControlToValidate="CountryListBox" runat="server" 
        ErrorMessage="Select at least one and maximum allowed is 6 countries" 
        Display="None" ValidateEmptyText="true" EnableClientScript="true"
        onservervalidate="CountryListBoxCustomValidator_ServerValidate" >
    </asp:CustomValidator>
    <ajaxToolkit:ValidatorCalloutExtender ID="CountryListBoxCustomValidator_ValidatorCalloutExtender" 
        runat="server" Enabled="True" TargetControlID="CountryListBoxCustomValidator" PopupPosition="Right">
    </ajaxToolkit:ValidatorCalloutExtender>
    <asp:RequiredFieldValidator ControlToValidate="CountryListbox" runat="server" ID="CountryListBoxRequiredFieldValidator" Display="None" ErrorMessage="The country field is required" />
    <ajaxToolkit:ValidatorCalloutExtender ID="CountryListBoxRequiredFieldValidator_ValidatorCalloutExtender" 
        runat="server" Enabled="True" TargetControlID="CountryListBoxRequiredFieldValidator" PopupPosition="Right">
    </ajaxToolkit:ValidatorCalloutExtender>
    <br />
    <ajaxToolkit:ListSearchExtender ID="CountryListBox_ListSearchExtender" runat="server" 
        Enabled="True" TargetControlID="CountryListBox">
    </ajaxToolkit:ListSearchExtender>

服务器端代码:

protected void CompanyNameCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;

        companynametxt = CompanyNameTextBox.Text;

        companynamequery = (from companylist in db.SupplierDBs
                            where companylist.Company == companynametxt
                            select companylist.Company).SingleOrDefault();
        // Add company to database if case is true
        if (companynametxt != companynamequery)
        {
            CountryListBoxCustomValidator_ServerValidate(source, args);
        }
        else
        {
            args.IsValid = false;
        }
    }

    protected void CountryListBoxCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = CountryListBox.Items.Cast<ListItem>().Where(i => i.Selected).Count() <= 6;

        selectedItemsArray = CountryListBox.GetSelectedIndices(); //gets total selected number of items in listbox
        selectedItemNumber = CountryListBox.GetSelectedIndices().Length;

        if (args.IsValid == true)//selecteditem <= 6 && selecteditem != 0)
        {
            if (selectedItemNumber <= 6 && selectedItemNumber != 0)
            {                                
                switch (selectedItemNumber)
                    {
                        case 1:
                            countrytxttransfer1 = CountryListBox.Items[selectedItemsArray[0]].ToString();                               
                            break;

                        case 2:
                            countrytxttransfer1 = CountryListBox.Items[selectedItemsArray[0]].ToString();
                            countrytxttransfer2 = CountryListBox.Items[selectedItemsArray[1]].ToString();                                 
                            break;

                        case 3:
                            countrytxttransfer1 = CountryListBox.Items[selectedItemsArray[0]].ToString();
                            countrytxttransfer2 = CountryListBox.Items[selectedItemsArray[1]].ToString();
                            countrytxttransfer3 = CountryListBox.Items[selectedItemsArray[2]].ToString();                                
                            break;

                        case 4:
                            countrytxttransfer1 = CountryListBox.Items[selectedItemsArray[0]].ToString();
                            countrytxttransfer2 = CountryListBox.Items[selectedItemsArray[1]].ToString();
                            countrytxttransfer3 = CountryListBox.Items[selectedItemsArray[2]].ToString();
                            countrytxttransfer4 = CountryListBox.Items[selectedItemsArray[3]].ToString();                            
                            break;

                        case 5:
                            countrytxttransfer1 = CountryListBox.Items[selectedItemsArray[0]].ToString();
                            countrytxttransfer2 = CountryListBox.Items[selectedItemsArray[1]].ToString();
                            countrytxttransfer3 = CountryListBox.Items[selectedItemsArray[2]].ToString();
                            countrytxttransfer4 = CountryListBox.Items[selectedItemsArray[3]].ToString();
                            countrytxttransfer5 = CountryListBox.Items[selectedItemsArray[4]].ToString();
                            break;

                        case 6:
                            countrytxttransfer1 = CountryListBox.Items[selectedItemsArray[0]].ToString();
                            countrytxttransfer2 = CountryListBox.Items[selectedItemsArray[1]].ToString();
                            countrytxttransfer3 = CountryListBox.Items[selectedItemsArray[2]].ToString();
                            countrytxttransfer4 = CountryListBox.Items[selectedItemsArray[3]].ToString();
                            countrytxttransfer5 = CountryListBox.Items[selectedItemsArray[4]].ToString();
                            countrytxttransfer6 = CountryListBox.Items[selectedItemsArray[5]].ToString();
                            break;
                    }                                         
                InsertData(); //Inserts data into DB
            }                
        }
    }

CompanyNameTextBox提交表单时显示以下错误,验证应该在位置上弹出一个,ValidatorCalloutExtenderRight它显示此错误。

Violation of UNIQUE KEY constraint 'IX_Company'. Cannot insert duplicate key in object 'dbo.SupplierDB'. The duplicate key value is (IBM). The statement has been terminated.此错误是正确的,但没有调用验证。

CountryListBox按预期工作。

4

0 回答 0