javascript - 如何将页面或母版页对象传递给 AJAX 页面方法

标签 javascript asp.net jquery vb.net pagemethods

我在我的aspx页面中写了一个pagePage方法。在 web 服务方法中,我需要调用 FindControl 方法返回文本框并获取文本框值。但是我的 findControl 将使用 MasterPage 对象进行迭代。

请看我的代码

<script type = "text/javascript">
    function ShowCurrentDateTime() {
        $.ajax({
            type: "POST",
            url: "HRDefault.aspx/GetDate",
            data: '',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function(response) {
                alert(response.d);
            }
        });
    }

    function OnSuccess(response) {  }
</script>

<System.Web.Services.WebMethod()> _
Public Shared Function GetDate() As String
    Dim txt22_2 As TextBox = CType(RenderControls.FindControlRecursive
(Page.Master, "txt22_2"), TextBox)
        Dim str As String
        str = txt22_2.Text
    Return String.Empty
End Function

但是我在使用 Page.Master 时遇到编译器错误:

Reference to non-shared member requires an object reference

如何将母版页对象或页面传递给页面方法?。所以我可以在 Sared 方法中使用。

有什么方法可以直接在 Page 方法中访问文本框值吗?我需要访问页面方法中的几个控件。

最佳答案

不知道 $.ajax,但这对我来说很好用:

<asp:ScriptManager runat="server" EnablePageMethods="true" />
<!-- ...................... -->
<script type="text/javascript">
    function ShowCurrentDateTime() {
        x = document.getElementById('<%= TextBox1.ClientID %>').value;
        PageMethods.GetDate(x, OnSuccess, OnFailure);
    }
    function OnSuccess(response) {
        alert(response);
    }
    function OnFailure(response) {
        alert(response._message);
    }
</script>

在后面的代码中:

<System.Web.Services.WebMethod()> _
Public Shared Function GetDate(x as String) As String
    ' do something with x
    ' u can add more params if you need
    Return String.Empty
End Function

希望语法没问题,我不太记得 vb:P

关于javascript - 如何将页面或母版页对象传递给 AJAX 页面方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861013/

相关文章:

c# - 使用授权属性清除 session ?

javascript - 防止 asp.net 中浏览器后退按钮上的 javascript

javascript - $.inArray 深度搜索

javascript - 如何在 JavaScript 中找到 ModalPopupExtender?

javascript - 如何避免javascript命名空间冲突?

javascript - 收到此错误消息 'TypeError: Failed to fetch'

javascript - 如何在iframe中显示用户输入

asp.net - 模态弹出后的页面加载

c# - 测试网络应用程序

jquery - JQGrid:编辑后如何刷新下拉列表?