我在aspx页面中编写了一个页面页面方法。在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
时出现编译错误:
对非共享成员的引用需要对象引用
如何将Master Page对象或Page传递给Page方法?所以我可以在Sared方法中使用。
有没有办法可以直接在Page方法中访问Textbox值?我需要在Page Method中访问几个控件。
答案 0 :(得分:1)
HttpContext.Current.Handler
可能会引用页面对象,但它没有用,因为页面生命周期不在PageMethods中执行(因此没有视图状态或请求数据)。你有几种选择:
HttpContext.Current.Session
)或缓存在页面上存储数据,然后在PageMethod中检索它。我更喜欢使用带有新guid的缓存作为密钥,然后将guid传递给PageMethod。答案 1 :(得分:1)
不了解$.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
答案 2 :(得分:0)
由于您已经发布了数据,因此您应该能够获得Request.Form集合的引用,您可以使用该集合来阅读发布的文本框值