本文介绍了如何使用自定义验证器取决于下拉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi all,
i have come confusion, how can we achieve this goal
for example, i have a dropdown, which the item is HR, Admin, IT
eg : <asp:DropDownList ID="DropDownList22" runat="server" OnSelectedIndexChanged="DropDownList22_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem>IT</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
<asp:ListItem>HR</asp:ListItem>
</asp:DropDownList>
so, i want to put my validation here in date textbox, only if user select IT, this textbox will validate, for other (Admin and HR) they can left the date empty
<asp:TextBox ID="TextBox32" runat="server" AutoPostBack="true" OnTextChanged="TextBox32_TextChanged"></asp:TextBox>
what kind of validator can i use and how are the validator can be made?
i already try put requiredvalidator, but if i select admin and left the field empty, i got the errormessage too.
<asp:RequiredFieldValidator ID="chkDateReturn" runat="server" ControlToValidate="TextBox32" ErrorMessage="Must select the return date" ></asp:RequiredFieldValidator>
thanks
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (DropDownList22.SelectedItem.Text == "IT")
{
Button1.CausesValidation = true;
}
else
{
Button1.CausesValidation = false;
}
}
}
protected void DropDownList22_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList22.SelectedItem.Text == "IT")
{
Button1.CausesValidation = true;
}
else
{
Button1.CausesValidation = false;
}
}
以下HTML代码..
HTML code below..
<asp:DropDownList ID="DropDownList22" runat="server" OnSelectedIndexChanged="DropDownList22_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>IT</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
<asp:ListItem>HR</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBox32" runat="server" AutoPostBack="true" OnTextChanged="TextBox32_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="chkDateReturn" runat="server" ControlToValidate="TextBox32"
ErrorMessage="Must select the return date"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="false" />
希望它可以帮助你..
Hope it helps you..
这篇关于如何使用自定义验证器取决于下拉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!