2013-06-11 28 views
0

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

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]
公共静态字符串getRowValue(串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。一切正常,除了清除文本框字段。任何帮助表示赞赏。

回答

0

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

该解决方案可以是:

  1. 使用控件的ClientID属性,当你在一个JavaScript注释一样。

  2. 若要使用ASP.NET 4 ClientMode =“Static”具有可预测的客户端ID。

+0

我尝试了两个建议,但没有奏效。有没有更好的方式在javascript中调用代码,而不是静态的。 ?在此先感谢 – Guru

+0

也许还有其他错误。你能在某个地方发布一个活页吗?否则:浏览器控制台中显示的内容?什么是报告的JS错误(如果有的话)? – jods

相关问题