我一直在努力使用 .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 的脚本管理器。除了清除文本框字段外,一切正常。任何帮助表示赞赏。