web服务中的引用控件

Reference controls within web service?

本文关键字:引用 控件 服务 web      更新时间:2023-09-26

我有一个onBlur()函数在一个文本框调用web服务。web服务检查在一个SQL表的文本框中输入的电子邮件,看看它是否在那里,如果它是,我需要它来停用一个ASP按钮。(加上一些更繁琐的东西,但一旦我按下按钮,一切就都好了)。然而,每当我试图在web服务中引用按钮控件(或任何其他ASP控件)时,我都会遇到一个错误"不能从共享方法中引用类的实例成员……"

如何禁用按钮&从web服务更改面板的可见性?

onBlur ()

在VB.net

txtEmail.Attributes.Add("onblur", CStr(IIf(c.AccountNo > 0, "", "CallMe(this.id,this.id);")))

Jscript.js文件

//AJAX Call to server side code
function CallMe(src, dest) {
aForgotPwd.style.display = 'none';
var ctrl = document.getElementById(src);
var cont = document.getElementById(btn);
var panel = document.getElementById(pnl);
 // call server side method
return PageMethods.ValidateEmail(ctrl.value, CallSuccess, CallFailed, dest);
}
// set the destination textbox value with the ContactName
function CallSuccess(res, destCtrl) {
var dest = document.getElementById(destCtrl);
if (res == "") {
    if(aForgotPwd.style.display != 'none')
    { aForgotPwd.style.display = 'none'; }
    return true;
} else {
    setTimeout("aForgotPwd.style.display='block';", 1);
    setTimeout("dest.focus();", 1);
    setTimeout("dest.select();", 1);
    alert("We have your email address already in our database. Please visit forgot your password page");

    return false;
}
//alert(res.get_message());
// var dest = document.getElementById(destCtrl);
}

// alert message on some failure
function CallFailed(res, destCtrl) {
var dest = document.getElementById(destCtrl);
 return true;
}

CallMe()函数调用的Web Service

'Email Validation
<System.Web.Services.WebMethod()> _
Public Shared Function ValidateEmail(email As String) As String
    Dim wbClient As WebClient = New WebClient()
    Dim strUrl As String =  ConfigurationManager.AppSettings("WebsiteURLFull") + "/ajax/check_email_address.aspx?Email=" + email
    Dim reqHTML As Byte()
    reqHTML = wbClient.DownloadData(strUrl)
    Dim objUTF8 As UTF8Encoding = New UTF8Encoding()
    Dim output As String = objUTF8.GetString(reqHTML)
   If String.IsNullOrEmpty(output) Then
        exists = False
    Else
        exists = True
        btnContinue.enabled = False
    End If
    If String.IsNullOrEmpty(output) Then Return String.Empty
    Dim c As GPCUser
    If TypeOf HttpContext.Current.Session("Customer") Is GPCUser Then
        c = CType(HttpContext.Current.Session("Customer"), GPCUser)
        If c.AccountNo > 0 Then Return ""
    End If

    Return output
End Function

您不能在web服务方法中访问页面对象,相反,您可以在回调函数中执行web服务后禁用按钮和面板的可见性。只要从你的方法返回一个消息,说电子邮件已经存在或新的。如果我不清楚的话,请告诉我。

编辑您可以在此链接https://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90).aspx

中找到webmethod实现的更多细节。
<System.Web.Services.WebMethod(EnableSession:=True)> _
Public Shared Function ValidateEmail(email As String) As String
Dim wbClient As WebClient = New WebClient()
Dim strUrl As String =  ConfigurationManager.AppSettings("WebsiteURLFull") + "/ajax/check_email_address.aspx?Email=" + email
Dim reqHTML As Byte()
reqHTML = wbClient.DownloadData(strUrl)
Dim objUTF8 As UTF8Encoding = New UTF8Encoding()
Dim output As String = objUTF8.GetString(reqHTML)
If String.IsNullOrEmpty(output) Then
    exists = False
Else
    exists = True
    'btnContinue.enabled = False
    'Commenting the Button enabling
     output="disable"
     'Assinging the output as disable so that in JS you can disable btn
End If
If String.IsNullOrEmpty(output) Then Return String.Empty
Dim c As GPCUser
If TypeOf HttpContext.Current.Session("Customer") Is GPCUser Then
    c = CType(HttpContext.Current.Session("Customer"), GPCUser)
    If c.AccountNo > 0 Then Return ""
End If
 Return output
End Function

同样现在在CallSuccess中,在你继续使用你的功能之前,检查res是否被禁用,然后你可以禁用按钮并显示已经存在的消息