Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与onservervalidate ASP.NETC位于同一页面上的两个自定义验证器#_C#_Asp.net_Entity Framework_Customvalidator - Fatal编程技术网

C# 与onservervalidate ASP.NETC位于同一页面上的两个自定义验证器#

C# 与onservervalidate ASP.NETC位于同一页面上的两个自定义验证器#,c#,asp.net,entity-framework,customvalidator,C#,Asp.net,Entity Framework,Customvalidator,我目前有一个文本框和列表框,它们中的每一个都在执行特定的活动来检查服务器验证中的MSSQL DB。但是,问题是只有最后一个自定义验证器在工作,而第一个验证器从未得到验证 参见代码: &nbsp;Company Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <asp:TextBox ID="CompanyNameTextBox" runat="serv

我目前有一个
文本框
列表框
,它们中的每一个都在执行特定的活动来检查
服务器验证
中的
MSSQL DB
。但是,问题是只有最后一个
自定义验证器
在工作,而第一个验证器从未得到验证

参见代码:

&nbsp;Company Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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
            }                
        }
    }
公司名称:
国家:

服务器端代码:

&nbsp;Company Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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
            }                
        }
    }
protectedvoid CompanyNameCustomValidator\u ServerValidate(对象源,ServerValidateEventArgs args)
{
args.IsValid=true;
companynametxt=CompanyNameTextBox.Text;
companynamequery=(来自db.SupplierDBs中的companylist
其中companylist.Company==companynametxt
选择companylist.Company).SingleOrDefault();
//如果案例为真,则将公司添加到数据库
if(companynametxt!=companynamequery)
{
CountryListBoxCustomValidator_ServerValidate(源,参数);
}
其他的
{
args.IsValid=false;
}
}
受保护的void CountryListBoxCustomValidator_ServerValidate(对象源,ServerValidateEventArgs参数)
{

args.IsValid=CountryListBox.Items.Cast().Where(i=>i.Selected.Count())来吧,有人请解释一下……我尝试了几种方法,制作了一个
自定义验证器
,但逻辑永远不会起作用,因为
文本框
正在检查数据库中的公司名称,
列表框
正在检查国家。有什么建议吗?!