我遇到了验证依赖项控件的问题。如何验证基于dropdownlist(组合框)值的另一个控件。请参阅以下示例。例如:下拉列表有两个值A和B.如果用户选择“A”,则其余字段应成为必填字段,如果用户从下拉列表中选择值“B”,则所有其他字段应成为非必填字段
注意:我正在使用带默认模板的devexpress gridview。
<dx:ASPxGridView ID="ABC" runat="server" AutoGenerateColumns="False"
DataSourceID="ABCDataSource" EnableTheming="True"
Width="100%" ClientInstanceName="gridABC">
<Columns>
<dx:GridViewCommandColumn Caption="Actions" ShowInCustomizationForm="True"
VisibleIndex="0">
<EditButton Visible="True">
</EditButton>
</dx:GridViewCommandColumn>
<dx:GridViewDataComboBoxColumn Caption="List of Type" FieldName="ListType"
ShowInCustomizationForm="True" VisibleIndex="4" Width="100px">
<PropertiesComboBox DataSourceID="ListTypeDataSource"
TextField="ListTypeABC" ValueField="ListTypeABCId" Width="100px">
<ValidationSettings>
<RequiredField ErrorText="ListType required" IsRequired="True" />
</ValidationSettings>
</PropertiesComboBox>
<EditFormSettings ColumnSpan="1" VisibleIndex="1" />
</dx:GridViewDataComboBoxColumn>
<dx:GridViewDataTextColumn Caption="Name" FieldName="Name" ShowInCustomizationForm="True"
VisibleIndex="2" Width="100px">
<PropertiesTextEdit MaxLength="30" Width="100px">
<ValidationSettings>
<RequiredField ErrorText="Item Name required" IsRequired="False" />
</ValidationSettings>
</PropertiesTextEdit>
<EditFormSettings ColumnSpan="1" VisibleIndex="2" />
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Contact" FieldName="Contact" ShowInCustomizationForm="True"
VisibleIndex="3" Width="100px">
<PropertiesTextEdit MaxLength="30" Width="100px">
<ValidationSettings>
<RequiredField ErrorText="Contact" IsRequired="False" />
</ValidationSettings>
</PropertiesTextEdit>
<EditFormSettings ColumnSpan="1" VisibleIndex="3" />
</dx:GridViewDataTextColumn>
<Templates>
<EditForm>
<table>
<tr>
<td>`<dx:ASPxGridViewTemplateReplacement ReplacementType="EditFormEditors" ID="ASPxGridViewTemplateReplacement1"
runat="server">
</dx:ASPxGridViewTemplateReplacement>
`
答案 0 :(得分:1)
最简单的方法是将DropDownList的AutoPostBack属性设置为true并处理它的SelectedIndexChanged事件。然后你可以在那里启用/禁用验证器。
另一种方法是使用CustomValidator。此验证器不依赖于单个控件。您必须自己编写验证规则。例如ClientValidationFunction:
<script type="text/javascript" >
function ClientValidate(source, arguments)
{
var txt = document.getElementById('TextBox1');
var ddl = document.getElementById('DropDownList1');
var decision = ddl.options[ddl.selectedIndex].text;
if(decision=='Yes')
{
arguments.IsValid = txt.value.length > 0;
}else{
arguments.IsValid = true;
}
}
</script>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem Selected="False">No</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="TextBox1" runat="server" />
<asp:Button ID="BtnSubmit" runat="server" Text="Submit" />
<asp:CustomValidator id="CustomValidator1"
ValidateEmptyText="true"
ControlToValidate="TextBox1"
ClientValidationFunction="ClientValidate"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Please enter text!"
runat="server"/>
请记住始终实现OnServerValidate,因为您不应该只依赖于javascript(可以禁用)。这很简单:
void ServerValidation(object source, ServerValidateEventArgs args)
{
args.IsValid = DropDownList1.SelectedIndex == 1 || TextBox1.Text.Length > 0;
}