使用onservervalidate ASP.Net C#在同一页面上使用两个自定义验证程序

时间:2013-03-22 21:13:48

标签: c# asp.net entity-framework customvalidator

我目前有TextBoxListBox,其中每个人都在执行某项活动,以便与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显示以下错误,验证应该在ValidatorCalloutExtender位置弹出Right,但会显示此错误。

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按预期工作。

0 个答案:

没有答案