C# 与onservervalidate ASP.NETC位于同一页面上的两个自定义验证器#
我目前有一个C# 与onservervalidate ASP.NETC位于同一页面上的两个自定义验证器#,c#,asp.net,entity-framework,customvalidator,C#,Asp.net,Entity Framework,Customvalidator,我目前有一个文本框和列表框,它们中的每一个都在执行特定的活动来检查服务器验证中的MSSQL DB。但是,问题是只有最后一个自定义验证器在工作,而第一个验证器从未得到验证 参见代码: Company Name: <asp:TextBox ID="CompanyNameTextBox" runat="serv
文本框
和列表框
,它们中的每一个都在执行特定的活动来检查服务器验证
中的MSSQL DB
。但是,问题是只有最后一个自定义验证器
在工作,而第一个验证器从未得到验证
参见代码:
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>
Country:
<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
}
}
}
公司名称:
国家:
服务器端代码:
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>
Country:
<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())来吧,有人请解释一下……我尝试了几种方法,制作了一个自定义验证器
,但逻辑永远不会起作用,因为文本框
正在检查数据库中的公司名称,列表框
正在检查国家。有什么建议吗?!