我在我的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/