0

我一直在努力使用 .net PageMethods。我有一个文本框,如果从数据库调用返回的值无效或不在表中,我想清除它,现在有一个 javascript 显示弹出消息但无法清除该字段。这是我的代码。

aspx code:
      asp:TextBox name="LoanNumber" ID="LoanNumber" runat="server" size="18" 
     style="font-size: 9pt"       ValidationGroup="requiredCheck" 
     onchange="javascript:SetFocus();loanCheckup();resetOnChange();" MaxLength="10" 
    ToolTip="Enter a Loan Number"  AutoPostBack="false"></asp:TextBox>

  <asp:RequiredFieldValidator   ID="reqLoanNumExists" runat="server"          ControlToValidate="LoanNumber" Display="Dynamic" 
    ErrorMessage="Required Loan Number"
     SetFocusOnError="true" ValidationGroup="requiredCheck">Required!     </asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="reqLoanNumber"
 ControlToValidate="LoanNumber" ValidationExpression="\d+"    Display="Static"             
   EnableClientScript="true"    SetFocusOnError="true" 
          ErrorMessage="Accepts Numbers Only!"    runat="server">
       </asp:RegularExpressionValidator>

页面方法(后面的代码) [WebMethod]
public static string getRowValue(string loanNum) {

    string result = "";
    string tmpResultPass = "";
    string tmpResultFail = "";
 // doing all db connections here 
     try
        {
            if (cmdGetLoanNum.Connection.State == ConnectionState.Closed)
            {
                cmdGetLoanNum.Connection.Open();
            }

            tmpResultPass = cmdGetLoanNum.ExecuteScalar().ToString();

            tmpResultPass = tmpResultPass + "Is Valid";
            return   tmpResultPass ;

        }
        catch (Exception ex)
        {
            string msg = ex.StackTrace.ToString();
             tmpResultFail=  loanNum + " The Existing Loan Number entered does not    appear to be an active loan number.  Please confirm that the value was entered correctly! ";
         return tmpResultFail;


        }
        finally
        {
            cmdGetLoanNum.Connection.Close();
            cmdGetLoanNum.Dispose();
            myConnection.Dispose();
        }

我的java脚本:

    function loanCheckup() {

 // var txtLoanNum = document.getElementById('<%=LoanNumber.ClientID %>').value;

 var txtLoanNum = document.getElementById('LoanNumber').value;
//return Regex.IsMatch(txtLoanNum, "^-?\d*[0-9]?(|.\d*[0-9]|,\d*[0-9])?$");
  PageMethods.getRowValue(txtLoanNum, successLoan,failLoan);


}


 function successLoan(tmpResultPass) {

  alert(tmpResultPass);
 }


 function failLoan(tmpResultFail ) {

  alert(tmpResultFail);
 document.getElementById('LoanNumber').value = "";    
  }

现在,当捕获到异常时,将出现 tmpResultFail,但下一行清除文本字段的行永远不会执行。我有启用 pagemethods = true 的脚本管理器。除了清除文本框字段外,一切正常。任何帮助表示赞赏。

4

1 回答 1

0

未注释的肯定不起作用,因为客户端控件的 ID 与服务器端控件的 ID 不同。如果您查看页面源代码,您的文本框不再称为“LoanNumber”,因此 .getElementById() 将不起作用。

解决方案是:

  1. 使用您的控件的 ClientID 属性,就像您在 javascript 注释中所做的那样。

  2. 使用 ASP.NET 4 ClientMode = "Static" 来获得可预测的客户端 ID。

于 2013-06-11T19:43:27.357 回答