奇怪的RequiredFieldValidator更新工作面板中 [英] RequiredFieldValidator Working Oddly in Update Panel

查看:154
本文介绍了奇怪的RequiredFieldValidator更新工作面板中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个RequiredFieldValidator和几个按钮的更新面板。

I've got an update panel with a textbox and a requiredfieldvalidator and a couple of buttons.

在页面加载,你会看到一个标签和一个按钮。按钮preSS,文本框和验证器显示(与其他按钮一起)。

On pageload, you see a label and a button. On button press, the textbox and validator display (along with the other button).

在显示器的基本变化工作正常。然而,我的验证程序(其被设定为动态)显示其错误消息,即使该文本框被填充。只要文本框填充,pressing提交按钮不实际工作,但我的code显然事情不是很正确。

The basic change in display works fine. However, my validator (which is set to be dynamic) displays its error message, even if the textbox is populated. Provided that the textbox is populated, pressing the submit button does actually work, but there's obviously something not quite right with my code.

  <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>

而codebehind ...

And the codebehind...

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

有什么建议?

推荐答案

验证验证器内的自定义事件的UpdatePanel。结果
你可以尝试调用使用jQuery模糊事件(或其他事件'点击','变','键preSS','KEYUP等)的验证。例如:

Validate validators inside updatepanel on custom event.
You can try to invoke validation on 'blur' event with jquery (or other event 'click', 'change' ,'keypress', 'keyup' etc.). For example:

 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文件中添加此code样品,包括jQuery库

Just add this code sample in your javascript file and include jquery library

这篇关于奇怪的RequiredFieldValidator更新工作面板中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆