RequiredFieldValidator在更新面板中奇怪地工作

时间:2012-08-31 09:48:25

标签: asp.net vb.net updatepanel requiredfieldvalidator

我有一个带有文本框和requiredfieldvalidator以及几个按钮的更新面板。

在页面加载中,您会看到标签和按钮。按下按钮,显示文本框和验证器(以及其他按钮)。

显示的基本变化很好。但是,即使填充了文本框,我的验证器(设置为动态)也会显示其错误消息。如果填充了文本框,按下提交按钮确实有效,但显然我的代码不太正确。

  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
        <%
        If DoNameUpdate = "" then
        %>
          <div class="formrow">
            <div class="standardlabel">Name: </div>
            <div class="fakeformfield"><%=TheName%></div>
            <div class="buttonclass"><asp:ImageButton ID="ImageButton1" ImageUrl="/images/changebutton.gif" alt="Change" CssClass="formsub" runat="server" /></div>
          </div>
        <%
        Else
        %>
        <div class="formrow">
          <asp:Label AssociatedControlID="client_name" CssClass="standardlabel" runat="server" Text="Name "></asp:Label><span class="mandatory">*</span>
          <asp:TextBox ID="client_name" runat="server" MaxLength="255" CssClass="textboxborder"></asp:TextBox><BR>
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="client_name" Display="Dynamic" CssClass="error" ErrorMessage="Required" InitialValue=""></asp:RequiredFieldValidator>
          <div class="buttonclass"><asp:ImageButton ID="ImageButton2" ImageUrl="/images/savebutton.gif" alt="Save Changes" CssClass="formsub" runat="server" /></div>
        </div>
        <%
        End if
        %>
      </ContentTemplate>
  </asp:UpdatePanel>

代码隐藏......

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
    Dim myReader As IDataReader = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), IDataReader)
    If myReader.read then
      client_name.text = myReader("ClientName")
    End if
    myReader.close
  DoNameUpdate = "val"
End Sub
Protected Sub ImageButton2_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
  SqlDataSource2a.update()
  Dim myReader As IDataReader = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), IDataReader)
  If myReader.read then
      TheName = myReader("ClientName")
  End if
  myReader.close
  DoNameUpdate = ""
End Sub

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

在自定义事件上验证updatepanel内的验证器 您可以尝试使用jquery(或其他事件'click','change','keypress','keyup'等)调用'blur'事件的验证。例如:

 function TextBoxValidate() {
    $('input:[type="text"]:[id*="client_name"]').each(function () { $(this).on('blur', function () { var txtId = this.id; $('span').each(function () { if (this.controltovalidate == txtId) ValidatorValidate(this); }); }); }); 
}
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(TextBoxValidate);

只需在javascript文件中添加此代码示例并包含jquery库

即可