I've been encountering some challenges with .net PageMethods. I have a text box and I want to clear it if the retrieved value from the database call is invalid or not in the table. Although I have a JavaScript function that displays a popup message, it fails to clear the field. Below is a snippet of my code.
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>
Page method (code behind)
[WebMethod]
public static string getRowValue(string loanNum)
{
string result = "";
string tmpResultPass = "";
string tmpResultFail = "";
// handling all database 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();
}
MY java script:
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 = "";
}
When an exception is caught, tmpResultFail is displayed, but the subsequent line to clear the text field does not execute. I've ensured that I have the script manager with enable pagemethods set to true. Everything is functioning well except for clearing the textbox field. Any assistance would be greatly appreciated.