ASP.Net - 下拉项目更改后更新面板内的验证错误消失

时间:2017-01-16 11:47:42

标签: c# asp.net validation updatepanel

我在更新面板中有一个提交表单。如果用户单击提交按钮而未填写任何值,则验证消息将显示在所有必填字段上。

现在,当用户从下拉控件中选择或更改项目时,所有验证消息都会消失。下拉控件有 AutoPostBack =“true”

要解决此问题,我尝试将所有下拉控件放在一个更新面板中,并将其他控件放在另一个面板中,但它没有解决问题。

2 个答案:

答案 0 :(得分:2)

您可以在更新面板中尝试此操作:
<asp:UpdatePanel ID="UP1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="DropDownList ID Here" /> </Triggers> <ContentTemplate> </asp:UpdatePanel>

OR

使用JavaScript代码:

if (document.getElementById("<%=DropDownList ID.ClientID%>").value == "--SELECT--") { alert('Your Message Here'); document.getElementById("<%=DropDownList ID.ClientID %>").focus(); return false; } else { return true; }

答案 1 :(得分:0)

您可以在DropDownList的SelectedIndexChanged事件完成后手动调用表单的Validaton。

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    // your code
    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "validateForm", "setTimeout(function () { Page_ClientValidate(); }, 25);", true);
}

如果您使用ValidationGroup,则需要在函数中指定它:Page_ClientValidate('myGroup');

唯一的缺点是它将触发所有验证器,而不仅仅是DropDownList上执行PostBack的用户激活的验证器。

相关问题