Я боролся с .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, но следующая строка для очистки текстового поля никогда не будет выполнена. У меня есть диспетчер сценариев с enable pagemethods = true. Все работает, кроме очистки поля текстового поля. Любая помощь приветствуется.